Create an organization webhook

Here's how you can create a hook that posts payloads in JSON format:

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 an organization webhook
6
 * Here's how you can create a hook that posts payloads in JSON format:
7
 */
8
export async function main(
9
  auth: Github,
10
  org: string,
11
  body: {
12
    active?: boolean;
13
    config: {
14
      content_type?: string;
15
      insecure_ssl?: string | number;
16
      password?: string;
17
      secret?: string;
18
      url: string;
19
      username?: string;
20
      [k: string]: unknown;
21
    };
22
    events?: string[];
23
    name: string;
24
    [k: string]: unknown;
25
  }
26
) {
27
  const url = new URL(`https://api.github.com/orgs/${org}/hooks`);
28

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