Skip to main content

subspace_client_github/
lifecycle.rs

1//! Bounded reads of recent closed pull requests and deployment status history.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, de::DeserializeOwned};
5use subspace_core::http::Error;
6
7use crate::Client;
8use crate::actions::{Deployment, DeploymentStatus};
9use crate::validate::{check_login, check_repo, check_sha};
10
11/// Default cadence for full background reads of PRs, Actions and deployments.
12// qual:api — Used by Subspace Desktop through the Framework facade.
13pub const REFRESH_INTERVAL: std::time::Duration = std::time::Duration::from_secs(10 * 60);
14
15// qual:allow(srp, god_struct) reason: "Each reader exposes one field of the GitHub pull request response."
16#[derive(Debug, Deserialize)]
17pub struct ClosedPull {
18    number: u32,
19    merged_at: Option<DateTime<Utc>>,
20    merge_commit_sha: Option<String>,
21    base: Option<PullBase>,
22    body: Option<String>,
23}
24
25#[derive(Debug, Deserialize)]
26struct PullBase {
27    repo: Option<PullRepository>,
28}
29
30#[derive(Debug, Deserialize)]
31struct PullRepository {
32    full_name: String,
33}
34
35impl ClosedPull {
36    // qual:api — Called from the separate Subspace Desktop workspace.
37    pub fn number(&self) -> u32 {
38        self.number
39    }
40
41    // qual:api — Called from the separate Subspace Desktop workspace.
42    pub fn merged_at(&self) -> Option<DateTime<Utc>> {
43        self.merged_at
44    }
45
46    // qual:api — Called from the separate Subspace Desktop workspace.
47    pub fn merge_commit_sha(&self) -> Option<&str> {
48        self.merge_commit_sha.as_deref()
49    }
50
51    // qual:api — Called from the separate Subspace Desktop workspace.
52    pub fn body(&self) -> Option<&str> {
53        self.body.as_deref()
54    }
55
56    /// Whether GitHub reports the pull request merged into this repository.
57    // qual:api — Called from the separate Subspace Desktop workspace.
58    pub fn is_merged_into(&self, repo: &str) -> bool {
59        self.merged_at.is_some()
60            && self
61                .base
62                .as_ref()
63                .and_then(|base| base.repo.as_ref())
64                .is_some_and(|base| base.full_name.eq_ignore_ascii_case(repo))
65    }
66}
67
68// qual:allow(coupling, oi) reason: "These repository reads reuse the GitHub client transport and credentials."
69impl Client {
70    /// The 100 most recently updated closed pull requests.
71    // qual:api — Called from the separate Subspace Desktop workspace.
72    pub fn recent_closed_pulls(&self, login: &str, repo: &str) -> Result<Vec<ClosedPull>, Error> {
73        self.lifecycle_page(
74            login,
75            repo,
76            &["pulls"],
77            &[
78                ("state", "closed"),
79                ("sort", "updated"),
80                ("direction", "desc"),
81            ],
82        )
83    }
84
85    /// Pull requests GitHub associates with the deployment commit.
86    // qual:api — Called from the separate Subspace Desktop workspace.
87    pub fn commit_pulls(
88        &self,
89        login: &str,
90        repo: &str,
91        sha: &str,
92    ) -> Result<Vec<ClosedPull>, Error> {
93        let sha = check_sha(sha)?;
94        self.lifecycle_page(login, repo, &["commits", sha, "pulls"], &[])
95    }
96
97    /// The 100 newest deployment records, without reading each one's history.
98    // qual:api — Called from the separate Subspace Desktop workspace.
99    pub fn recent_deployments(&self, login: &str, repo: &str) -> Result<Vec<Deployment>, Error> {
100        self.lifecycle_page(login, repo, &["deployments"], &[])
101    }
102
103    /// The newest 100 status records for one deployment.
104    // qual:api — Called from the separate Subspace Desktop workspace.
105    pub fn deployment_history(
106        &self,
107        login: &str,
108        repo: &str,
109        id: u64,
110    ) -> Result<Vec<DeploymentStatus>, Error> {
111        self.lifecycle_page(
112            login,
113            repo,
114            &["deployments", &id.to_string(), "statuses"],
115            &[],
116        )
117    }
118
119    fn lifecycle_page<T: DeserializeOwned>(
120        &self,
121        login: &str,
122        repo: &str,
123        path: &[&str],
124        query: &[(&str, &str)],
125    ) -> Result<Vec<T>, Error> {
126        check_login(login)?;
127        let (owner, name) = check_repo(repo)?;
128        let segments = ["repos", owner.as_str(), name.as_str()]
129            .into_iter()
130            .chain(path.iter().copied());
131        let mut query = query.to_vec();
132        query.push(("per_page", "100"));
133        let url = self.rest_url(segments, &query)?;
134        self.rest_json(login, &url)
135    }
136}