List workflow runs for a required workflow

List all workflow runs for a required workflow.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 367 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List workflow runs for a required workflow
6
 * List all workflow runs for a required workflow.
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  required_workflow_id_for_repo: string,
13
  actor: string | undefined,
14
  branch: string | undefined,
15
  event: string | undefined,
16
  status:
17
    | "completed"
18
    | "action_required"
19
    | "cancelled"
20
    | "failure"
21
    | "neutral"
22
    | "skipped"
23
    | "stale"
24
    | "success"
25
    | "timed_out"
26
    | "in_progress"
27
    | "queued"
28
    | "requested"
29
    | "waiting"
30
    | "pending"
31
    | undefined,
32
  per_page: string | undefined,
33
  page: string | undefined,
34
  created: string | undefined,
35
  exclude_pull_requests: string | undefined,
36
  check_suite_id: string | undefined,
37
  head_sha: string | undefined
38
) {
39
  const url = new URL(
40
    `https://api.github.com/repos/${owner}/${repo}/actions/required_workflows/${required_workflow_id_for_repo}/runs`
41
  );
42
  for (const [k, v] of [
43
    ["actor", actor],
44
    ["branch", branch],
45
    ["event", event],
46
    ["status", status],
47
    ["per_page", per_page],
48
    ["page", page],
49
    ["created", created],
50
    ["exclude_pull_requests", exclude_pull_requests],
51
    ["check_suite_id", check_suite_id],
52
    ["head_sha", head_sha],
53
  ]) {
54
    if (v !== undefined && v !== "") {
55
      url.searchParams.append(k, v);
56
    }
57
  }
58
  const response = await fetch(url, {
59
    method: "GET",
60
    headers: {
61
      Authorization: "Bearer " + auth.token,
62
    },
63
    body: undefined,
64
  });
65
  if (!response.ok) {
66
    const text = await response.text();
67
    throw new Error(`${response.status} ${text}`);
68
  }
69
  return await response.json();
70
}
71