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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#![allow(unused_imports)]
#![cfg_attr(rustfmt, rustfmt_skip)]
/* THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT */
use crate::{Client, ClientBuilder, Credentials, Retry};
use anyhow::Error;
use serde_json::Value;
use std::time::Duration;
use crate::util::urlencode;

/// GitHub Service
///
/// The github service is responsible for creating tasks in response
/// to GitHub events, and posting results to the GitHub UI.
///
/// This document describes the API end-point for consuming GitHub
/// web hooks, as well as some useful consumer APIs.
///
/// When Github forbids an action, this service returns an HTTP 403
/// with code ForbiddenByGithub.
pub struct Github {
    /// The underlying client used to make API calls for this service.
    pub client: Client
}

#[allow(non_snake_case)]
impl Github {
    /// Create a new Github instance, based on the given client builder
    pub fn new<CB: Into<ClientBuilder>>(client_builder: CB) -> Result<Self, Error> {
        Ok(Self{
            client: client_builder
                .into()
                .path_prefix("api/github/v1/")
                .build()?,
        })
    }

    /// Ping Server
    ///
    /// Respond without doing anything.
    /// This endpoint is used to check that the service is up.
    pub async fn ping(&self) -> Result<(), Error> {
        let method = "GET";
        let (path, query) = Self::ping_details();
        let body = None;
        let resp = self.client.request(method, path, query, body).await?;
        resp.bytes().await?;
        Ok(())
    }

    /// Generate an unsigned URL for the ping endpoint
    pub fn ping_url(&self) -> Result<String, Error> {
        let (path, query) = Self::ping_details();
        self.client.make_url(path, query)
    }

    /// Generate a signed URL for the ping endpoint
    pub fn ping_signed_url(&self, ttl: Duration) -> Result<String, Error> {
        let (path, query) = Self::ping_details();
        self.client.make_signed_url(path, query, ttl)
    }

