Skip to main content

subspace_contracts/
github.rs

1//! GitHub pull request navigation, independent of any board or account.
2//!
3//! A board declares that it responds to [`OpenPullRequest`]; requesters declare
4//! that they request it. This request key is a navigation capability, routed
5//! through `BoardNavigation` and the broker, not a synchronous data request.
6//! The responder validates the URL again in `Dashboard::open_link`, opens its
7//! normal detail panel and loads missing data with its own current account.
8//! No credential, account choice or board ID crosses the contract.
9//!
10//! The same responder also resolves pull request references in text to
11//! short summaries ([`ResolvePullRequests`]), with its own current account,
12//! from memory: a reference it has not read yet is
13//! [`ResolutionState::Pending`] and it publishes [`Changed`] when the read
14//! lands. When the viewing account or its access changes, the responder
15//! also publishes [`AccessChanged`] so subscribers can discard held summaries. Summaries are pull request content, so these
16//! types do not implement `Debug`.
17
18mod details;
19pub mod recognition;
20
21use crate::{Contract, Event, Request};
22use recognition::PullRequestLink;
23
24pub use details::{CheckCounts, CheckStatus, PullRequestDetails, PullRequestStatus, ReviewStatus};
25
26/// At most this many pull requests in one [`ResolvePullRequests`].
27pub const MAX_RESOLVE: usize = 50;
28
29pub struct Github;
30
31impl Contract for Github {
32    const NAME: &'static str = "github";
33    const VERSION: u32 = 1;
34}
35
36/// Navigation responder role for a URL recognised by [`recognition::PullRequestLink`].
37/// Used as a `BoardNavigation` routing key; no `contracts::request` or attached
38/// synchronous responder is needed. `Dashboard::open_link` accepts or refuses it.
39#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
40pub struct OpenPullRequest;
41
42impl Request for OpenPullRequest {
43    type Contract = Github;
44    const NAME: &'static str = "open-pull-request";
45    type Response = ();
46}
47
48/// Pull request summaries changed. Subscribers may ask for the latest answers.
49#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
50pub struct Changed;
51
52impl Event for Changed {
53    type Contract = Github;
54    const NAME: &'static str = "changed";
55}
56
57/// The viewing account changed or its access was reset.
58#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
59pub struct AccessChanged;
60
61impl Event for AccessChanged {
62    type Contract = Github;
63    const NAME: &'static str = "access-changed";
64}
65
66/// Asks for short summaries of pull requests by canonical
67/// `owner/repository#number` key, read with the responder's current account.
68#[derive(Clone, PartialEq, Eq)]
69pub struct ResolvePullRequests {
70    keys: Vec<String>,
71    refresh: bool,
72}
73
74impl ResolvePullRequests {
75    /// Keeps the first [`MAX_RESOLVE`] distinct qualified keys, in order,
76    /// written as [`PullRequestLink::key`] writes them. Others are dropped.
77    pub fn new(keys: impl IntoIterator<Item = impl AsRef<str>>) -> Self {
78        let mut distinct: Vec<String> = Vec::new();
79        for link in keys
80            .into_iter()
81            .filter_map(|key| PullRequestLink::from_key(key.as_ref()))
82        {
83            if distinct.len() == MAX_RESOLVE {
84                break;
85            }
86            let key = link.key();
87            if !distinct.contains(&key) {
88                distinct.push(key);
89            }
90        }
91        Self {
92            keys: distinct,
93            refresh: false,
94        }
95    }
96
97    /// Revalidate these specific keys in the background while returning any
98    /// current answers. The responder retains its normal read deduplication.
99    // qual:api - The Channels Space sets this when revalidating the open thread.
100    pub fn refresh(mut self) -> Self {
101        self.refresh = true;
102        self
103    }
104
105    // qual:api - The PRs Space uses this to start a background read.
106    pub fn refresh_requested(&self) -> bool {
107        self.refresh
108    }
109
110    pub fn keys(&self) -> &[String] {
111        &self.keys
112    }
113}
114
115impl Request for ResolvePullRequests {
116    type Contract = Github;
117    const NAME: &'static str = "resolve-pull-requests";
118    type Response = Vec<Resolution>;
119}
120
121/// What the responder knows about one pull request.
122#[derive(Clone, PartialEq, Eq)]
123#[non_exhaustive]
124pub struct Resolution {
125    pub key: String,
126    pub state: ResolutionState,
127}
128
129impl Resolution {
130    pub fn new(key: &str, state: ResolutionState) -> Self {
131        Self {
132            key: key.into(),
133            state,
134        }
135    }
136}
137
138#[derive(Clone, PartialEq, Eq)]
139#[non_exhaustive]
140pub enum ResolutionState {
141    Available(PullRequestDetails),
142    /// The current account cannot read it. This never says whether it exists.
143    Unavailable,
144    /// A read has started; [`Changed`] follows when it lands.
145    Pending,
146    /// No GitHub account is signed in.
147    SignedOut,
148    /// The read failed for another reason, such as the network.
149    Failed,
150}