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}
72
73impl ResolvePullRequests {
74    /// Keeps the first [`MAX_RESOLVE`] distinct qualified keys, in order,
75    /// written as [`PullRequestLink::key`] writes them. Others are dropped.
76    pub fn new(keys: impl IntoIterator<Item = impl AsRef<str>>) -> Self {
77        let mut distinct: Vec<String> = Vec::new();
78        for link in keys
79            .into_iter()
80            .filter_map(|key| PullRequestLink::from_key(key.as_ref()))
81        {
82            if distinct.len() == MAX_RESOLVE {
83                break;
84            }
85            let key = link.key();
86            if !distinct.contains(&key) {
87                distinct.push(key);
88            }
89        }
90        Self { keys: distinct }
91    }
92
93    pub fn keys(&self) -> &[String] {
94        &self.keys
95    }
96}
97
98impl Request for ResolvePullRequests {
99    type Contract = Github;
100    const NAME: &'static str = "resolve-pull-requests";
101    type Response = Vec<Resolution>;
102}
103
104/// What the responder knows about one pull request.
105#[derive(Clone, PartialEq, Eq)]
106#[non_exhaustive]
107pub struct Resolution {
108    pub key: String,
109    pub state: ResolutionState,
110}
111
112impl Resolution {
113    pub fn new(key: &str, state: ResolutionState) -> Self {
114        Self {
115            key: key.into(),
116            state,
117        }
118    }
119}
120
121#[derive(Clone, PartialEq, Eq)]
122#[non_exhaustive]
123pub enum ResolutionState {
124    Available(PullRequestDetails),
125    /// The current account cannot read it. This never says whether it exists.
126    Unavailable,
127    /// A read has started; [`Changed`] follows when it lands.
128    Pending,
129    /// No GitHub account is signed in.
130    SignedOut,
131    /// The read failed for another reason, such as the network.
132    Failed,
133}