Update a webhook configuration for a repository

Updates the webhook configuration for a repository. To update more information about the webhook, including the `active` state and `events`, use "Update a repository webhook." Access tokens must have the `write:repo_hook` or `repo` scope, and GitHub Apps must have the `repository_hooks:write` permission.

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 webhook configuration for a repository
6
 * Updates the webhook configuration for a repository. To update more information about the webhook, including the `active` state and `events`, use "Update a repository webhook."
7

8
Access tokens must have the `write:repo_hook` or `repo` scope, and GitHub Apps must have the `repository_hooks:write` permission.
9
 */
10
export async function main(
11
  auth: Github,
12
  owner: string,
13
  repo: string,
14
  hook_id: string,
15
  body: {
16
    content_type?: string;
17
    insecure_ssl?: string | number;
18
    secret?: string;
19
    url?: string;
20
  }
21
) {
22
  const url = new URL(
23
    `https://api.github.com/repos/${owner}/${repo}/hooks/${hook_id}/config`
24
  );
25

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