Skip to main content

subspace_contracts/github/
details.rs

1//! A pull request's short summary for a reference's hover card. Optional
2//! facts stay `None` when the responder has not read them.
3
4/// Where a pull request stands.
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6#[non_exhaustive]
7pub enum PullRequestStatus {
8    Open,
9    Draft,
10    Merged,
11    Closed,
12}
13
14/// The review decision GitHub reports.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub enum ReviewStatus {
18    Approved,
19    ChangesRequested,
20    ReviewRequired,
21}
22
23/// The combined state of the head commit's checks.
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub enum CheckStatus {
27    Success,
28    Failure,
29    Pending,
30}
31
32/// How many of the head commit's checks passed, out of how many, such as
33/// 4 of 5. `passed` is never more than `total`.
34#[derive(Clone, Copy, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct CheckCounts {
37    pub passed: u32,
38    pub total: u32,
39}
40
41impl CheckCounts {
42    pub fn new(passed: u32, total: u32) -> Self {
43        Self {
44            passed: passed.min(total),
45            total,
46        }
47    }
48}
49
50/// A short pull request summary, held only in memory for this account.
51#[derive(Clone, PartialEq, Eq)]
52#[non_exhaustive]
53pub struct PullRequestDetails {
54    /// The canonical `owner/repository#number`.
55    pub key: String,
56    pub title: String,
57    /// The canonical pull request URL.
58    pub url: String,
59    pub status: PullRequestStatus,
60    /// The author's login.
61    pub author: Option<String>,
62    pub review: Option<ReviewStatus>,
63    pub checks: Option<CheckStatus>,
64    /// How many checks passed out of how many, beside `checks`; `None` when
65    /// the responder has not read them.
66    pub check_counts: Option<CheckCounts>,
67    /// When it last changed, as RFC 3339.
68    pub updated_at: Option<String>,
69    pub head_ref: Option<String>,
70    pub base_ref: Option<String>,
71    pub additions: Option<u32>,
72    pub deletions: Option<u32>,
73}
74
75// qual:allow(dry, boilerplate) reason: "The record is #[non_exhaustive] so responders in other crates cannot build it literally; these setters are the contract's documented additive builder API (docs/board-integration.md), as IssueSummary's are."
76impl PullRequestDetails {
77    pub fn new(key: &str, title: &str, url: &str, status: PullRequestStatus) -> Self {
78        Self {
79            key: key.into(),
80            title: title.into(),
81            url: url.into(),
82            status,
83            author: None,
84            review: None,
85            checks: None,
86            check_counts: None,
87            updated_at: None,
88            head_ref: None,
89            base_ref: None,
90            additions: None,
91            deletions: None,
92        }
93    }
94
95    // qual:api - Consumed by app crates through the Framework facade.
96    pub fn with_author(mut self, author: Option<&str>) -> Self {
97        self.author = author.map(str::to_owned);
98        self
99    }
100
101    // qual:api - Consumed by app crates through the Framework facade.
102    pub fn with_review(mut self, review: Option<ReviewStatus>) -> Self {
103        self.review = review;
104        self
105    }
106
107    pub fn with_checks(mut self, checks: Option<CheckStatus>) -> Self {
108        self.checks = checks;
109        self
110    }
111
112    /// How many checks passed out of how many.
113    // qual:api - Consumed by app crates through the Framework facade.
114    pub fn with_check_counts(mut self, counts: Option<CheckCounts>) -> Self {
115        self.check_counts = counts;
116        self
117    }
118
119    // qual:api - Consumed by app crates through the Framework facade.
120    pub fn with_updated_at(mut self, updated_at: Option<&str>) -> Self {
121        self.updated_at = updated_at.map(str::to_owned);
122        self
123    }
124
125    /// The head branch and the branch it merges into.
126    // qual:api - Consumed by app crates through the Framework facade.
127    pub fn with_branches(mut self, head: Option<&str>, base: Option<&str>) -> Self {
128        self.head_ref = head.map(str::to_owned);
129        self.base_ref = base.map(str::to_owned);
130        self
131    }
132
133    /// Lines added and deleted.
134    // qual:api - Consumed by app crates through the Framework facade.
135    pub fn with_changes(mut self, additions: Option<u32>, deletions: Option<u32>) -> Self {
136        self.additions = additions;
137        self.deletions = deletions;
138        self
139    }
140}