nightporter 0.6.0

Handles your PRs while you're away: an agentic night shift for GitHub and GitLab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
//! Forge-neutral types plus enum dispatch over GitHub, GitLab and Forgejo/Gitea.

pub mod forgejo;
mod ghlike;
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,
    /// Forgejo and Gitea (one API). `gitea` is accepted in config.
    #[serde(alias = "gitea")]
    Forgejo,
}

impl ForgeKind {
    pub fn as_str(self) -> &'static str {
        match self {
            ForgeKind::GitHub => "github",
            ForgeKind::GitLab => "gitlab",
            ForgeKind::Forgejo => "forgejo",
        }
    }
    pub fn default_host(self) -> &'static str {
        match self {
            ForgeKind::GitHub => "github.com",
            ForgeKind::GitLab => "gitlab.com",
            ForgeKind::Forgejo => "codeberg.org",
        }
    }
    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "github" => Some(Self::GitHub),
            "gitlab" => Some(Self::GitLab),
            "forgejo" | "gitea" => Some(Self::Forgejo),
            _ => None,
        }
    }
}

/// A repository on a forge. `path` is `owner/name` (GitLab may nest: `group/sub/name`).
#[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() }
    }
    /// Stable key used in the database: `github:github.com/owner/name`.
    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)
    }
}

/// Normalised author role. Ordered from least to most privileged.
#[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 {
    /// GitHub `author_association`.
    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,
        }
    }
    /// GitLab access level (10 guest, 20 reporter, 30 developer, 40 maintainer, 50 owner).
    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,
}

/// How a review job's reply is posted: as a GitHub/Forgejo/GitLab review with this outcome.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReviewVerdict {
    Comment,
    RequestChanges,
    Approve,
}

/// What a trusted maintainer asked for in maintainer mode.
#[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 {
    /// Activity on a PR/MR the user authored.
    MyPrs,
    /// A trusted user asked Night Porter to work on someone else's issue/MR.
    Maintainer,
}

/// Kind of thing the event targets.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Target {
    Pr,
    Issue,
}

/// One piece of activity, after polling. Bodies are untrusted until `trust::decide` says otherwise.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Event {
    /// Forge-unique id used for de-duplication, e.g. `gh-review-comment-123`.
    pub id: String,
    pub repo: RepoId,
    pub target: Target,
    pub number: u64,
    pub trigger: Trigger,
    pub author: String,
    /// The author's numeric user id, 0 when unknown. Logins can change hands; ids don't.
    #[serde(default)]
    pub author_id: u64,
    pub role: Role,
    pub is_bot: bool,
    pub body: String,
    pub url: String,
    /// Review thread / discussion id to reply to, when there is one.
    pub thread: Option<String>,
    pub path: Option<String>,
    pub line: Option<u64>,
    pub created_at: String,
    /// Set when the event is a maintainer-mode request (label or `/nightporter <verb>`).
    pub verb: Option<Verb>,
}

impl Event {
    /// An event with no author, text or place yet: set what applies.
    pub fn new(id: String, repo: &RepoId, target: Target, number: u64, trigger: Trigger) -> Self {
        Event {
            id,
            repo: repo.clone(),
            target,
            number,
            trigger,
            author: String::new(),
            author_id: 0,
            role: Role::None,
            is_bot: false,
            body: String::new(),
            url: String::new(),
            thread: None,
            path: None,
            line: None,
            created_at: now_iso(),
            verb: None,
        }
    }

    /// A conflict or CI failure the sweep found on your own PR. `id` carries the head SHA, so
    /// each state fires once per push.
    pub fn state(id: String, repo: &RepoId, number: u64, trigger: Trigger, me: &str, body: String) -> Self {
        Event { author: me.into(), role: Role::Owner, body, ..Self::new(id, repo, Target::Pr, number, trigger) }
    }

    /// Someone asked for your review of PR `number`: a maintainer `review` request from you. One
    /// per head, so a re-request after new pushes gets a fresh review.
    pub fn review_request(repo: &RepoId, number: u64, head_sha: &str, me: &str, me_id: u64) -> Self {
        let id = format!("{REVIEW_REQUEST}{}-{number}-{head_sha}", repo.key());
        let body = "(your review was requested)".into();
        Event { author_id: me_id, verb: Some(Verb::Review), ..Self::state(id, repo, number, Trigger::Comment, me, body) }
    }
    pub fn is_review_request(&self) -> bool {
        self.id.starts_with(REVIEW_REQUEST)
    }

