Send notification for issue

Creates an email notification for an issue and adds it to the mail queue. **[Permissions](#permissions) required:** * *Browse Projects* [project permission](https://confluence.atlassian.com/x/yodKLg) for the project that the issue is in. * If [issue-level security](https://confluence.atlassian.com/x/J4lKLg) is configured, issue-level security permission to view the issue.

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
 * Send notification for issue
8
 * Creates an email notification for an issue and adds it to the mail queue.
9

10
**[Permissions](#permissions) required:**
11

12
 *  *Browse Projects* [project permission](https://confluence.atlassian.com/x/yodKLg) for the project that the issue is in.
13
 *  If [issue-level security](https://confluence.atlassian.com/x/J4lKLg) is configured, issue-level security permission to view the issue.
14
 */
15
export async function main(
16
  auth: Jira,
17
  issueIdOrKey: string,
18
  body: {
19
    htmlBody?: string;
20
    restrict?: {
21
      groupIds?: string[];
22
      groups?: { groupId?: string; name?: string; self?: string }[];
23
      permissions?: { id?: string; key?: string; [k: string]: unknown }[];
24
    };
25
    subject?: string;
26
    textBody?: string;
27
    to?: {
28
      assignee?: boolean;
29
      groupIds?: string[];
30
      groups?: { groupId?: string; name?: string; self?: string }[];
31
      reporter?: boolean;
32
      users?: {
33
        accountId?: string;
34
        accountType?: string;
35
        active?: boolean;
36
        avatarUrls?: {
37
          "16x16"?: string;
38
          "24x24"?: string;
39
          "32x32"?: string;
40
          "48x48"?: string;
41
        };
42
        displayName?: string;
43
        emailAddress?: string;
44
        key?: string;
45
        name?: string;
46
        self?: string;
47
        timeZone?: string;
48
      }[];
49
      voters?: boolean;
50
      watchers?: boolean;
51
      [k: string]: unknown;
52
    };
53
    [k: string]: unknown;
54
  }
55
) {
56
  const url = new URL(
57
    `https://${auth.domain}.atlassian.net/rest/api/2/issue/${issueIdOrKey}/notify`
58
  );
59

60
  const response = await fetch(url, {
61
    method: "POST",
62
    headers: {
63
      "Content-Type": "application/json",
64
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
65
    },
66
    body: JSON.stringify(body),
67
  });
68
  if (!response.ok) {
69
    const text = await response.text();
70
    throw new Error(`${response.status} ${text}`);
71
  }
72
  return await response.json();
73
}
74