forgetop-core 1.10.2

Domain model, configuration, and keychain-backed token storage for forgetop. Internal crate: no API stability guarantees, use the `forgetop` binary instead.
Documentation
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
//! Provider-neutral domain model (mirrors the .NET `Forgetop.Core.Domain`).

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// The three top-level sections, each independently bound to a provider.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum Section {
    #[default]
    PullRequests,
    WorkItems,
    Pipelines,
}

/// The platforms forgetop can talk to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ProviderType {
    Demo,
    GitHub,
    AzureDevOps,
    Linear,
    GitLab,
    Bitbucket,
    Jira,
}

impl ProviderType {
    pub fn as_str(&self) -> &'static str {
        match self {
            ProviderType::Demo => "Demo",
            ProviderType::GitHub => "GitHub",
            ProviderType::AzureDevOps => "AzureDevOps",
            ProviderType::Linear => "Linear",
            ProviderType::GitLab => "GitLab",
            ProviderType::Bitbucket => "Bitbucket",
            ProviderType::Jira => "Jira",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum PullRequestStatus {
    Open,
    Draft,
    Merged,
    Closed,
}

/// Provider-neutral reviewer vote (ADO numeric votes / GitHub review states map here).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReviewVote {
    Rejected,
    WaitingForAuthor,
    NoVote,
    ApprovedWithSuggestions,
    Approved,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WorkItemStateCategory {
    Triage,
    Backlog,
    Unstarted,
    Started,
    /// Past the doing: the work is done and waits on someone else — review, QA, verification.
    /// Still open and still in flight, so whatever treats [`Started`](Self::Started) as active
    /// asks [`is_active`](Self::is_active) and gets this too. What sets it apart is told from
    /// the forge where the forge knows (Linear's state order, Azure's Resolved) and from the
    /// state's name where it doesn't ([`state_name_is_review`]).
    Review,
    Completed,
    Canceled,
}

impl WorkItemStateCategory {
    /// In flight: being worked on, or past that and awaiting review.
    pub fn is_active(self) -> bool {
        matches!(self, WorkItemStateCategory::Started | WorkItemStateCategory::Review)
    }
}

/// Whether a state's *name* says the doing is over and it waits on someone else: "In Review",
/// "Code review", "Ready for QA", "Testing", "Awaiting approval".
///
/// State names are whatever a team typed, so this is the fallback for a forge that gives no
/// order among its in-progress states (Jira), never the first thing asked. It matches whole
/// words, so "Latest" is not "test" and "Preview" is not "review".
pub fn state_name_is_review(name: &str) -> bool {
    const WORDS: [&str; 12] =
        ["review", "reviewing", "qa", "test", "testing", "verify", "verifying", "verification", "uat", "approval", "resolved", "validation"];
    name.split(|c: char| !c.is_alphanumeric()).any(|word| WORDS.iter().any(|w| word.eq_ignore_ascii_case(w)))
}

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

    #[test]
    fn a_review_state_is_told_by_whole_words_in_its_name() {
        for name in ["In Review", "Code review", "Ready for QA", "Testing", "In test", "Awaiting approval", "UAT", "Resolved", "to-verify"] {
            assert!(state_name_is_review(name), "{name}");
        }
        // Not by letters that merely appear inside another word.
        for name in ["In Progress", "Doing", "Latest", "Preview build", "Contested", "Ready for dev", "Blocked", ""] {
            assert!(!state_name_is_review(name), "{name}");
        }
    }

    #[test]
    fn in_review_is_still_in_flight_and_reads_back_from_json() {
        assert!(WorkItemStateCategory::Started.is_active() && WorkItemStateCategory::Review.is_active());
        assert!(!WorkItemStateCategory::Unstarted.is_active() && !WorkItemStateCategory::Completed.is_active());
        let json = serde_json::to_string(&WorkItemStateCategory::Review).unwrap();
        assert_eq!(json, "\"Review\"");
        assert_eq!(serde_json::from_str::<WorkItemStateCategory>(&json).unwrap(), WorkItemStateCategory::Review);
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PipelineRunStatus {
    Queued,
    Running,
    /// In flight but parked: held on an approval gate, so nothing is running and nothing will
    /// until someone acts. A stage behind the gate reads this too.
    Waiting,
    Succeeded,
    PartiallySucceeded,
    Failed,
    Canceled,
    /// Never ran — skipped by its condition, or because something before it failed. Not a
    /// failure in its own right.
    Skipped,
}

impl PipelineRunStatus {
    /// Still in flight: queued, running, or waiting on a gate. Only these can be cancelled or
    /// held by an approval.
    pub fn is_active(self) -> bool {
        matches!(self, Self::Queued | Self::Running | Self::Waiting)
    }

    /// An active status, held: [`Waiting`](Self::Waiting). Callers with a whole run should use
    /// [`PipelineRun::shown_status`], which also checks nothing is still executing.
    pub fn with_pending_approval(self, pending: bool) -> Self {
        if pending && self.is_active() {
            Self::Waiting
        } else {
            self
        }
    }
}

/// Roll-up CI/check state for a pull request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum CheckStatus {
    #[default]
    None,
    Pending,
    Passed,
    Failed,
}

/// Whether a pull request can be merged.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum MergeableState {
    #[default]
    Unknown,
    Mergeable,
    Blocked,
    Conflicting,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FileChangeKind {
    Added,
    Modified,
    Deleted,
    Renamed,
}

// ---- entities ----

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: String,
    pub display_name: String,
    pub handle: Option<String>,
    pub avatar_url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Repository {
    pub id: String,
    pub name: String,
    pub full_name: Option<String>,
    pub default_branch: Option<String>,
    pub url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Comment {
    pub id: String,
    pub author: User,
    pub body: String,
    pub created_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommentThread {
    pub id: String,
    pub comments: Vec<Comment>,
    pub file_path: Option<String>,
    pub line: Option<i64>,
    pub is_resolved: bool,
    /// Whether the forge can mark this thread resolved at all. `false` for threads a forge only
    /// bundles (GitHub's flat conversation comments, GitLab's individual notes, work-item
    /// comments), so a frontend offers "resolve" only where it would work.
    #[serde(default = "resolvable_by_default")]
    pub is_resolvable: bool,
}

/// Serde default for [`CommentThread::is_resolvable`]: a thread cached before the field
/// existed is assumed resolvable, which is the common case.
fn resolvable_by_default() -> bool {
    true
}

/// What happened in a timeline event — drives its icon/colour in the UI.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TimelineEventKind {
    Approved,
    ChangesRequested,
    Reviewed,
    Commented,
    Merged,
    Closed,
    Reopened,
    StateChanged,
    Assigned,
    Labeled,
    Committed,
    Other,
}

/// A single event on a pull request or work item (a review, a merge, a state change, an
/// assignment, …), assembled from whatever the provider's timeline / activity / history API
/// exposes. Ordered oldest → newest.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimelineEvent {
    pub actor: Option<User>,
    pub kind: TimelineEventKind,
    /// Human-readable one-liner, e.g. "approved", "changed status to In Progress", "merged".
    pub summary: String,
    pub at: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Reviewer {
    pub user: User,
    pub vote: ReviewVote,
    pub is_required: bool,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CheckSummary {
    pub successful: u32,
    pub in_progress: u32,
    pub failed: u32,
    pub neutral: u32,
}

impl CheckSummary {
    pub fn total(&self) -> u32 {
        self.successful + self.in_progress + self.failed + self.neutral
    }
}

/// A single named CI check / status on a pull request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckRun {
    pub name: String,
    pub status: CheckStatus,
    pub url: Option<String>,
}

/// A commit on a pull request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Commit {
    pub sha: String,
    pub message: String,
    pub author: String,
    pub date: Option<DateTime<Utc>>,
    pub url: Option<String>,
}

/// Which side of a diff a line belongs to: the old (removed) or new (added) file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DiffSide {
    Old,
    New,
}

/// A pending review comment targeting a specific line of a file in a pull request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LineComment {
    pub path: String,
    pub line: i64,
    pub side: DiffSide,
    pub body: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileChange {
    pub path: String,
    pub kind: FileChangeKind,
    pub additions: i64,
    pub deletions: i64,
    /// Unified-diff patch text when the provider supplies it.
    pub patch: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PullRequest {
    pub id: String,
    /// The repository this pull request lives in, **connection-relative** (`acme/pay`) — see
    /// [`crate::repo`]. `None` for providers that aren't repo-addressed (Jira, Linear).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,
    pub number: Option<i64>,
    pub title: String,
    pub description: Option<String>,
    pub author: User,
    pub status: PullRequestStatus,
    pub is_draft: bool,
    pub source_ref: Option<String>,
    pub target_ref: Option<String>,
    pub reviewers: Vec<Reviewer>,
    pub labels: Vec<String>,
    pub checks: CheckStatus,
    pub check_summary: Option<CheckSummary>,
    pub mergeable: MergeableState,
    pub changed_files: i64,
    pub additions: i64,
    pub deletions: i64,
    pub created_at: Option<DateTime<Utc>>,
    pub updated_at: Option<DateTime<Utc>>,
    pub url: Option<String>,
}

/// What a work item sits under: a milestone, an epic, a project, a parent ticket. Every forge
/// has one and none of them agree on the name, so this carries the forge's own word for it.
///
/// It is what the Work Items list groups by. It is **not** a scope: nothing is fetched or
/// withheld because of it.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkItemParent {
    /// What the forge calls it, for display: "Milestone", "Epic", "Feature", "Project".
    pub kind: String,
    /// Its identity within the connection. Two items share a parent when `kind` and `id` match.
    pub id: String,
    pub title: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkItem {
    pub id: String,
    /// Where this item lives, **connection-relative** — a repository (`acme/pay`) for the
    /// repo-addressed forges, or a bare project name for Azure DevOps, whose work items are
    /// project-addressed rather than repo-addressed. `None` for Jira and Linear.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,
    pub identifier: Option<String>,
    pub title: String,
    pub description: Option<String>,
    pub state: String,
    pub state_category: WorkItemStateCategory,
    pub work_item_type: Option<String>,
    pub assignee: Option<User>,
    pub created_at: Option<DateTime<Utc>>,
    pub updated_at: Option<DateTime<Utc>>,
    pub url: Option<String>,
    /// The milestone, epic or project this sits under, when it has one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent: Option<WorkItemParent>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineDefinition {
    pub id: String,
    /// The repository this definition belongs to, **connection-relative**. `None` for providers
    /// that aren't repo-addressed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,
    pub name: String,
    pub path: Option<String>,
    pub url: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineStep {
    pub name: String,
    pub status: PipelineRunStatus,
    pub started_at: Option<DateTime<Utc>>,
    pub finished_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineJob {
    pub id: String,
    pub name: String,
    pub status: PipelineRunStatus,
    pub started_at: Option<DateTime<Utc>>,
    pub finished_at: Option<DateTime<Utc>>,
    pub steps: Vec<PipelineStep>,
    /// Deep link to this job in the provider's web UI.
    pub url: Option<String>,
    /// A short problem summary for failed jobs (provider-specific: GitLab's
    /// failure reason, Azure's error/warning counts, etc.).
    pub problem: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineStage {
    pub name: String,
    pub status: PipelineRunStatus,
    pub jobs: Vec<PipelineJob>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineRun {
    pub id: String,
    /// The repository this run belongs to, **connection-relative**. `None` for providers that
    /// aren't repo-addressed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,
    pub definition_id: String,
    pub number: Option<i64>,
    pub name: Option<String>,
    /// A human-readable title for the run — the triggering PR title or commit-message subject
    /// where the provider exposes it (GitHub `display_title`). `None` when unavailable.
    pub title: Option<String>,
    pub status: PipelineRunStatus,
    pub triggered_by: Option<User>,
    pub branch: Option<String>,
    pub commit_sha: Option<String>,
    pub started_at: Option<DateTime<Utc>>,
    pub finished_at: Option<DateTime<Utc>>,
    pub url: Option<String>,
    pub stages: Vec<PipelineStage>,
    /// What started the run, in the provider's own words (`push`, `pull_request`,
    /// `schedule`, Azure `individualCI`, GitLab `merge_request_event`, …). `None` when unknown.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub event: Option<String>,
    /// Which attempt of this run this is (GitHub `run_attempt`); `None` where the provider
    /// doesn't number re-runs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub attempt: Option<u32>,
    /// The pull/merge request this run was built for, by number, when the provider says.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pull_request: Option<i64>,
}

/// How serious a [`PipelineAnnotation`] is. Ordered most severe first, so sorting by it puts
/// failures on top.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum AnnotationLevel {
    Failure,
    Warning,
    Notice,
}

/// A problem a run reported, usually anchored to a file and line: a GitHub check-run
/// annotation, a GitLab failed test case, an Azure timeline issue.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PipelineAnnotation {
    pub level: AnnotationLevel,
    pub message: String,
    /// Short heading where the provider gives one (a test name, an annotation title).
    pub title: Option<String>,
    /// Repository-relative file path, when the problem is anchored to one.
    pub path: Option<String>,
    pub line: Option<u32>,
    /// The job that reported it, matching a [`PipelineJob::id`], when known.
    pub job_id: Option<String>,
}

/// A file a run published (build output, report, installer).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PipelineArtifact {
    pub id: String,
    pub name: String,
    pub size_bytes: Option<u64>,
    pub expires_at: Option<DateTime<Utc>>,
    /// Where a person opens or downloads it in a browser.
    pub url: Option<String>,
}

/// A gate on a pipeline run that is waiting for a manual decision (a deployment
/// environment reviewer, an approval check, or a manual job).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PipelineApproval {
    /// Provider-native identifier used to act on this gate (environment id,
    /// approval id, or manual job id, depending on the provider).
    pub id: String,
    /// Human label for the gate — usually the environment or stage name.
    pub name: String,
    /// Whether the authenticated user is allowed to respond to this gate.
    pub can_respond: bool,
    /// Whether the run is held until this gate is answered. An environment approval or an Azure
    /// check is; an optional GitLab manual job (`allow_failure: true`) isn't — the pipeline runs
    /// on without it, so it mustn't make the run read as [`PipelineRunStatus::Waiting`].
    #[serde(default = "holds_by_default")]
    pub blocks_run: bool,
}

fn holds_by_default() -> bool {
    true
}

/// Addressing helpers: each item knows the repository it came from, so a call site never has to
/// reconstruct one. Use these instead of building an [`crate::provider::ItemRef`] by hand.
impl PullRequest {
    pub fn item_ref(&self) -> crate::provider::ItemRef {
        crate::provider::ItemRef::maybe(self.repository.clone(), self.id.clone())
    }
}

impl WorkItem {
    pub fn item_ref(&self) -> crate::provider::ItemRef {
        crate::provider::ItemRef::maybe(self.repository.clone(), self.id.clone())
    }
}

impl PipelineRun {
    pub fn item_ref(&self) -> crate::provider::ItemRef {
        crate::provider::ItemRef::maybe(self.repository.clone(), self.id.clone())
    }

    /// Addresses this run's *definition* (for a re-run/trigger), not the run itself.
    pub fn definition_ref(&self) -> crate::provider::ItemRef {
        crate::provider::ItemRef::maybe(self.repository.clone(), self.definition_id.clone())
    }

    /// Whether any stage, job or step of this run is executing right now. A list row usually
    /// carries no stages, so this is only ever evidence *for* running, never against.
    pub fn has_running_work(&self) -> bool {
        let running = PipelineRunStatus::Running;
        self.stages.iter().any(|s| {
            s.status == running || s.jobs.iter().any(|j| j.status == running || j.steps.iter().any(|t| t.status == running))
        })
    }

    /// The status to show for this run. It reads [`Waiting`](PipelineRunStatus::Waiting) when it
    /// is in flight, held on a gate — a pending approval that `blocks_run`, or a stage the
    /// provider itself reports as waiting — and nothing in it is still executing: a gate further
    /// on doesn't make a run that's still building "waiting". Every surface asks here, so the
    /// list, the run view, the Command Center and the dashboard can't disagree.
    pub fn shown_status(&self, blocking_gate: bool) -> PipelineRunStatus {
        let held = blocking_gate || self.stages.iter().any(|s| s.status == PipelineRunStatus::Waiting);
        self.status.with_pending_approval(held && !self.has_running_work())
    }
}

impl PipelineDefinition {
    pub fn item_ref(&self) -> crate::provider::ItemRef {
        crate::provider::ItemRef::maybe(self.repository.clone(), self.id.clone())
    }
}

/// A decision on a pending [`PipelineApproval`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ApprovalDecision {
    Approve,
    Reject,
}

/// Why a notification fired — each provider's native reason/action maps onto one of these.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NotificationKind {
    /// You were asked to review a PR/MR.
    ReviewRequested,
    /// You were @-mentioned (comment, description, team mention).
    Mention,
    /// Something was assigned to you.
    Assigned,
    /// CI failed on something of yours.
    CiFailed,
    /// A new comment/reply on something you follow.
    Comment,
    /// The state of something you follow changed (merged, closed, status).
    StateChange,
    /// Anything else the provider notified about.
    Other,
}

