Get the combined status for a specific reference

Users with pull access in a repository can access a combined view of commit statuses for a given ref. The ref can be a SHA, a branch name, or a tag name. Additionally, a combined `state` is returned. The `state` is one of: * **failure** if any of the contexts report as `error` or `failure` * **pending** if there are no statuses or a context is `pending` * **success** if the latest status for all contexts is `success`

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
 * Get the combined status for a specific reference
6
 * Users with pull access in a repository can access a combined view of commit statuses for a given ref. The ref can be a SHA, a branch name, or a tag name.
7

8

9
Additionally, a combined `state` is returned. The `state` is one of:
10

11
*   **failure** if any of the contexts report as `error` or `failure`
12
*   **pending** if there are no statuses or a context is `pending`
13
*   **success** if the latest status for all contexts is `success`
14
 */
15
export async function main(
16
  auth: Github,
17
  owner: string,
18
  repo: string,
19
  ref: string,
20
  per_page: string | undefined,
21
  page: string | undefined
22
) {
23
  const url = new URL(
24
    `https://api.github.com/repos/${owner}/${repo}/commits/${ref}/status`
25
  );
26
  for (const [k, v] of [
27
    ["per_page", per_page],
28
    ["page", page],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      Authorization: "Bearer " + auth.token,
38
    },
39
    body: undefined,
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47