    /// Written by the token's own user: same login and, when both ids are known, the same id
    /// (an account renamed away from your old login isn't you).
    pub fn own(&self, me: &str, me_id: u64) -> bool {
        self.author.eq_ignore_ascii_case(me) && (self.author_id == 0 || me_id == 0 || self.author_id == me_id)
    }

    pub fn mode(&self) -> Mode {
        if self.verb.is_some() { Mode::Maintainer } else { Mode::MyPrs }
    }
}

const REVIEW_REQUEST: &str = "review-request-";

/// Current time as `YYYY-MM-DDTHH:MM:SSZ` (what both forge APIs accept in queries).
pub fn now_iso() -> String {
    jiff::Timestamp::now().strftime("%Y-%m-%dT%H:%M:%SZ").to_string()
}

/// How far back each poll re-reads, to cover late indexing and clock drift.
pub const OVERLAP_SECS: i64 = 300;

/// Where a sweep's catch-up starts: the previous sweep (minus the overlap), or nothing on the
/// first sweep, which only records state.
pub fn catch_up_since(prev_sweep: Option<i64>, swept: bool) -> Option<String> {
    let t = jiff::Timestamp::from_second(prev_sweep.filter(|_| swept)?).ok()?;
    Some(overlap(&t.strftime("%Y-%m-%dT%H:%M:%SZ").to_string()))
}

/// `since` moved back by [`OVERLAP_SECS`].
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(),
    }
}

/// `ts` is strictly after `since`. Parses timestamps (forges mix `Z`, offsets and fractions);
/// falls back to string comparison if either doesn't parse.
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,
    }
}

/// Parse a `/nightporter <verb>` command from the first line that starts with it.
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()?)
    })
}

/// A job command you post on the PR/MR yourself (e.g. from the forge's mobile app). Acts on Night
/// Porter's newest job for that PR, never on the PR itself.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Control {
    Approve,
    Dismiss,
    Retry,
    Status,
}

/// Parse `/nightporter approve|dismiss|retry|status` from the first line that starts with it.
pub fn parse_control(body: &str) -> Option<Control> {
    body.lines().find_map(|l| {
        let mut it = l.split_whitespace();
        if it.next()? != "/nightporter" {
            return None;
        }
        match it.next()? {
            "approve" => Some(Control::Approve),
            "dismiss" => Some(Control::Dismiss),
            "retry" => Some(Control::Retry),
            "status" => Some(Control::Status),
            _ => None,
        }
    })
}

/// A [`Control`] comment written by the token's own user (checked by user id).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ControlComment {
    /// The forge's comment/note id (for the reaction and the processed-once record).
    pub id: String,
    pub control: Control,
}

/// The command in a comment, if the token's own user posted it after `since`.
/// `me_id == 0` (unknown) never matches. Night Porter's own posts never count, nor do edited
/// comments: people with write access can edit yours, so the text may not be your own.
pub fn control_of(
    me_id: u64,
    author_id: u64,
    via_app: bool,
    body: &str,
    created_at: &str,
    updated_at: &str,
    since: &str,
) -> Option<Control> {
    if me_id == 0
        || author_id != me_id
        || via_app
        || body.contains(crate::prompts::FOOTER_MARK)
        || !newer(created_at, Some(since))
        || edited(created_at, updated_at)
    {
        return None;
    }
    parse_control(body)
}

/// Changed more than a second after it was posted (an unknown time counts as edited).
fn edited(created_at: &str, updated_at: &str) -> bool {
    if updated_at.is_empty() || updated_at == created_at {
        return false;
    }
    match (created_at.parse::<jiff::Timestamp>(), updated_at.parse::<jiff::Timestamp>()) {
        (Ok(c), Ok(u)) => u.duration_since(c) > jiff::SignedDuration::from_secs(1),
        _ => true,
    }
}

/// What we need to check out and push a PR/MR.
#[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,
    /// Clone URLs of the repo holding the head branch (may be a fork).
    pub head_repo_ssh: String,
    pub head_repo_https: String,
    pub base_ref: String,
    pub draft: bool,
    pub body: String,
}

