0

Add Watcher to Issue

by
Published Nov 2, 2023

Add a user to an issue's watcher list by account ID (find one with Get Users).

Script jira Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
/**
4
 * Add Watcher to Issue
5
 * Add a user to an issue's watcher list by account ID (find one with Get Users).
6
 */
7
export async function main(
8
  auth: RT.Jira,
9
  issue_id_or_key: string,
10
  account_id: string
11
) {
12
  const url = new URL(
13
    `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}/watchers`
14
  )
15

16
  const response = await fetch(url, {
17
    method: "POST",
18
    headers: {
19
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
20
      "Content-Type": "application/json",
21
      Accept: "application/json",
22
    },
23
    body: JSON.stringify(account_id),
24
  })
25

26
  if (!response.ok) {
27
    throw new Error(`${response.status} ${await response.text()}`)
28
  }
29

30
  if (response.status === 204) return { success: true }
31
  return await response.json()
32
}
33

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 525 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Add watcher
    8
     * Adds a user as a watcher of an issue by passing the account ID of the user.
    9
     */
    10
    export async function main(auth: Jira, issueIdOrKey: string, body: string) {
    11
      const url = new URL(
    12
        `https://${auth.domain}.atlassian.net/rest/api/2/issue/${issueIdOrKey}/watchers`
    13
      );
    14
    
    
    15
      const response = await fetch(url, {
    16
        method: "POST",
    17
        headers: {
    18
          "Content-Type": "application/json",
    19
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    20
        },
    21
        body: JSON.stringify(body),
    22
      });
    23
      if (!response.ok) {
    24
        const text = await response.text();
    25
        throw new Error(`${response.status} ${text}`);
    26
      }
    27
      return await response.json();
    28
    }
    29