List jobs for a workflow run

Lists jobs for a workflow run. Anyone with read access to the repository can use this endpoint. If the repository is private you must use an access token with the `repo` scope. GitHub Apps must have the `actions:read` permission to use this endpoint. You can use parameters to narrow the list of results. For more information about using parameters, see [Parameters](https://docs.github.com/rest/overview/resources-in-the-rest-api#parameters).

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 jobs for a workflow run
6
 * Lists jobs for a workflow run. Anyone with read access to the repository can use this endpoint. If the repository is private you must use an access token with the `repo` scope. GitHub Apps must have the `actions:read` permission to use this endpoint. You can use parameters to narrow the list of results. For more information about using parameters, see [Parameters](https://docs.github.com/rest/overview/resources-in-the-rest-api#parameters).
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  run_id: string,
13
  filter: "latest" | "all" | undefined,
14
  per_page: string | undefined,
15
  page: string | undefined
16
) {
17
  const url = new URL(
18
    `https://api.github.com/repos/${owner}/${repo}/actions/runs/${run_id}/jobs`
19
  );
20
  for (const [k, v] of [
21
    ["filter", filter],
22
    ["per_page", per_page],
23
    ["page", page],
24
  ]) {
25
    if (v !== undefined && v !== "") {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42