/// An issue's text, for maintainer mode. Always untrusted.
#[derive(Clone, Debug, Default)]
pub struct IssueInfo {
    pub title: String,
    pub body: String,
    pub author: String,
    pub open: bool,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PrState {
    #[default]
    Open,
    Closed,
    Merged,
}

/// One reply the agent wants posted.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Reply {
    /// Event id this answers (thread replies), or `None` for a top-level comment.
    #[serde(default)]
    pub event: Option<String>,
    pub body: String,
}

/// One open issue or PR/MR, as listed for `nightporter sweep`. Text is untrusted.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Listed {
    pub number: u64,
    pub title: String,
    pub author: String,
    pub created_at: String,
    pub labels: Vec<String>,
    /// A draft PR/MR (never set for issues).
    pub draft: bool,
    /// Logins it's assigned to.
    pub assignees: Vec<String>,
    pub milestone: Option<Milestone>,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Milestone {
    pub title: String,
    /// Still open (a closed one isn't the current sprint, even with issues left in it).
    pub open: bool,
    /// Due date, `YYYY-MM-DD…` (GitHub and Gitea send a timestamp, GitLab a date).
    pub due: Option<String>,
}

/// `milestone = "@current"`: of the milestones on `items`, the one due soonest from today on.
pub fn current_milestone(items: &[Listed], today: &str) -> Option<String> {
    items
        .iter()
        .filter_map(|i| i.milestone.as_ref())
        .filter(|m| m.open)
        .filter_map(|m| Some((m.due.as_deref()?.get(..10)?, &m.title)))
        .filter(|(due, _)| *due >= today)
        .min()
        .map(|(_, t)| t.clone())
}

/// Which listed items a sweep takes: all set conditions must hold.
#[derive(Clone, Debug, Default)]
pub struct Pick {
    /// Every one of these labels (case-insensitive).
    pub labels: Vec<String>,
    pub created_before: Option<jiff::Timestamp>,
    pub created_after: Option<jiff::Timestamp>,
    /// Leave out this author's items (your own PRs are already watched).
    pub skip_author: Option<String>,
    /// Only items nobody is assigned to.
    pub unassigned: bool,
    /// Only items in this milestone (title, case-insensitive).
    pub milestone: Option<String>,
    /// Only these numbers (e.g. a Project iteration's issues).
    pub numbers: Option<Vec<u64>>,
    pub limit: Option<usize>,
}

/// Oldest first, so a capped sweep works through the backlog in order. Done in one place (not
/// with each forge's query parameters, which differ in label and date semantics).
pub fn pick(mut items: Vec<Listed>, p: &Pick) -> Vec<Listed> {
    let at = |i: &Listed| i.created_at.parse::<jiff::Timestamp>().ok();
    items.retain(|i| {
        p.labels.iter().all(|l| i.labels.iter().any(|x| x.eq_ignore_ascii_case(l)))
            && p.skip_author.as_ref().is_none_or(|a| !i.author.eq_ignore_ascii_case(a))
            && p.created_before.is_none_or(|t| at(i).is_some_and(|c| c < t))
            && p.created_after.is_none_or(|t| at(i).is_some_and(|c| c > t))
            && (!p.unassigned || i.assignees.is_empty())
            && p.numbers.as_ref().is_none_or(|ns| ns.contains(&i.number))
            && p.milestone.as_ref().is_none_or(|m| i.milestone.as_ref().is_some_and(|x| x.title.eq_ignore_ascii_case(m)))
    });
    items.sort_by(|a, b| at(a).cmp(&at(b)).then(a.number.cmp(&b.number)));
    items.truncate(p.limit.unwrap_or(usize::MAX));
    items
}

/// Pages fetched when listing open issues or PRs (100 per page; 50 on Forgejo/Gitea).
pub const LIST_PAGES: usize = 50;

/// A forge client. Enum dispatch (three forges, no trait objects needed).
pub enum Forge {
    GitHub(github::GitHub),
    GitLab(gitlab::GitLab),
    Forgejo(forgejo::Forgejo),
}

/// The same expression on whichever forge this is; every forge has the same method names.
macro_rules! each {
    ($s:expr, $g:ident => $e:expr) => {
        match $s {
            Forge::GitHub($g) => $e,
            Forge::GitLab($g) => $e,
            Forge::Forgejo($g) => $e,
        }
    };
}

