subspace_client_github/
lifecycle.rs1use 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
11pub const REFRESH_INTERVAL: std::time::Duration = std::time::Duration::from_secs(10 * 60);
14
15#[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 pub fn number(&self) -> u32 {
38 self.number
39 }
40
41 pub fn merged_at(&self) -> Option<DateTime<Utc>> {
43 self.merged_at
44 }
45
46 pub fn merge_commit_sha(&self) -> Option<&str> {
48 self.merge_commit_sha.as_deref()
49 }
50
51 pub fn body(&self) -> Option<&str> {
53 self.body.as_deref()
54 }
55
56 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
68impl Client {
70 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 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 pub fn recent_deployments(&self, login: &str, repo: &str) -> Result<Vec<Deployment>, Error> {
100 self.lifecycle_page(login, repo, &["deployments"], &[])
101 }
102
103 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}