forked from XAMPPRocky/octocrab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecks.rs
355 lines (319 loc) · 10.8 KB
/
checks.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use crate::models::{CheckRunId, CheckSuiteId};
use crate::params::repos::Commitish;
use crate::{models, Octocrab, Result};
use chrono::{DateTime, Utc};
/// Handler for GitHub's Checks API.
///
/// Created with [`Octocrab::checks`].
pub struct ChecksHandler<'octo> {
crab: &'octo Octocrab,
owner: String,
repo: String,
}
#[derive(serde::Serialize)]
#[serde(rename_all = "snake_case")]
pub enum CheckRunStatus {
Queued,
InProgress,
Completed,
}
#[derive(serde::Serialize)]
pub struct CreateCheckRunBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r ChecksHandler<'octo>,
name: String,
head_sha: String,
#[serde(skip_serializing_if = "Option::is_none")]
details_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
external_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
status: Option<CheckRunStatus>,
}
impl<'octo, 'r> CreateCheckRunBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r ChecksHandler<'octo>, name: String, head_sha: String) -> Self {
Self {
handler,
name,
head_sha,
details_url: None,
external_id: None,
status: None,
}
}
/// The URL of the integrator's site that has the full details of the check.
/// If the integrator does not provide this, then the homepage of the GitHub app is used.
pub fn details_url(mut self, details_url: impl Into<String>) -> Self {
self.details_url = Some(details_url.into());
self
}
/// A reference for the run on the integrator's system.
pub fn external_id(mut self, external_id: impl Into<String>) -> Self {
self.external_id = Some(external_id.into());
self
}
/// The current status.
/// Can be one of `queued`, `in_progress`, or `completed`.
pub fn status(mut self, status: CheckRunStatus) -> Self {
self.status = Some(status);
self
}
/// Sends the actual request.
pub async fn send(self) -> Result<models::checks::CheckRun> {
let route = format!(
"/repos/{owner}/{repo}/check-runs",
owner = self.handler.owner,
repo = self.handler.repo
);
self.handler.crab.post(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
pub struct UpdateCheckRunBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r ChecksHandler<'octo>,
check_run_id: CheckRunId,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
details_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
external_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
started_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
status: Option<CheckRunStatus>,
#[serde(skip_serializing_if = "Option::is_none")]
conclusion: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
completed_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
output: Option<serde_json::Value>,
}
impl<'octo, 'r> UpdateCheckRunBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r ChecksHandler<'octo>, check_run_id: CheckRunId) -> Self {
Self {
handler,
check_run_id,
name: None,
details_url: None,
external_id: None,
started_at: None,
status: None,
conclusion: None,
completed_at: None,
output: None,
}
}
/// The name of the check. For example, "code-coverage".
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
/// The URL of the integrator's site that has the full details of the check.
/// If the integrator does not provide this, then the homepage of the GitHub app is used.
pub fn details_url(mut self, details_url: impl Into<String>) -> Self {
self.details_url = Some(details_url.into());
self
}
/// A reference for the run on the integrator's system.
pub fn external_url(mut self, external_id: impl Into<String>) -> Self {
self.external_id = Some(external_id.into());
self
}
/// The time that the check run began.
pub fn started_at(mut self, started_at: DateTime<Utc>) -> Self {
self.started_at = Some(started_at);
self
}
/// The current status.
/// Can be one of `queued`, `in_progress`, or `completed`.
pub fn status(mut self, status: CheckRunStatus) -> Self {
self.status = Some(status);
self
}
/// The final conclusion of the check.
/// Can be one of `success`, `failure`, `neutral`, `cancelled`, `timed_out`,
/// `skipped`, `stale` or `action_required`.
pub fn conclusion(mut self, conclusion: impl Into<String>) -> Self {
self.conclusion = Some(conclusion.into());
self
}
/// The time that the check run completed.
pub fn completed_at(mut self, completed_at: DateTime<Utc>) -> Self {
self.completed_at = Some(completed_at);
self
}
/// Check runs can accept a variety of data in the output object,
/// including a title and summary and can optionally provide
/// descriptive details about the run.
pub fn output(mut self, output: serde_json::Value) -> Self {
self.output = Some(output);
self
}
/// Sends the actual request.
pub async fn send(self) -> Result<models::checks::CheckRun> {
let route = format!(
"/repos/{owner}/{repo}/check-runs/{check_run_id}",
owner = self.handler.owner,
repo = self.handler.repo,
check_run_id = self.check_run_id
);
self.handler.crab.patch(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
pub struct ListCheckRunsInCheckSuiteBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r ChecksHandler<'octo>,
check_suite_id: CheckSuiteId,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}
impl<'octo, 'r> ListCheckRunsInCheckSuiteBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r ChecksHandler<'octo>, check_suite_id: CheckSuiteId) -> Self {
Self {
handler,
check_suite_id,
per_page: None,
page: None,
}
}
/// Results per page (max 100).
pub fn per_page(mut self, per_page: impl Into<u8>) -> Self {
self.per_page = Some(per_page.into());
self
}
/// Page number of the results to fetch.
pub fn page(mut self, page: impl Into<u32>) -> Self {
self.page = Some(page.into());
self
}
/// Send the actual request.
pub async fn send(self) -> Result<models::checks::ListCheckRuns> {
let route = format!(
"/repos/{owner}/{repo}/check-suites/{check_suite_id}/check-runs",
owner = self.handler.owner,
repo = self.handler.repo,
check_suite_id = self.check_suite_id,
);
self.handler.crab.get(route, Some(&self)).await
}
}
#[derive(serde::Serialize)]
pub struct ListCheckRunsForGitRefBuilder<'octo, 'r> {
#[serde(skip)]
handler: &'r ChecksHandler<'octo>,
#[serde(skip)]
git_ref: Commitish,
#[serde(skip_serializing_if = "Option::is_none")]
per_page: Option<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
page: Option<u32>,
}
impl<'octo, 'r> ListCheckRunsForGitRefBuilder<'octo, 'r> {
pub(crate) fn new(handler: &'r ChecksHandler<'octo>, git_ref: Commitish) -> Self {
Self {
handler,
git_ref,
per_page: None,
page: None,
}
}
/// Results per page (max 100).
pub fn per_page(mut self, per_page: impl Into<u8>) -> Self {
self.per_page = Some(per_page.into());
self
}
/// Page number of the results to fetch.
pub fn page(mut self, page: impl Into<u32>) -> Self {
self.page = Some(page.into());
self
}
/// Send the actual request.
pub async fn send(self) -> Result<models::checks::ListCheckRuns> {
let route = format!(
"/repos/{owner}/{repo}/commits/{ref}/check-runs",
owner = self.handler.owner,
repo = self.handler.repo,
ref = self.git_ref,
);
self.handler.crab.get(route, Some(&self)).await
}
}
impl<'octo> ChecksHandler<'octo> {
pub(crate) fn new(crab: &'octo Octocrab, owner: String, repo: String) -> Self {
Self { crab, owner, repo }
}
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let check_runs = octocrab::instance()
/// .checks("owner", "repo")
/// .list_check_runs_in_a_check_suite(123456.into())
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn list_check_runs_in_a_check_suite(
&self,
suite_id: CheckSuiteId,
) -> ListCheckRunsInCheckSuiteBuilder<'_, '_> {
ListCheckRunsInCheckSuiteBuilder::new(self, suite_id)
}
/// ```no_run
/// # use octocrab::params::repos::Commitish;
/// async fn run() -> octocrab::Result<()> {
/// let check_runs = octocrab::instance()
/// .checks("owner", "repo")
/// .list_check_runs_for_git_ref(Commitish("ref".to_string()))
/// .send()
/// .await?;
/// # Ok(())
/// # }
pub fn list_check_runs_for_git_ref(
&self,
git_ref: Commitish,
) -> ListCheckRunsForGitRefBuilder<'_, '_> {
ListCheckRunsForGitRefBuilder::new(self, git_ref)
}
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let check_run = octocrab::instance()
/// .checks("owner", "repo")
/// .create_check_run("name", "head_sha")
/// .details_url("https://example.com")
/// .external_id("external_id")
/// .status(octocrab::checks::CheckRunStatus::InProgress)
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn create_check_run(
&self,
name: impl Into<String>,
head_sha: impl Into<String>,
) -> CreateCheckRunBuilder<'_, '_> {
CreateCheckRunBuilder::new(self, name.into(), head_sha.into())
}
/// ```no_run
/// # async fn run() -> octocrab::Result<()> {
/// let check_run = octocrab::instance()
/// .checks("owner", "repo")
/// .update_check_run(123456.into())
/// .name("name")
/// .details_url("https://example.com")
/// .external_url("external_id")
/// .status(octocrab::checks::CheckRunStatus::InProgress)
/// .send()
/// .await?;
/// # Ok(())
/// # }
/// ```
pub fn update_check_run(&self, check_run_id: CheckRunId) -> UpdateCheckRunBuilder<'_, '_> {
UpdateCheckRunBuilder::new(self, check_run_id)
}
}