/// What kind of item a notification points at — drives its icon and which source opens it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NotificationItemType {
    PullRequest,
    WorkItem,
    Pipeline,
    Other,
}

/// One item in the cross-provider notification inbox.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Notification {
    /// Provider-native notification id — used to mark it read.
    pub id: String,
    pub kind: NotificationKind,
    pub item_type: NotificationItemType,
    /// The underlying PR / work-item / pipeline id, for opening it in-app. `None` when the
    /// notification has no resolvable item (then we fall back to the web URL).
    pub item_id: Option<String>,
    /// The repository the item lives in, **connection-relative** — needed to address `item_id`
    /// on a connection that spans several repositories. `None` when the provider doesn't say.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repository: Option<String>,
    /// Subject line — the PR/issue title.
    pub title: String,
    /// Where it lives: `org/repo`, or the project / team name.
    pub context: String,
    /// Web URL, for the browser fallback.
    pub url: Option<String>,
    pub unread: bool,
    pub updated_at: Option<DateTime<Utc>>,
}

#[cfg(test)]
mod pipeline_status_tests {
    use super::PipelineRunStatus::{self, *};

    #[test]
    fn waiting_is_in_flight_and_skipped_is_not() {
        for s in [Queued, Running, Waiting] {
            assert!(s.is_active(), "{s:?}");
        }
        for s in [Succeeded, PartiallySucceeded, Failed, Canceled, Skipped] {
            assert!(!s.is_active(), "{s:?}");
        }
    }

