@sigit / si Mirror · read-only
Fork 0

Mirror of github.com/getsigit/si.

Mirrored from https://github.com/getsigit/si.git

Synced Nov 24, 09:30 UTC

1 //! Commit statuses — how CI reports pass or fail onto a commit.
2
3 use {
4 crate::{
5 client::Client,
6 error::Result,
7 models::{CombinedStatus, CommitStatus},
8 },
9 reqwest::{Method, RequestBuilder},
10 };
11
12 /// Fields accepted by `POST /api/v1/repos/:owner/:repo/statuses/:sha`.
13 #[derive(Debug, Clone, serde::Serialize)]
14 pub struct NewStatus {
15 /// `pending`, `success`, `failure` or `error`.
16 pub state: String,
17 /// Which check this is. The server uses `default` when it is left out.
18 #[serde(skip_serializing_if = "Option::is_none")]
19 pub context: Option<String>,
20 #[serde(skip_serializing_if = "Option::is_none")]
21 pub description: Option<String>,
22 #[serde(skip_serializing_if = "Option::is_none")]
23 pub target_url: Option<String>,
24 }
25
26 impl NewStatus {
27 pub fn new(state: impl Into<String>) -> Self {
28 Self {
29 state: state.into(),
30 context: None,
31 description: None,
32 target_url: None,
33 }
34 }
35
36 pub fn context(mut self, context: Option<String>) -> Self {
37 self.context = context.filter(|c| !c.trim().is_empty());
38 self
39 }
40
41 pub fn description(mut self, description: Option<String>) -> Self {
42 self.description = description.filter(|d| !d.trim().is_empty());
43 self
44 }
45
46 pub fn target_url(mut self, target_url: Option<String>) -> Self {
47 self.target_url = target_url.filter(|u| !u.trim().is_empty());
48 self
49 }
50 }
51
52 impl Client {
53 /// `POST /api/v1/repos/:owner/:repo/statuses/:sha` — report a status on a
54 /// commit. Owner only. Reporting the same context again replaces it.
55 pub async fn create_status(
56 &self,
57 owner: &str,
58 repo: &str,
59 sha: &str,
60 status: &NewStatus,
61 ) -> Result<CommitStatus> {
62 self.send(self.create_status_request(owner, repo, sha, status))
63 .await
64 }
65
66 /// `GET /api/v1/repos/:owner/:repo/commits/:sha/status` — the combined
67 /// state of a commit and every context behind it.
68 pub async fn combined_status(
69 &self,
70 owner: &str,
71 repo: &str,
72 sha: &str,
73 ) -> Result<CombinedStatus> {
74 self.get(&format!("repos/{owner}/{repo}/commits/{sha}/status"))
75 .await
76 }
77
78 fn create_status_request(
79 &self,
80 owner: &str,
81 repo: &str,
82 sha: &str,
83 status: &NewStatus,
84 ) -> RequestBuilder {
85 self.request(
86 Method::POST,
87 &format!("repos/{owner}/{repo}/statuses/{sha}"),
88 )
89 .json(status)
90 }
91 }
92
93 #[cfg(test)]
94 mod tests {
95 use {super::*, crate::Environment};
96
97 fn client() -> Client {
98 Client::builder(Environment::Production)
99 .base_url("https://sigit.si")
100 .token("tok")
101 .build()
102 .unwrap()
103 }
104
105 #[test]
106 fn a_status_is_posted_to_the_commit() {
107 let status = NewStatus::new("success").context(Some("ci/test".into()));
108 let request = client()
109 .create_status_request("seto", "demo", "abc123", &status)
110 .build()
111 .unwrap();
112
113 assert_eq!(request.method(), Method::POST);
114 assert_eq!(
115 request.url().as_str(),
116 "https://sigit.si/api/v1/repos/seto/demo/statuses/abc123"
117 );
118 let body = request.body().and_then(|b| b.as_bytes()).unwrap();
119 let body: serde_json::Value = serde_json::from_slice(body).unwrap();
120 assert_eq!(body["state"], "success");
121 assert_eq!(body["context"], "ci/test");
122 assert!(body.get("description").is_none());
123 assert!(body.get("target_url").is_none());
124 }
125
126 #[test]
127 fn blank_optional_fields_are_left_out() {
128 let status = NewStatus::new("pending")
129 .context(Some(" ".into()))
130 .description(Some(String::new()))
131 .target_url(None);
132 let json = serde_json::to_value(&status).unwrap();
133 assert_eq!(json, serde_json::json!({ "state": "pending" }));
134 }
135
136 #[test]
137 fn a_combined_status_parses() {
138 let body = r#"{"sha":"abc","state":"success","total_count":1,"statuses":[
139 {"id":1,"sha":"abc","state":"success","context":"ci","description":null,
140 "target_url":null,"created_at":"2026-10-01T10:00:00Z","updated_at":"2026-10-01T10:00:00Z"}]}"#;
141 let combined: CombinedStatus = serde_json::from_str(body).unwrap();
142 assert_eq!(combined.total_count, 1);
143 assert_eq!(combined.statuses[0].context, "ci");
144 }
145 }