impl Forge {
    pub fn new(kind: ForgeKind, host: &str, api: crate::http::Api) -> Self {
        match kind {
            ForgeKind::GitHub => Forge::GitHub(github::GitHub::new(host, api)),
            ForgeKind::GitLab => Forge::GitLab(gitlab::GitLab::new(host, api)),
            ForgeKind::Forgejo => Forge::Forgejo(forgejo::Forgejo::new(host, api)),
        }
    }

    pub fn api_base(kind: ForgeKind, host: &str) -> String {
        match kind {
            ForgeKind::GitHub => github::GitHub::api_base(host),
            ForgeKind::GitLab => gitlab::GitLab::api_base(host),
            ForgeKind::Forgejo => forgejo::Forgejo::api_base(host),
        }
    }

    /// What to watch: subscribed repo paths, the maintainer label per repo with maintainer mode
    /// on, and how often to sweep your open PRs.
    pub fn watch(
        &mut self,
        subscribed: std::collections::HashSet<String>,
        labels: std::collections::HashMap<String, String>,
        sweep_every: std::time::Duration,
    ) {
        each!(self, g => {
            g.subscribed = subscribed;
            g.labels = labels;
            g.sweep_every = sweep_every;
        });
    }

    /// Comments on `number` posted after `since` by the token's own user that are job commands,
    /// oldest first.
    pub async fn controls(&self, repo: &RepoId, target: Target, number: u64, since: &str) -> Result<Vec<ControlComment>> {
        each!(self, g => g.controls(repo, target, number, since).await)
    }

    /// Acknowledge a command comment with a thumbs-up.
    pub async fn ack(&self, repo: &RepoId, target: Target, number: u64, comment: &str) -> Result<()> {
        each!(self, g => g.ack(repo, target, number, comment).await)
    }

    pub fn me(&self) -> &str {
        each!(self, g => &g.me)
    }

    /// The token's scopes, when the forge says: GitHub classic and OAuth tokens
    /// (`X-OAuth-Scopes`), GitLab personal access tokens. `None`: this token doesn't list them
    /// (GitHub fine-grained, GitLab OAuth, Forgejo/Gitea), or the request failed.
    pub async fn scopes(&self) -> Option<Vec<String>> {
        #[derive(serde::Deserialize)]
        struct Pat {
            scopes: Vec<String>,
        }
        match self {
            // `get_cond` only for the headers: with no date sent, it's never "not modified".
            Forge::GitHub(g) => match g.api.get_cond::<serde_json::Value>("/user", None).await.ok()? {
                crate::http::Got::Fresh(_, h) => {
                    let s: Vec<String> = h
                        .get("x-oauth-scopes")?
                        .to_str()
                        .ok()?
                        .split(',')
                        .map(|s| s.trim().to_string())
                        .filter(|s| !s.is_empty())
                        .collect();
                    // An empty header (some app tokens) says nothing either.
                    (!s.is_empty()).then_some(s)
                }
                crate::http::Got::NotModified => None,
            },
            Forge::GitLab(g) => g.api.get::<Pat>("/personal_access_tokens/self").await.ok().map(|p| p.scopes),
            Forge::Forgejo(_) => None,
        }
    }

    pub fn me_id(&self) -> u64 {
        each!(self, g => g.me_id)
    }

    pub async fn login(&mut self) -> Result<()> {
        each!(self, g => g.login().await)
    }

    /// New events since the last poll. `cursor` is opaque, persisted by the caller.
    pub async fn poll(&mut self, cursor: &mut Cursor) -> Result<Vec<Event>> {
        if cursor.since.is_none() {
            // First run: start from now. Never replay history (old comments, CI already red).
            cursor.since = Some(now_iso());
            cursor.last_sweep = Some(crate::state::now());
            return Ok(vec![]);
        }
        each!(self, g => g.poll(cursor).await)
    }

    pub async fn pr(&self, repo: &RepoId, number: u64) -> Result<PrInfo> {
        each!(self, g => g.pr(repo, number).await)
    }

    /// Reviews and comments since `since` (ISO) on your open PRs/MRs in `repo`, read directly
    /// rather than through notifications (`nightporter rescan`). Repeats are de-duplicated.
    pub async fn rescan(&self, repo: &RepoId, since: &str) -> Result<Vec<Event>> {
        each!(self, g => g.rescan(repo, since).await)
    }

