-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.ts
82 lines (72 loc) · 2.46 KB
/
github.ts
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
import { Octokit as Core } from "https://esm.sh/@octokit/[email protected]";
import { Octokit as REST } from "https://esm.sh/@octokit/[email protected]";
import { retry } from "https://esm.sh/@octokit/[email protected]";
import { throttling } from "https://esm.sh/@octokit/[email protected]";
import { GetResponseDataTypeFromEndpointMethod } from "https://esm.sh/@octokit/[email protected]";
const Octokit = REST.plugin(retry, throttling)
export const Endpoints = new REST()
export type Workflow = GetResponseDataTypeFromEndpointMethod<typeof Endpoints.actions.getWorkflowRun>
export type Job = GetResponseDataTypeFromEndpointMethod<typeof Endpoints.actions.getJobForWorkflowRun >
export class Client {
client: REST
constructor(token: string, debug: boolean) {
this.client = new Octokit({
auth: token,
throttle: {
onRateLimit: (
retryAfter: number,
options: {method: string; url: string},
_octokit: Core,
retryCount: number
) => {
if (debug) {
console.log(
`Request quota exhausted for request ${options.method} ${options.url}`
)
}
if (retryCount === 0) {
// only retries once
if (debug) {
console.info(`Retrying after ${retryAfter} seconds!`)
}
return true
}
},
onSecondaryRateLimit: (
retryAfter: number,
options: {method: string; url: string},
_octokit: Core,
retryCount: number
) => {
if (debug) {
console.log(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`
)
}
if (retryCount === 0) {
// only retries once
if (debug) {
console.info(`Retrying after ${retryAfter} seconds!`)
}
return true
}
}
}
})
}
listWorkflowRunsForRepo(owner: string, repo: string, created: string): Promise<Workflow[]> {
return this.client.paginate(this.client.actions.listWorkflowRunsForRepo, {
owner,
repo,
created: `>=${created}`
})
}
async listJobsForWorkflowRunAttempt(owner: string, repo: string, _created: string, run_id: number, attempt_number: number): Promise<Job[]> {
return (await this.client.paginate(this.client.actions.listJobsForWorkflowRunAttempt, {
owner,
repo,
run_id,
attempt_number
})).jobs
}
}