pub mod github;
pub mod gitlab;
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum ForgeKind {
GitHub,
GitLab,
}
impl ForgeKind {
pub fn as_str(self) -> &'static str {
match self {
ForgeKind::GitHub => "github",
ForgeKind::GitLab => "gitlab",
}
}
pub fn parse(s: &str) -> Option<Self> {
match s {
"github" => Some(Self::GitHub),
"gitlab" => Some(Self::GitLab),
_ => None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct RepoId {
pub forge: ForgeKind,
pub host: String,
pub path: String,
}
impl RepoId {
pub fn new(forge: ForgeKind, host: &str, path: &str) -> Self {
Self { forge, host: host.to_ascii_lowercase(), path: path.trim_matches('/').trim_end_matches(".git").to_string() }
}
pub fn key(&self) -> String {
format!("{}:{}/{}", self.forge.as_str(), self.host, self.path)
}
pub fn parse_key(s: &str) -> Option<Self> {
let (forge, rest) = s.split_once(':')?;
let (host, path) = rest.split_once('/')?;
Some(Self::new(ForgeKind::parse(forge)?, host, path))
}
}
impl fmt::Display for RepoId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.path)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
None,
Contributor,
Collaborator,
Member,
Maintainer,
Owner,
}
impl Role {
pub fn from_github(assoc: &str) -> Self {
match assoc {
"OWNER" => Role::Owner,
"MEMBER" => Role::Member,
"COLLABORATOR" => Role::Collaborator,
"CONTRIBUTOR" | "FIRST_TIME_CONTRIBUTOR" | "FIRST_TIMER" => Role::Contributor,
_ => Role::None,
}
}
pub fn from_gitlab(level: u32) -> Self {
match level {
50.. => Role::Owner,
40..=49 => Role::Maintainer,
30..=39 => Role::Collaborator,
1..=29 => Role::Contributor,
0 => Role::None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Trigger {
ReviewComment,
Review,
Comment,
CiFailure,
MergeConflict,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Verb {
Triage,
Reproduce,
Fix,
Review,
}
impl Verb {
pub fn parse(s: &str) -> Option<Self> {
match s {
"triage" => Some(Self::Triage),
"reproduce" => Some(Self::Reproduce),
"fix" => Some(Self::Fix),
"review" => Some(Self::Review),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Mode {
MyPrs,
Maintainer,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Target {
Pr,
Issue,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Event {
pub id: String,
pub repo: RepoId,
pub target: Target,
pub number: u64,
pub trigger: Trigger,
pub author: String,
pub role: Role,
pub is_bot: bool,
pub body: String,
pub url: String,
pub thread: Option<String>,
pub path: Option<String>,
pub line: Option<u64>,
pub created_at: String,
pub verb: Option<Verb>,
}
impl Event {
pub fn mode(&self) -> Mode {
if self.verb.is_some() { Mode::Maintainer } else { Mode::MyPrs }
}
}
pub fn now_iso() -> String {
jiff::Timestamp::now().strftime("%Y-%m-%dT%H:%M:%SZ").to_string()
}
pub const OVERLAP_SECS: i64 = 300;
pub fn overlap(since: &str) -> String {
match since.parse::<jiff::Timestamp>() {
Ok(t) => (t - jiff::SignedDuration::from_secs(OVERLAP_SECS)).strftime("%Y-%m-%dT%H:%M:%SZ").to_string(),
Err(_) => since.to_string(),
}
}
pub fn newer(ts: &str, since: Option<&str>) -> bool {
let Some(since) = since else { return true };
match (ts.parse::<jiff::Timestamp>(), since.parse::<jiff::Timestamp>()) {
(Ok(a), Ok(b)) => a > b,
_ => ts > since,
}
}
pub fn parse_command(body: &str) -> Option<Verb> {
body.lines().find_map(|l| {
let mut it = l.trim().strip_prefix("/nightporter")?.split_whitespace();
Verb::parse(it.next()?)
})
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct PrInfo {
pub number: u64,
pub title: String,
pub author: String,
pub state: PrState,
pub head_ref: String,
pub head_sha: String,
pub head_repo_ssh: String,
pub head_repo_https: String,
pub base_ref: String,
pub draft: bool,
pub body: String,
}
#[derive(Clone, Debug, Default)]
pub struct IssueInfo {
pub title: String,
pub body: String,
pub author: String,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PrState {
#[default]
Open,
Closed,
Merged,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Reply {
#[serde(default)]
pub event: Option<String>,
pub body: String,
}
pub enum Forge {
GitHub(github::GitHub),
GitLab(gitlab::GitLab),
}
impl Forge {
pub fn me(&self) -> &str {
match self {
Forge::GitHub(g) => &g.me,
Forge::GitLab(g) => &g.me,
}
}
pub async fn login(&mut self) -> Result<()> {
match self {
Forge::GitHub(g) => g.login().await,
Forge::GitLab(g) => g.login().await,
}
}
pub async fn poll(&mut self, cursor: &mut Cursor) -> Result<Vec<Event>> {
if cursor.since.is_none() {
cursor.since = Some(now_iso());
cursor.last_sweep = Some(crate::state::now());
return Ok(vec![]);
}
match self {
Forge::GitHub(g) => g.poll(cursor).await,
Forge::GitLab(g) => g.poll(cursor).await,
}
}
pub async fn pr(&self, repo: &RepoId, number: u64) -> Result<PrInfo> {
match self {
Forge::GitHub(g) => g.pr(repo, number).await,
Forge::GitLab(g) => g.mr(repo, number).await,
}
}
pub async fn issue(&self, repo: &RepoId, number: u64) -> Result<IssueInfo> {
match self {
Forge::GitHub(g) => g.issue(repo, number).await,
Forge::GitLab(g) => g.issue(repo, number).await,
}
}
pub async fn reply(&self, repo: &RepoId, target: Target, number: u64, to: Option<&Event>, body: &str) -> Result<()> {
match self {
Forge::GitHub(g) => g.reply(repo, target, number, to, body).await,
Forge::GitLab(g) => g.reply(repo, target, number, to, body).await,
}
}
pub async fn resolve(&self, repo: &RepoId, number: u64, thread: &str) -> Result<()> {
match self {
Forge::GitHub(g) => g.resolve(repo, thread).await,
Forge::GitLab(g) => g.resolve(repo, number, thread).await,
}
}
pub async fn open_draft(&self, repo: &RepoId, head: &str, base: &str, title: &str, body: &str) -> Result<String> {
match self {
Forge::GitHub(g) => g.open_draft(repo, head, base, title, body).await,
Forge::GitLab(g) => g.open_draft(repo, head, base, title, body).await,
}
}
pub async fn in_team(&self, team: &str, user: &str) -> Result<bool> {
match self {
Forge::GitHub(g) => g.in_team(team, user).await,
Forge::GitLab(g) => g.in_group(team, user).await,
}
}
}
pub struct Ttl<K, V>(std::sync::Mutex<std::collections::HashMap<K, (i64, V)>>);
impl<K: std::hash::Hash + Eq, V: Clone> Default for Ttl<K, V> {
fn default() -> Self {
Self(std::sync::Mutex::default())
}
}
impl<K: std::hash::Hash + Eq, V: Clone> Ttl<K, V> {
pub fn get(&self, k: &K) -> Option<V> {
let m = self.0.lock().unwrap();
m.get(k).filter(|(at, _)| crate::state::now() - at < 3600).map(|(_, v)| v.clone())
}
pub fn put(&self, k: K, v: V) {
self.0.lock().unwrap().insert(k, (crate::state::now(), v));
}
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Cursor {
#[serde(default)]
pub last_modified: Option<String>,
#[serde(default)]
pub since: Option<String>,
#[serde(default)]
pub poll_interval_secs: Option<u64>,
#[serde(default)]
pub last_sweep: Option<i64>,
#[serde(default)]
pub seen_status: std::collections::BTreeMap<String, String>,
#[serde(default)]
pub swept: bool,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn repo_key_roundtrip() {
let r = RepoId::new(ForgeKind::GitLab, "Git.DrupalCode.org", "project/token.git");
assert_eq!(r.key(), "gitlab:git.drupalcode.org/project/token");
assert_eq!(RepoId::parse_key(&r.key()), Some(r));
}
#[test]
fn timestamps() {
assert!(newer("2026-11-05T10:00:01Z", Some("2026-11-05T10:00:00Z")));
assert!(!newer("2026-11-05T10:00:00Z", Some("2026-11-05T10:00:00Z")));
assert!(newer("2026-11-05T11:30:00.000+01:00", Some("2026-11-05T10:00:00Z")));
assert!(!newer("2026-11-05T10:30:00.000+01:00", Some("2026-11-05T10:00:00Z")));
assert!(newer("x", None));
assert_eq!(overlap("2026-11-05T10:05:00Z"), "2026-11-05T10:00:00Z");
}
#[test]
fn commands() {
assert_eq!(parse_command("thanks!\n/nightporter fix please"), Some(Verb::Fix));
assert_eq!(parse_command(" /nightporter triage"), Some(Verb::Triage));
assert_eq!(parse_command("/nightporter merge"), None);
assert_eq!(parse_command("see /nightporter fix"), None);
}
#[test]
fn roles() {
assert_eq!(Role::from_github("MEMBER"), Role::Member);
assert_eq!(Role::from_github("NONE"), Role::None);
assert_eq!(Role::from_gitlab(40), Role::Maintainer);
assert_eq!(Role::from_gitlab(30), Role::Collaborator);
assert!(Role::Owner > Role::Contributor);
}
}