    /// The issues of `repo` in a GitHub Project's iteration (`@current`: the one running on
    /// `today`, `YYYY-MM-DD`; else its title). Other forges have no Projects.
    pub async fn iteration_issues(&self, repo: &RepoId, project: &str, iteration: &str, today: &str) -> Result<Vec<u64>> {
        match self {
            Forge::GitHub(g) => g.iteration_issues(repo, project, iteration, today).await,
            _ => anyhow::bail!("Projects and iterations are GitHub only; use a label or a milestone"),
        }
    }

    /// Assign the token's user to an issue (`on`), or take them off it.
    pub async fn assign(&self, repo: &RepoId, n: u64, on: bool) -> Result<()> {
        each!(self, g => g.assign(repo, n, on).await)
    }

    pub async fn comment(&self, repo: &RepoId, n: u64, body: &str) -> Result<()> {
        each!(self, g => g.reply(repo, Target::Issue, n, None, body).await)
    }

    /// Every open issue (`Target::Issue`) or PR/MR (`Target::Pr`) in `repo`.
    pub async fn open_items(&self, repo: &RepoId, target: Target) -> Result<Vec<Listed>> {
        each!(self, g => g.open_items(repo, target).await)
    }

    pub async fn issue(&self, repo: &RepoId, number: u64) -> Result<IssueInfo> {
        each!(self, g => g.issue(repo, number).await)
    }

    /// Reply in the event's thread when it has one, else a top-level comment on the PR/issue.
    pub async fn reply(&self, repo: &RepoId, target: Target, number: u64, to: Option<&Event>, body: &str) -> Result<()> {
        each!(self, g => g.reply(repo, target, number, to, body).await)
    }

    pub async fn resolve(&self, repo: &RepoId, number: u64, thread: &str) -> Result<()> {
        each!(self, g => g.resolve(repo, number, thread).await)
    }

    /// Unresolved review threads on PR `number` that you haven't answered, one event per thread.
    // ponytail: GitHub only; GitLab and Forgejo return nothing until someone needs them there
    // (GitLab discussions carry `resolved`; Forgejo has no thread state to read).
    pub async fn open_threads(&self, repo: &RepoId, number: u64) -> Result<Vec<Event>> {
        match self {
            Forge::GitHub(g) => g.open_threads(repo, number).await,
            Forge::GitLab(_) | Forge::Forgejo(_) => Ok(vec![]),
        }
    }

    /// Post `body` as a review of PR/MR `n` with `verdict`.
    pub async fn review(&self, repo: &RepoId, n: u64, verdict: ReviewVerdict, body: &str) -> Result<()> {
        each!(self, g => g.review(repo, n, verdict, body).await)
    }

    /// Open a PR/MR; `draft` unless it's meant to merge. Returns its URL.
    pub async fn open_pr(&self, repo: &RepoId, head: &str, base: &str, title: &str, body: &str, draft: bool) -> Result<String> {
        each!(self, g => g.open_pr(repo, head, base, title, body, draft).await)
    }

    /// Why Night Porter won't set PRs into `base` to merge by themselves, or `None` when the
    /// forge's rules for it (that this token can read) require passing checks and an approval
    /// of the latest push.
    pub async fn merge_gate(&self, repo: &RepoId, base: &str) -> Result<Option<String>> {
        each!(self, g => g.merge_gate(repo, base).await)
    }

    /// The forge's own auto-merge, for `sha` only: it merges when its rules are met, never before.
    pub async fn enable_auto_merge(&self, repo: &RepoId, n: u64, sha: &str) -> Result<()> {
        each!(self, g => g.enable_auto_merge(repo, n, sha).await)
    }

    /// Team (`org/team`) or GitLab group membership, cached for an hour.
    pub async fn in_team(&self, team: &str, user: &str) -> Result<bool> {
        each!(self, g => g.in_team(team, user).await)
    }
}

/// A small map whose entries expire after an hour (role and team lookups).
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));
    }
}

impl Ttl<String, Vec<String>> {
    /// Whether `user` is in `team`, fetching (and caching) the member list on a miss. `fetch`
    /// must read every page: a member past the first must still match, or `deny_teams` fails open.
    pub async fn member(&self, team: &str, user: &str, fetch: impl Future<Output = Result<Vec<String>>>) -> Result<bool> {
        let members = if let Some(m) = self.get(&team.to_string()) {
            m
        } else {
            let m = fetch.await?;
            self.put(team.into(), m.clone());
            m
        };
        Ok(members.iter().any(|m| m.eq_ignore_ascii_case(user)))
    }
}

