Update a repository webhook

Updates a webhook configured in a repository. If you previously had a `secret` set, you must provide the same `secret` or set a new `secret` or the secret will be removed. If you are only updating individual webhook `config` properties, use "Update a webhook configuration for a repository."

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
 * Update a repository webhook
6
 * Updates a webhook configured in a repository. If you previously had a `secret` set, you must provide the same `secret` or set a new `secret` or the secret will be removed. If you are only updating individual webhook `config` properties, use "Update a webhook configuration for a repository."
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  hook_id: string,
13
  body: {
14
    active?: boolean;
15
    add_events?: string[];
16
    config?: {
17
      address?: string;
18
      content_type?: string;
19
      insecure_ssl?: string | number;
20
      room?: string;
21
      secret?: string;
22
      url: string;
23
      [k: string]: unknown;
24
    };
25
    events?: string[];
26
    remove_events?: string[];
27
    [k: string]: unknown;
28
  }
29
) {
30
  const url = new URL(
31
    `https://api.github.com/repos/${owner}/${repo}/hooks/${hook_id}`
32
  );
33

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