1 | |
2 |
|
3 | export type DynSelect_workflow_id = string |
4 |
|
5 | |
6 | export async function workflow_id( |
7 | auth: RT.Github, |
8 | owner: string, |
9 | repo: string |
10 | ) { |
11 | if (!auth?.token || !owner || !repo) return [] |
12 | const response = await fetch( |
13 | `https://api.github.com/repos/${owner}/${repo}/actions/workflows?per_page=100`, |
14 | { |
15 | headers: { |
16 | Authorization: `Bearer ${auth.token}`, |
17 | Accept: "application/vnd.github+json", |
18 | "X-GitHub-Api-Version": "2022-11-28", |
19 | }, |
20 | } |
21 | ) |
22 | if (!response.ok) { |
23 | throw new Error(`${response.status} ${await response.text()}`) |
24 | } |
25 | const { workflows } = (await response.json()) as { |
26 | workflows: { id: number; name: string; path: string }[] |
27 | } |
28 | return workflows.map((w) => ({ |
29 | value: String(w.id), |
30 | label: `${w.name} (${w.path})`, |
31 | })) |
32 | } |
33 |
|
34 | |
35 | * List Workflow Runs |
36 | * List GitHub Actions runs of a repository, or of one workflow when workflow_id is set, filtered by branch, event or status. Paginate with page. |
37 | */ |
38 | export async function main( |
39 | auth: RT.Github, |
40 | owner: string, |
41 | repo: string, |
42 | workflow_id: DynSelect_workflow_id | undefined, |
43 | branch: string | undefined, |
44 | event: string | undefined, |
45 | status: |
46 | | "completed" |
47 | | "action_required" |
48 | | "cancelled" |
49 | | "failure" |
50 | | "neutral" |
51 | | "skipped" |
52 | | "stale" |
53 | | "success" |
54 | | "timed_out" |
55 | | "in_progress" |
56 | | "queued" |
57 | | "requested" |
58 | | "waiting" |
59 | | "pending" |
60 | | undefined, |
61 | per_page: number = 30, |
62 | page: number = 1 |
63 | ) { |
64 | const base = `https://api.github.com/repos/${owner}/${repo}/actions` |
65 | const url = new URL( |
66 | workflow_id ? `${base}/workflows/${workflow_id}/runs` : `${base}/runs` |
67 | ) |
68 | for (const [k, v] of Object.entries({ |
69 | branch, |
70 | event, |
71 | status, |
72 | per_page, |
73 | page, |
74 | })) { |
75 | if (v !== undefined && v !== "") url.searchParams.append(k, String(v)) |
76 | } |
77 |
|
78 | const response = await fetch(url, { |
79 | headers: { |
80 | Authorization: `Bearer ${auth.token}`, |
81 | Accept: "application/vnd.github+json", |
82 | "X-GitHub-Api-Version": "2022-11-28", |
83 | }, |
84 | }) |
85 |
|
86 | if (!response.ok) { |
87 | throw new Error(`${response.status} ${await response.text()}`) |
88 | } |
89 |
|
90 | return await response.json() |
91 | } |
92 |
|