/// Opaque per-account poll cursor, stored as JSON in the state db.
#[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>,
    /// Last time the slow "my open PRs" sweep ran (unix secs).
    #[serde(default)]
    pub last_sweep: Option<i64>,
    /// Mergeable / CI status seen last sweep, per `repo#n`, so we only fire on change.
    #[serde(default)]
    pub seen_status: std::collections::BTreeMap<String, String>,
    /// A first (record-only) sweep has happened.
    #[serde(default)]
    pub swept: bool,
}

impl Cursor {
    /// Time for the slow sweep of your open PRs; marks it as run.
    pub fn sweep_due(&mut self, every: std::time::Duration) -> bool {
        let now = crate::state::now();
        let due = self.last_sweep.is_none_or(|t| now - t >= i64::try_from(every.as_secs()).unwrap_or(i64::MAX));
        if due {
            self.last_sweep = Some(now);
        }
        due
    }

    /// One PR's sweep result: its events go out, its status is remembered under `key`. The first
    /// sweep after install only records, so PRs that were already red or conflicted don't all
    /// start jobs at once (set `swept` once a sweep has read the PR list).
    pub fn record(&mut self, key: String, r: Result<(Vec<Event>, String)>, out: &mut Vec<Event>) {
        match r {
            Ok((evs, status)) => {
                if self.swept {
                    out.extend(evs);
                }
                self.seen_status.insert(key, status);
            }
            Err(e) => tracing::warn!("sweep {key}: {e:#}"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sweep_bookkeeping_records_first_then_fires() {
        let mut c = Cursor::default();
        let every = std::time::Duration::from_secs(600);
        assert!(c.sweep_due(every));
        assert!(!c.sweep_due(every), "just ran");
        let repo = RepoId::new(ForgeKind::GitHub, "github.com", "o/r");
        let red = || Ok((vec![Event::state("x".into(), &repo, 1, Trigger::CiFailure, "me", "CI failed".into())], "red".into()));
        let mut out = vec![];
        // First sweep: state only, no events (the PR may have been red for days).
        c.record("k".into(), red(), &mut out);
        assert_eq!((out.len(), c.seen_status["k"].as_str()), (0, "red"));
        c.swept = true;
        c.record("k".into(), red(), &mut out);
        assert_eq!(out.len(), 1);
        assert_eq!((out[0].role, out[0].author.as_str(), out[0].target), (Role::Owner, "me", Target::Pr));
        // A failed read keeps the last status.
        c.record("k".into(), Err(anyhow::anyhow!("boom")), &mut out);
        assert_eq!(c.seen_status["k"], "red");
    }

    #[test]
    fn catch_up_starts_at_the_previous_sweep() {
        let t = "2026-11-09T16:30:00Z".parse::<jiff::Timestamp>().unwrap().as_second();
        assert_eq!(catch_up_since(Some(t), true).as_deref(), Some("2026-11-09T16:25:00Z"), "minus the overlap");
        assert_eq!(catch_up_since(Some(t), false), None, "the first sweep only records");
        assert_eq!(catch_up_since(None, true), None);
    }

    #[test]
    fn own_events_match_by_id_when_known() {
        let repo = RepoId::new(ForgeKind::GitHub, "github.com", "o/r");
        let mut e = Event::new("x".into(), &repo, Target::Pr, 1, Trigger::Comment);
        e.author = "Me".into();
        assert!(e.own("me", 7), "no id on the event: login decides");
        e.author_id = 7;
        assert!(e.own("me", 7) && e.own("me", 0));
        e.author_id = 8;
        assert!(!e.own("me", 7), "someone else now holding your old login");
        e.author = "you".into();
        e.author_id = 7;
        assert!(!e.own("me", 7));
    }

    #[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 controls() {
        assert_eq!(parse_control("/nightporter approve"), Some(Control::Approve));
        assert_eq!(parse_control("lgtm\n  /nightporter retry now"), Some(Control::Retry));
        assert_eq!(parse_control("/nightporter dismiss"), Some(Control::Dismiss));
        assert_eq!(parse_control("/nightporter status"), Some(Control::Status));
        assert_eq!(parse_control("/nightporter merge"), None);
        assert_eq!(parse_control("/nightporter fix"), None);
        assert_eq!(parse_control("please /nightporter approve"), None);
        assert_eq!(parse_control("/nightporterapprove"), None);
        let at = "2026-11-05T10:00:01Z";
        let since = "2026-11-05T10:00:00Z";
        let ok = |me, author, app, body: &str, updated| control_of(me, author, app, body, at, updated, since);
        assert_eq!(ok(7, 7, false, "/nightporter approve", at), Some(Control::Approve));
        assert_eq!(ok(7, 7, false, "/nightporter approve", ""), Some(Control::Approve), "no updated_at");
        assert_eq!(ok(7, 7, false, "/nightporter approve", "2026-11-05T10:00:01.500Z"), Some(Control::Approve), "same second");
        assert_eq!(ok(7, 7, false, "/nightporter approve", "2026-11-05T10:05:00Z"), None, "edited later, maybe by someone else");
        assert_eq!(ok(7, 8, false, "/nightporter approve", at), None, "someone else");
        assert_eq!(ok(0, 0, false, "/nightporter approve", at), None, "unknown id");
        assert_eq!(ok(7, 7, true, "/nightporter approve", at), None, "an app acting as me");
        assert_eq!(control_of(7, 7, false, "/nightporter approve", since, since, since), None, "older than the job");
        let ours = format!("/nightporter approve\n{}", crate::prompts::FOOTER_MARK);
        assert_eq!(ok(7, 7, false, &ours, at), None, "our own post");
    }

    #[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);
    }

    #[test]
    fn current_milestone_is_the_next_one_due() {
        let it = |title: &str, due: Option<&str>| Listed {
            milestone: Some(Milestone { title: title.into(), due: due.map(Into::into), open: title != "closed" }),
            ..Default::default()
        };
        let items = [
            it("old", Some("2026-01-01T00:00:00Z")),
            it("closed", Some("2026-11-20")),
            it("next", Some("2026-12-01")),
            it("later", Some("2027-01-01")),
            it("none", None),
        ];
        assert_eq!(current_milestone(&items, "2026-11-16").as_deref(), Some("next"));
        assert_eq!(current_milestone(&items, "2028-01-01"), None);
        let p = Pick { milestone: Some("NEXT".into()), unassigned: true, ..Default::default() };
        let mut taken = it("next", None);
        taken.assignees = vec!["bob".into()];
        assert_eq!(pick(vec![items[2].clone(), taken, items[3].clone()], &p).len(), 1);
    }

    #[test]
    fn picks_by_label_age_and_author() {
        let it = |n, at: &str, who: &str, labels: &[&str]| Listed {
            number: n,
            title: format!("#{n}"),
            author: who.into(),
            created_at: at.into(),
            labels: labels.iter().map(ToString::to_string).collect(),
            ..Default::default()
        };
        let items = vec![
            it(3, "2026-10-01T00:00:00Z", "ann", &["Bug"]),
            it(1, "2026-01-01T00:00:00Z", "bob", &["bug", "needs review"]),
            it(2, "2026-06-01T00:00:00Z", "me", &[]),
            it(4, "not a date", "ann", &["bug"]),
        ];
        let nums = |p: &Pick| pick(items.clone(), p).iter().map(|i| i.number).collect::<Vec<_>>();
        assert_eq!(nums(&Pick::default()), [4, 1, 2, 3]); // undated first, then oldest
        assert_eq!(nums(&Pick { labels: vec!["BUG".into()], ..Default::default() }), [4, 1, 3]);
        assert_eq!(nums(&Pick { labels: vec!["bug".into(), "needs review".into()], ..Default::default() }), [1]);
        let t = |s: &str| Some(s.parse().unwrap());
        assert_eq!(nums(&Pick { created_before: t("2026-07-01T00:00:00Z"), ..Default::default() }), [1, 2]);
        assert_eq!(nums(&Pick { created_after: t("2026-05-01T00:00:00Z"), ..Default::default() }), [2, 3]);
        assert_eq!(nums(&Pick { skip_author: Some("ME".into()), limit: Some(2), ..Default::default() }), [4, 1]);
    }

    #[test]
    fn forgejo_kind() {
        assert_eq!(ForgeKind::parse("gitea"), Some(ForgeKind::Forgejo));
        assert_eq!(toml::from_str::<toml::Table>("f='gitea'").unwrap()["f"].clone().try_into::<ForgeKind>().unwrap(), ForgeKind::Forgejo);
        let r = RepoId::new(ForgeKind::Forgejo, "Codeberg.org", "o/r");
        assert_eq!(RepoId::parse_key(&r.key()), Some(r));
    }
}