Create a required workflow

Create a required workflow in an organization. You must authenticate using an access token with the `admin:org` scope to use this endpoint. For more information, see "[Required Workflows](https://docs.github.com/actions/using-workflows/required-workflows)."

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 required workflow
6
 * Create a required workflow in an organization.
7

8
You must authenticate using an access token with the `admin:org` scope to use this endpoint.
9

10
For more information, see "[Required Workflows](https://docs.github.com/actions/using-workflows/required-workflows)."
11
 */
12
export async function main(
13
  auth: Github,
14
  org: string,
15
  body: {
16
    repository_id: string;
17
    scope?: "selected" | "all";
18
    selected_repository_ids?: number[];
19
    workflow_file_path: string;
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(
24
    `https://api.github.com/orgs/${org}/actions/required_workflows`
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