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, and whenever the account changes or signs out. Subscribers keep
15//! nothing across a [`Changed`]. 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/// The account changed, it signed out, or pending summaries finished: ask again.
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/// Asks for short summaries of pull requests by canonical
58/// `owner/repository#number` key, read with the responder's current account.
59#[derive(Clone, PartialEq, Eq)]
60pub struct ResolvePullRequests {
61 keys: Vec<String>,
62}
63
64impl ResolvePullRequests {
65 /// Keeps the first [`MAX_RESOLVE`] distinct qualified keys, in order,
66 /// written as [`PullRequestLink::key`] writes them. Others are dropped.
67 pub fn new(keys: impl IntoIterator<Item = impl AsRef<str>>) -> Self {
68 let mut distinct: Vec<String> = Vec::new();
69 for link in keys
70 .into_iter()
71 .filter_map(|key| PullRequestLink::from_key(key.as_ref()))
72 {
73 if distinct.len() == MAX_RESOLVE {
74 break;
75 }
76 let key = link.key();
77 if !distinct.contains(&key) {
78 distinct.push(key);
79 }
80 }
81 Self { keys: distinct }
82 }
83
84 pub fn keys(&self) -> &[String] {
85 &self.keys
86 }
87}
88
89impl Request for ResolvePullRequests {
90 type Contract = Github;
91 const NAME: &'static str = "resolve-pull-requests";
92 type Response = Vec<Resolution>;
93}
94
95/// What the responder knows about one pull request.
96#[derive(Clone, PartialEq, Eq)]
97#[non_exhaustive]
98pub struct Resolution {
99 pub key: String,
100 pub state: ResolutionState,
101}
102
103impl Resolution {
104 pub fn new(key: &str, state: ResolutionState) -> Self {
105 Self {
106 key: key.into(),
107 state,
108 }
109 }
110}
111
112#[derive(Clone, PartialEq, Eq)]
113#[non_exhaustive]
114pub enum ResolutionState {
115 Available(PullRequestDetails),
116 /// The current account cannot read it. This never says whether it exists.
117 Unavailable,
118 /// A read has started; [`Changed`] follows when it lands.
119 Pending,
120 /// No GitHub account is signed in.
121 SignedOut,
122 /// The read failed for another reason, such as the network.
123 Failed,
124}