Skip to content

Commit e4251f2

Browse files
Checks API (#461)
1 parent 7194ac5 commit e4251f2

File tree

1 file changed

+280
-5
lines changed

1 file changed

+280
-5
lines changed

src/api/checks.rs

+280-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use crate::models::CheckSuiteId;
1+
use crate::models::{CheckRunId, CheckSuiteId};
2+
use crate::params::repos::Commitish;
23
use crate::{models, Octocrab, Result};
4+
use chrono::{DateTime, Utc};
35

46
/// Handler for GitHub's Checks API.
57
///
@@ -11,7 +13,177 @@ pub struct ChecksHandler<'octo> {
1113
}
1214

1315
#[derive(serde::Serialize)]
14-
pub struct ListCheckRunsinCheckSuiteBuilder<'octo, 'r> {
16+
#[serde(rename_all = "snake_case")]
17+
pub enum CheckRunStatus {
18+
Queued,
19+
InProgress,
20+
Completed,
21+
}
22+
23+
#[derive(serde::Serialize)]
24+
pub struct CreateCheckRunBuilder<'octo, 'r> {
25+
#[serde(skip)]
26+
handler: &'r ChecksHandler<'octo>,
27+
name: String,
28+
head_sha: String,
29+
#[serde(skip_serializing_if = "Option::is_none")]
30+
details_url: Option<String>,
31+
#[serde(skip_serializing_if = "Option::is_none")]
32+
external_id: Option<String>,
33+
#[serde(skip_serializing_if = "Option::is_none")]
34+
status: Option<CheckRunStatus>,
35+
}
36+
37+
impl<'octo, 'r> CreateCheckRunBuilder<'octo, 'r> {
38+
pub(crate) fn new(handler: &'r ChecksHandler<'octo>, name: String, head_sha: String) -> Self {
39+
Self {
40+
handler,
41+
name,
42+
head_sha,
43+
details_url: None,
44+
external_id: None,
45+
status: None,
46+
}
47+
}
48+
49+
/// The URL of the integrator's site that has the full details of the check.
50+
/// If the integrator does not provide this, then the homepage of the GitHub app is used.
51+
pub fn details_url(mut self, details_url: impl Into<String>) -> Self {
52+
self.details_url = Some(details_url.into());
53+
self
54+
}
55+
56+
/// A reference for the run on the integrator's system.
57+
pub fn external_id(mut self, external_id: impl Into<String>) -> Self {
58+
self.external_id = Some(external_id.into());
59+
self
60+
}
61+
62+
/// The current status.
63+
/// Can be one of `queued`, `in_progress`, or `completed`.
64+
pub fn status(mut self, status: CheckRunStatus) -> Self {
65+
self.status = Some(status);
66+
self
67+
}
68+
69+
/// Sends the actual request.
70+
pub async fn send(self) -> Result<models::checks::CheckRun> {
71+
let route = format!(
72+
"/repos/{owner}/{repo}/check-runs",
73+
owner = self.handler.owner,
74+
repo = self.handler.repo
75+
);
76+
self.handler.crab.post(route, Some(&self)).await
77+
}
78+
}
79+
80+
#[derive(serde::Serialize)]
81+
pub struct UpdateCheckRunBuilder<'octo, 'r> {
82+
#[serde(skip)]
83+
handler: &'r ChecksHandler<'octo>,
84+
check_run_id: CheckRunId,
85+
#[serde(skip_serializing_if = "Option::is_none")]
86+
name: Option<String>,
87+
#[serde(skip_serializing_if = "Option::is_none")]
88+
details_url: Option<String>,
89+
#[serde(skip_serializing_if = "Option::is_none")]
90+
external_id: Option<String>,
91+
#[serde(skip_serializing_if = "Option::is_none")]
92+
started_at: Option<DateTime<Utc>>,
93+
#[serde(skip_serializing_if = "Option::is_none")]
94+
status: Option<CheckRunStatus>,
95+
#[serde(skip_serializing_if = "Option::is_none")]
96+
conclusion: Option<String>,
97+
#[serde(skip_serializing_if = "Option::is_none")]
98+
completed_at: Option<DateTime<Utc>>,
99+
#[serde(skip_serializing_if = "Option::is_none")]
100+
output: Option<serde_json::Value>,
101+
}
102+
103+
impl<'octo, 'r> UpdateCheckRunBuilder<'octo, 'r> {
104+
pub(crate) fn new(handler: &'r ChecksHandler<'octo>, check_run_id: CheckRunId) -> Self {
105+
Self {
106+
handler,
107+
check_run_id,
108+
name: None,
109+
details_url: None,
110+
external_id: None,
111+
started_at: None,
112+
status: None,
113+
conclusion: None,
114+
completed_at: None,
115+
output: None,
116+
}
117+
}
118+
119+
/// The name of the check. For example, "code-coverage".
120+
pub fn name(mut self, name: impl Into<String>) -> Self {
121+
self.name = Some(name.into());
122+
self
123+
}
124+
125+
/// The URL of the integrator's site that has the full details of the check.
126+
/// If the integrator does not provide this, then the homepage of the GitHub app is used.
127+
pub fn details_url(mut self, details_url: impl Into<String>) -> Self {
128+
self.details_url = Some(details_url.into());
129+
self
130+
}
131+
132+
/// A reference for the run on the integrator's system.
133+
pub fn external_url(mut self, external_id: impl Into<String>) -> Self {
134+
self.external_id = Some(external_id.into());
135+
self
136+
}
137+
138+
/// The time that the check run began.
139+
pub fn started_at(mut self, started_at: DateTime<Utc>) -> Self {
140+
self.started_at = Some(started_at);
141+
self
142+
}
143+
144+
/// The current status.
145+
/// Can be one of `queued`, `in_progress`, or `completed`.
146+
pub fn status(mut self, status: CheckRunStatus) -> Self {
147+
self.status = Some(status);
148+
self
149+
}
150+
151+
/// The final conclusion of the check.
152+
/// Can be one of `success`, `failure`, `neutral`, `cancelled`, `timed_out`,
153+
/// `skipped`, `stale` or `action_required`.
154+
pub fn conclusion(mut self, conclusion: impl Into<String>) -> Self {
155+
self.conclusion = Some(conclusion.into());
156+
self
157+
}
158+
159+
/// The time that the check run completed.
160+
pub fn completed_at(mut self, completed_at: DateTime<Utc>) -> Self {
161+
self.completed_at = Some(completed_at);
162+
self
163+
}
164+
165+
/// Check runs can accept a variety of data in the output object,
166+
/// including a title and summary and can optionally provide
167+
/// descriptive details about the run.
168+
pub fn output(mut self, output: serde_json::Value) -> Self {
169+
self.output = Some(output);
170+
self
171+
}
172+
173+
/// Sends the actual request.
174+
pub async fn send(self) -> Result<models::checks::CheckRun> {
175+
let route = format!(
176+
"/repos/{owner}/{repo}/check-runs/{check_run_id}",
177+
owner = self.handler.owner,
178+
repo = self.handler.repo,
179+
check_run_id = self.check_run_id
180+
);
181+
self.handler.crab.patch(route, Some(&self)).await
182+
}
183+
}
184+
185+
#[derive(serde::Serialize)]
186+
pub struct ListCheckRunsInCheckSuiteBuilder<'octo, 'r> {
15187
#[serde(skip)]
16188
handler: &'r ChecksHandler<'octo>,
17189
check_suite_id: CheckSuiteId,
@@ -21,7 +193,7 @@ pub struct ListCheckRunsinCheckSuiteBuilder<'octo, 'r> {
21193
page: Option<u32>,
22194
}
23195

24-
impl<'octo, 'r> ListCheckRunsinCheckSuiteBuilder<'octo, 'r> {
196+
impl<'octo, 'r> ListCheckRunsInCheckSuiteBuilder<'octo, 'r> {
25197
pub(crate) fn new(handler: &'r ChecksHandler<'octo>, check_suite_id: CheckSuiteId) -> Self {
26198
Self {
27199
handler,
@@ -56,6 +228,53 @@ impl<'octo, 'r> ListCheckRunsinCheckSuiteBuilder<'octo, 'r> {
56228
}
57229
}
58230

231+
#[derive(serde::Serialize)]
232+
pub struct ListCheckRunsForGitRefBuilder<'octo, 'r> {
233+
#[serde(skip)]
234+
handler: &'r ChecksHandler<'octo>,
235+
#[serde(skip)]
236+
git_ref: Commitish,
237+
#[serde(skip_serializing_if = "Option::is_none")]
238+
per_page: Option<u8>,
239+
#[serde(skip_serializing_if = "Option::is_none")]
240+
page: Option<u32>,
241+
}
242+
243+
impl<'octo, 'r> ListCheckRunsForGitRefBuilder<'octo, 'r> {
244+
pub(crate) fn new(handler: &'r ChecksHandler<'octo>, git_ref: Commitish) -> Self {
245+
Self {
246+
handler,
247+
git_ref,
248+
per_page: None,
249+
page: None,
250+
}
251+
}
252+
253+
/// Results per page (max 100).
254+
pub fn per_page(mut self, per_page: impl Into<u8>) -> Self {
255+
self.per_page = Some(per_page.into());
256+
self
257+
}
258+
259+
/// Page number of the results to fetch.
260+
pub fn page(mut self, page: impl Into<u32>) -> Self {
261+
self.page = Some(page.into());
262+
self
263+
}
264+
265+
/// Send the actual request.
266+
pub async fn send(self) -> Result<models::checks::ListCheckRuns> {
267+
let route = format!(
268+
"/repos/{owner}/{repo}/commits/{ref}/check-runs",
269+
owner = self.handler.owner,
270+
repo = self.handler.repo,
271+
ref = self.git_ref,
272+
);
273+
274+
self.handler.crab.get(route, Some(&self)).await
275+
}
276+
}
277+
59278
impl<'octo> ChecksHandler<'octo> {
60279
pub(crate) fn new(crab: &'octo Octocrab, owner: String, repo: String) -> Self {
61280
Self { crab, owner, repo }
@@ -74,7 +293,63 @@ impl<'octo> ChecksHandler<'octo> {
74293
pub fn list_check_runs_in_a_check_suite(
75294
&self,
76295
suite_id: CheckSuiteId,
77-
) -> ListCheckRunsinCheckSuiteBuilder<'_, '_> {
78-
ListCheckRunsinCheckSuiteBuilder::new(self, suite_id)
296+
) -> ListCheckRunsInCheckSuiteBuilder<'_, '_> {
297+
ListCheckRunsInCheckSuiteBuilder::new(self, suite_id)
298+
}
299+
300+
/// ```no_run
301+
/// # use octocrab::params::repos::Commitish;
302+
/// async fn run() -> octocrab::Result<()> {
303+
/// let check_runs = octocrab::instance()
304+
/// .checks("owner", "repo")
305+
/// .list_check_runs_for_git_ref(Commitish("ref".to_string()))
306+
/// .send()
307+
/// .await?;
308+
/// # Ok(())
309+
/// # }
310+
pub fn list_check_runs_for_git_ref(
311+
&self,
312+
git_ref: Commitish,
313+
) -> ListCheckRunsForGitRefBuilder<'_, '_> {
314+
ListCheckRunsForGitRefBuilder::new(self, git_ref)
315+
}
316+
317+
/// ```no_run
318+
/// # async fn run() -> octocrab::Result<()> {
319+
/// let check_run = octocrab::instance()
320+
/// .checks("owner", "repo")
321+
/// .create_check_run("name", "head_sha")
322+
/// .details_url("https://example.com")
323+
/// .external_id("external_id")
324+
/// .status(octocrab::checks::CheckRunStatus::InProgress)
325+
/// .send()
326+
/// .await?;
327+
/// # Ok(())
328+
/// # }
329+
/// ```
330+
pub fn create_check_run(
331+
&self,
332+
name: impl Into<String>,
333+
head_sha: impl Into<String>,
334+
) -> CreateCheckRunBuilder<'_, '_> {
335+
CreateCheckRunBuilder::new(self, name.into(), head_sha.into())
336+
}
337+
338+
/// ```no_run
339+
/// # async fn run() -> octocrab::Result<()> {
340+
/// let check_run = octocrab::instance()
341+
/// .checks("owner", "repo")
342+
/// .update_check_run(123456.into())
343+
/// .name("name")
344+
/// .details_url("https://example.com")
345+
/// .external_url("external_id")
346+
/// .status(octocrab::checks::CheckRunStatus::InProgress)
347+
/// .send()
348+
/// .await?;
349+
/// # Ok(())
350+
/// # }
351+
/// ```
352+
pub fn update_check_run(&self, check_run_id: CheckRunId) -> UpdateCheckRunBuilder<'_, '_> {
353+
UpdateCheckRunBuilder::new(self, check_run_id)
79354
}
80355
}

0 commit comments

Comments
 (0)