    /// Determine the HTTP request details for ping
    fn ping_details<'a>() -> (&'static str, Option<Vec<(&'static str, &'a str)>>) {
        let path = "ping";
        let query = None;

        (path, query)
    }

    /// Load Balancer Heartbeat
    ///
    /// Respond without doing anything.
    /// This endpoint is used to check that the service is up.
    pub async fn lbheartbeat(&self) -> Result<(), Error> {
        let method = "GET";
        let (path, query) = Self::lbheartbeat_details();
        let body = None;
        let resp = self.client.request(method, path, query, body).await?;
        resp.bytes().await?;
        Ok(())
    }

    /// Generate an unsigned URL for the lbheartbeat endpoint
    pub fn lbheartbeat_url(&self) -> Result<String, Error> {
        let (path, query) = Self::lbheartbeat_details();
        self.client.make_url(path, query)
    }

    /// Generate a signed URL for the lbheartbeat endpoint
    pub fn lbheartbeat_signed_url(&self, ttl: Duration) -> Result<String, Error> {
        let (path, query) = Self::lbheartbeat_details();
        self.client.make_signed_url(path, query, ttl)
    }

    /// Determine the HTTP request details for lbheartbeat
    fn lbheartbeat_details<'a>() -> (&'static str, Option<Vec<(&'static str, &'a str)>>) {
        let path = "__lbheartbeat__";
        let query = None;

        (path, query)
    }

    /// Taskcluster Version
    ///
    /// Respond with the JSON version object.
    /// https://github.com/mozilla-services/Dockerflow/blob/main/docs/version_object.md
    pub async fn version(&self) -> Result<(), Error> {
        let method = "GET";
        let (path, query) = Self::version_details();
        let body = None;
        let resp = self.client.request(method, path, query, body).await?;
        resp.bytes().await?;
        Ok(())
    }

    /// Generate an unsigned URL for the version endpoint
    pub fn version_url(&self) -> Result<String, Error> {
        let (path, query) = Self::version_details();
        self.client.make_url(path, query)
    }

    /// Generate a signed URL for the version endpoint
    pub fn version_signed_url(&self, ttl: Duration) -> Result<String, Error> {
        let (path, query) = Self::version_details();
        self.client.make_signed_url(path, query, ttl)
    }

    /// Determine the HTTP request details for version
    fn version_details<'a>() -> (&'static str, Option<Vec<(&'static str, &'a str)>>) {
        let path = "__version__";
        let query = None;

        (path, query)
    }

    /// Consume GitHub WebHook
    ///
    /// Capture a GitHub event and publish it via pulse, if it's a push,
    /// release, check run or pull request.
    pub async fn githubWebHookConsumer(&self) -> Result<(), Error> {
        let method = "POST";
        let (path, query) = Self::githubWebHookConsumer_details();
        let body = None;
        let resp = self.client.request(method, path, query, body).await?;
        resp.bytes().await?;
        Ok(())
    }

    /// Determine the HTTP request details for githubWebHookConsumer
    fn githubWebHookConsumer_details<'a>() -> (&'static str, Option<Vec<(&'static str, &'a str)>>) {
        let path = "github";
        let query = None;

        (path, query)
    }

    /// List of Builds
    ///
    /// A paginated list of builds that have been run in
    /// Taskcluster. Can be filtered on various git-specific
    /// fields.
    pub async fn builds(&self, continuationToken: Option<&str>, limit: Option<&str>, organization: Option<&str>, repository: Option<&str>, sha: Option<&str>, pullRequest: Option<&str>) -> Result<Value, Error> {
        let method = "GET";
        let (path, query) = Self::builds_details(continuationToken, limit, organization, repository, sha, pullRequest);
        let body = None;
        let resp = self.client.request(method, path, query, body).await?;
        Ok(resp.json().await?)
    }

    /// Generate an unsigned URL for the builds endpoint
    pub fn builds_url(&self, continuationToken: Option<&str>, limit: Option<&str>, organization: Option<&str>, repository: Option<&str>, sha: Option<&str>, pullRequest: Option<&str>) -> Result<String, Error> {
        let (path, query) = Self::builds_details(continuationToken, limit, organization, repository, sha, pullRequest);
        self.client.make_url(path, query)
    }

    /// Generate a signed URL for the builds endpoint
    pub fn builds_signed_url(&self, continuationToken: Option<&str>, limit: Option<&str>, organization: Option<&str>, repository: Option<&str>, sha: Option<&str>, pullRequest: Option<&str>, ttl: Duration) -> Result<String, Error> {
        let (path, query) = Self::builds_details(continuationToken, limit, organization, repository, sha, pullRequest);
        self.client.make_signed_url(path, query, ttl)
    }

    /// Determine the HTTP request details for builds
    fn builds_details<'a>(continuationToken: Option<&'a str>, limit: Option<&'a str>, organization: Option<&'a str>, repository: Option<&'a str>, sha: Option<&'a str>, pullRequest: Option<&'a str>) -> (&'static str, Option<Vec<(&'static str, &'a str)>>) {
        let path = "builds";
        let mut query = None;
        if let Some(q) = continuationToken {
            query.get_or_insert_with(Vec::new).push(("continuationToken", q));
        }
        if let Some(q) = limit {
            query.get_or_insert_with(Vec::new).push(("limit", q));
        }
        if let Some(q) = organization {
            query.get_or_insert_with(Vec::new).push(("organization", q));
        }
        if let Some(q) = repository {
            query.get_or_insert_with(Vec::new).push(("repository", q));
        }
        if let Some(q) = sha {
            query.get_or_insert_with(Vec::new).push(("sha", q));
        }
        if let Some(q) = pullRequest {
            query.get_or_insert_with(Vec::new).push(("pullRequest", q));
        }

        (path, query)
    }

    /// Cancel repository builds
    ///
    /// Cancel all running Task Groups associated with given repository and sha or pullRequest number
    pub async fn cancelBuilds(&self, owner: &str, repo: &str, sha: Option<&str>, pullRequest: Option<&str>) -> Result<Value, Error> {
        let method = "POST";
        let (path, query) = Self::cancelBuilds_details(owner, repo, sha, pullRequest);
        let body = None;
        let resp = self.client.request(method, &path, query, body).await?;
        Ok(resp.json().await?)
    }

    /// Determine the HTTP request details for cancelBuilds
    fn cancelBuilds_details<'a>(owner: &'a str, repo: &'a str, sha: Option<&'a str>, pullRequest: Option<&'a str>) -> (String, Option<Vec<(&'static str, &'a str)>>) {
        let path = format!("builds/{}/{}/cancel", urlencode(owner), urlencode(repo));
        let mut query = None;
        if let Some(q) = sha {
            query.get_or_insert_with(Vec::new).push(("sha", q));
        }
        if let Some(q) = pullRequest {
            query.get_or_insert_with(Vec::new).push(("pullRequest", q));
        }

        (path, query)
    }

    /// Latest Build Status Badge
    ///
    /// Checks the status of the latest build of a given branch
    /// and returns corresponding badge svg.
    pub async fn badge(&self, owner: &str, repo: &str, branch: &str) -> Result<(), Error> {
        let method = "GET";
        let (path, query) = Self::badge_details(owner, repo, branch);
        let body = None;
        let resp = self.client.request(method, &path, query, body).await?;
        resp.bytes().await?;
        Ok(())
    }

    /// Generate an unsigned URL for the badge endpoint
    pub fn badge_url(&self, owner: &str, repo: &str, branch: &str) -> Result<String, Error> {
        let (path, query) = Self::badge_details(owner, repo, branch);
        self.client.make_url(&path, query)
    }

    /// Generate a signed URL for the badge endpoint
    pub fn badge_signed_url(&self, owner: &str, repo: &str, branch: &str, ttl: Duration) -> Result<String, Error> {
        let (path, query) = Self::badge_details(owner, repo, branch);
        self.client.make_signed_url(&path, query, ttl)
    }

    /// Determine the HTTP request details for badge
    fn badge_details<'a>(owner: &'a str, repo: &'a str, branch: &'a str) -> (String, Option<Vec<(&'static str, &'a str)>>) {
        let path = format!("repository/{}/{}/{}/badge.svg", urlencode(owner), urlencode(repo), urlencode(branch));
        let query = None;

        (path, query)
    }

    /// Get Repository Info
    ///
    /// Returns any repository metadata that is
    /// useful within Taskcluster related services.
    pub async fn repository(&self, owner: &str, repo: &str) -> Result<Value, Error> {
        let method = "GET";
        let (path, query) = Self::repository_details(owner, repo);
        let body = None;
        let resp = self.client.request(method, &path, query, body).await?;
        Ok(resp.json().await?)
    }

    /// Generate an unsigned URL for the repository endpoint
    pub fn repository_url(&self, owner: &str, repo: &str) -> Result<String, Error> {
        let (path, query) = Self::repository_details(owner, repo);
        self.client.make_url(&path, query)
    }

    /// Generate a signed URL for the repository endpoint
    pub fn repository_signed_url(&self, owner: &str, repo: &str, ttl: Duration) -> Result<String, Error> {
        let (path, query) = Self::repository_details(owner, repo);
        self.client.make_signed_url(&path, query, ttl)
    }

    /// Determine the HTTP request details for repository
    fn repository_details<'a>(owner: &'a str, repo: &'a str) -> (String, Option<Vec<(&'static str, &'a str)>>) {
        let path = format!("repository/{}/{}", urlencode(owner), urlencode(repo));
        let query = None;

        (path, query)
    }

    /// Latest Status for Branch
    ///
    /// For a given branch of a repository, this will always point
    /// to a status page for the most recent task triggered by that
    /// branch.
    ///
    /// Note: This is a redirect rather than a direct link.
    pub async fn latest(&self, owner: &str, repo: &str, branch: &str) -> Result<(), Error> {
        let method = "GET";
        let (path, query) = Self::latest_details(owner, repo, branch);
        let body = None;
        let resp = self.client.request(method, &path, query, body).await?;
        resp.bytes().await?;
        Ok(())
    }

    /// Generate an unsigned URL for the latest endpoint
    pub fn latest_url(&self, owner: &str, repo: &str, branch: &str) -> Result<String, Error> {
        let (path, query) = Self::latest_details(owner, repo, branch);
        self.client.make_url(&path, query)
    }

    /// Generate a signed URL for the latest endpoint
    pub fn latest_signed_url(&self, owner: &str, repo: &str, branch: &str, ttl: Duration) -> Result<String, Error> {
        let (path, query) = Self::latest_details(owner, repo, branch);
        self.client.make_signed_url(&path, query, ttl)
    }

    /// Determine the HTTP request details for latest
    fn latest_details<'a>(owner: &'a str, repo: &'a str, branch: &'a str) -> (String, Option<Vec<(&'static str, &'a str)>>) {
        let path = format!("repository/{}/{}/{}/latest", urlencode(owner), urlencode(repo), urlencode(branch));
        let query = None;

        (path, query)
    }

    /// Post a status against a given changeset
    ///
    /// For a given changeset (SHA) of a repository, this will attach a "commit status"
    /// on github. These statuses are links displayed next to each revision.
    /// The status is either OK (green check) or FAILURE (red cross),
    /// made of a custom title and link.
    pub async fn createStatus(&self, owner: &str, repo: &str, sha: &str, payload: &Value) -> Result<(), Error> {
        let method = "POST";
        let (path, query) = Self::createStatus_details(owner, repo, sha);
        let body = Some(payload);
        let resp = self.client.request(method, &path, query, body).await?;
        resp.bytes().await?;
        Ok(())
    }

    /// Determine the HTTP request details for createStatus
    fn createStatus_details<'a>(owner: &'a str, repo: &'a str, sha: &'a str) -> (String, Option<Vec<(&'static str, &'a str)>>) {
        let path = format!("repository/{}/{}/statuses/{}", urlencode(owner), urlencode(repo), urlencode(sha));
        let query = None;

        (path, query)
    }

    /// Post a comment on a given GitHub Issue or Pull Request
    ///
    /// For a given Issue or Pull Request of a repository, this will write a new message.
    pub async fn createComment(&self, owner: &str, repo: &str, number: &str, payload: &Value) -> Result<(), Error> {
        let method = "POST";
        let (path, query) = Self::createComment_details(owner, repo, number);
        let body = Some(payload);
        let resp = self.client.request(method, &path, query, body).await?;
        resp.bytes().await?;
        Ok(())
    }

    /// Determine the HTTP request details for createComment
    fn createComment_details<'a>(owner: &'a str, repo: &'a str, number: &'a str) -> (String, Option<Vec<(&'static str, &'a str)>>) {
        let path = format!("repository/{}/{}/issues/{}/comments", urlencode(owner), urlencode(repo), urlencode(number));
        let query = None;

        (path, query)
    }

    /// Render .taskcluster.yml file
    ///
    /// This endpoint allows to render the .taskcluster.yml file for a given event or payload.
    /// This is useful to preview the result of the .taskcluster.yml file before pushing it to
    /// the repository.
    /// Read more about the .taskcluster.yml file in the [documentation](https://docs.taskcluster.net/docs/reference/integrations/github/taskcluster-yml-v1)
    pub async fn renderTaskclusterYml(&self, payload: &Value) -> Result<Value, Error> {
        let method = "POST";
        let (path, query) = Self::renderTaskclusterYml_details();
        let body = Some(payload);
        let resp = self.client.request(method, path, query, body).await?;
        Ok(resp.json().await?)
    }

    /// Determine the HTTP request details for renderTaskclusterYml
    fn renderTaskclusterYml_details<'a>() -> (&'static str, Option<Vec<(&'static str, &'a str)>>) {
        let path = "taskcluster-yml";
        let query = None;

        (path, query)
    }

    /// Heartbeat
    ///
    /// Respond with a service heartbeat.
    ///
    /// This endpoint is used to check on backing services this service
    /// depends on.
    pub async fn heartbeat(&self) -> Result<(), Error> {
        let method = "GET";
        let (path, query) = Self::heartbeat_details();
        let body = None;
        let resp = self.client.request(method, path, query, body).await?;
        resp.bytes().await?;
        Ok(())
    }

    /// Generate an unsigned URL for the heartbeat endpoint
    pub fn heartbeat_url(&self) -> Result<String, Error> {
        let (path, query) = Self::heartbeat_details();
        self.client.make_url(path, query)
    }

    /// Generate a signed URL for the heartbeat endpoint
    pub fn heartbeat_signed_url(&self, ttl: Duration) -> Result<String, Error> {
        let (path, query) = Self::heartbeat_details();
        self.client.make_signed_url(path, query, ttl)
    }

    /// Determine the HTTP request details for heartbeat
    fn heartbeat_details<'a>() -> (&'static str, Option<Vec<(&'static str, &'a str)>>) {
        let path = "__heartbeat__";
        let query = None;

        (path, query)
    }
}