    #[test]
    fn a_pending_gate_holds_only_an_active_run() {
        assert_eq!(Running.with_pending_approval(true), Waiting);
        assert_eq!(Queued.with_pending_approval(true), Waiting);
        assert_eq!(Running.with_pending_approval(false), Running);
        // A finished run's leftover gate doesn't rewrite its outcome.
        assert_eq!(Succeeded.with_pending_approval(true), Succeeded);
        assert_eq!(Failed.with_pending_approval(true), Failed);
    }

    fn run_with(status: PipelineRunStatus, stages: Vec<super::PipelineStage>) -> super::PipelineRun {
        super::PipelineRun {
            event: None, attempt: None, pull_request: None, repository: None, id: "1".into(), definition_id: "d".into(),
            number: None, name: None, title: None, status, triggered_by: None, branch: None, commit_sha: None,
            started_at: None, finished_at: None, url: None, stages,
        }
    }

    fn stage(status: PipelineRunStatus) -> super::PipelineStage {
        super::PipelineStage { name: "s".into(), status, jobs: vec![] }
    }

    #[test]
    fn a_run_reads_waiting_only_when_a_gate_holds_it_and_nothing_runs() {
        // A list row: no stages to say otherwise, so a blocking gate parks it.
        assert_eq!(run_with(Running, vec![]).shown_status(true), Waiting);
        assert_eq!(run_with(Running, vec![]).shown_status(false), Running);
        // A stage still executing keeps the run Running whatever is gated further on.
        assert_eq!(run_with(Running, vec![stage(Succeeded), stage(Running), stage(Queued)]).shown_status(true), Running);
        // The provider's own Waiting stage holds the run even before the approvals call answers.
        assert_eq!(run_with(Running, vec![stage(Succeeded), stage(Waiting)]).shown_status(false), Waiting);
        // A finished run is never rewritten.
        assert_eq!(run_with(Canceled, vec![stage(Waiting)]).shown_status(true), Canceled);
    }

    #[test]
    fn an_approval_from_an_older_cache_still_holds_its_run() {
        let a: super::PipelineApproval = serde_json::from_str(r#"{ "id": "g", "name": "Prod", "can_respond": true }"#).unwrap();
        assert!(a.blocks_run);
    }

    #[test]
    fn new_states_round_trip_by_name() {
        for s in [Waiting, Skipped] {
            let json = serde_json::to_string(&s).unwrap();
            assert_eq!(serde_json::from_str::<PipelineRunStatus>(&json).unwrap(), s);
        }
        assert_eq!(serde_json::to_string(&Waiting).unwrap(), "\"Waiting\"");
    }
}