Create a commit status

Users with push access in a repository can create commit statuses for a given SHA. Note: there is a limit of 1000 statuses per `sha` and `context` within a repository. Attempts to create more than 1000 statuses will result in a validation error.

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
 * Create a commit status
6
 * Users with push access in a repository can create commit statuses for a given SHA.
7

8
Note: there is a limit of 1000 statuses per `sha` and `context` within a repository. Attempts to create more than 1000 statuses will result in a validation error.
9
 */
10
export async function main(
11
  auth: Github,
12
  owner: string,
13
  repo: string,
14
  sha: string,
15
  body: {
16
    context?: string;
17
    description?: string;
18
    state: "error" | "failure" | "pending" | "success";
19
    target_url?: string;
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(
24
    `https://api.github.com/repos/${owner}/${repo}/statuses/${sha}`
25
  );
26

27
  const response = await fetch(url, {
28
    method: "POST",
29
    headers: {
30
      "Content-Type": "application/json",
31
      Authorization: "Bearer " + auth.token,
32
    },
33
    body: JSON.stringify(body),
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41