Create or update an environment

Create or update an environment with protection rules, such as required reviewers.

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 or update an environment
6
 * Create or update an environment with protection rules, such as required reviewers.
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  environment_name: string,
13
  body: {
14
    deployment_branch_policy?: {
15
      custom_branch_policies: boolean;
16
      protected_branches: boolean;
17
      [k: string]: unknown;
18
    };
19
    reviewers?: { id?: number; type?: "User" | "Team"; [k: string]: unknown }[];
20
    wait_timer?: number;
21
  }
22
) {
23
  const url = new URL(
24
    `https://api.github.com/repos/${owner}/${repo}/environments/${environment_name}`
25
  );
26

27
  const response = await fetch(url, {
28
    method: "PUT",
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