Create a deployment status

Users with `push` access can create deployment statuses for a given deployment. GitHub Apps require `read & write` access to "Deployments" and `read-only` access to "Repo contents" (for private repos). OAuth Apps require the `repo_deployment` scope.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create a deployment status
6
 * Users with `push` access can create deployment statuses for a given deployment.
7

8
GitHub Apps require `read & write` access to "Deployments" and `read-only` access to "Repo contents" (for private repos). OAuth Apps require the `repo_deployment` scope.
9
 */
10
export async function main(
11
  auth: Github,
12
  owner: string,
13
  repo: string,
14
  deployment_id: string,
15
  body: {
16
    auto_inactive?: boolean;
17
    description?: string;
18
    environment?: "production" | "staging" | "qa";
19
    environment_url?: string;
20
    log_url?: string;
21
    state:
22
      | "error"
23
      | "failure"
24
      | "inactive"
25
      | "in_progress"
26
      | "queued"
27
      | "pending"
28
      | "success";
29
    target_url?: string;
30
    [k: string]: unknown;
31
  }
32
) {
33
  const url = new URL(
34
    `https://api.github.com/repos/${owner}/${repo}/deployments/${deployment_id}/statuses`
35
  );
36

37
  const response = await fetch(url, {
38
    method: "POST",
39
    headers: {
40
      "Content-Type": "application/json",
41
      Authorization: "Bearer " + auth.token,
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51