Register dynamic webhooks

Registers webhooks. **NOTE:** for non-public OAuth apps, webhooks are delivered only if there is a match between the app owner and the user who registered a dynamic webhook. **[Permissions](#permissions) required:** Only [Connect](https://developer.atlassian.com/cloud/jira/platform/#connect-apps) and [OAuth 2.0](https://developer.atlassian.com/cloud/jira/platform/oauth-2-3lo-apps) apps can use this operation.

Script jira Verified

by hugo697 ยท 11/2/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Jira = {
2
  username: string;
3
  password: string;
4
  domain: string;
5
};
6
/**
7
 * Register dynamic webhooks
8
 * Registers webhooks.
9

10
**NOTE:** for non-public OAuth apps, webhooks are delivered only if there is a match between the app owner and the user who registered a dynamic webhook.
11

12
**[Permissions](#permissions) required:** Only [Connect](https://developer.atlassian.com/cloud/jira/platform/#connect-apps) and [OAuth 2.0](https://developer.atlassian.com/cloud/jira/platform/oauth-2-3lo-apps) apps can use this operation.
13
 */
14
export async function main(
15
  auth: Jira,
16
  body: {
17
    url: string;
18
    webhooks: {
19
      events: (
20
        | "jira:issue_created"
21
        | "jira:issue_updated"
22
        | "jira:issue_deleted"
23
        | "comment_created"
24
        | "comment_updated"
25
        | "comment_deleted"
26
        | "issue_property_set"
27
        | "issue_property_deleted"
28
      )[];
29
      fieldIdsFilter?: string[];
30
      issuePropertyKeysFilter?: string[];
31
      jqlFilter: string;
32
    }[];
33
  }
34
) {
35
  const url = new URL(
36
    `https://${auth.domain}.atlassian.net/rest/api/2/webhook`
37
  );
38

39
  const response = await fetch(url, {
40
    method: "POST",
41
    headers: {
42
      "Content-Type": "application/json",
43
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
44
    },
45
    body: JSON.stringify(body),
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53