0

Assign Issue

by
Published Nov 2, 2023

Assign an issue to a user, picked from the users assignable to that issue. Leave the user empty to unassign the issue.

Script jira Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 8 days ago
1
//native
2

3
async function get(auth: RT.Jira, path: string) {
4
  const response = await fetch(`https://${auth.domain}.atlassian.net${path}`, {
5
    headers: {
6
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
7
      Accept: "application/json",
8
    },
9
  })
10
  if (!response.ok) {
11
    throw new Error(`${response.status} ${await response.text()}`)
12
  }
13
  return await response.json()
14
}
15

16
export type DynSelect_account_id = string
17

18
// Resolver: the users who can be assigned to the chosen issue (first 100).
19
export async function account_id(auth: RT.Jira, issue_id_or_key: string) {
20
  if (!issue_id_or_key) return []
21
  const users = await get(
22
    auth,
23
    `/rest/api/3/user/assignable/search?issueKey=${issue_id_or_key}&maxResults=100`
24
  )
25
  return users.map((u: { accountId: string; displayName: string }) => ({
26
    value: u.accountId,
27
    label: u.displayName,
28
  }))
29
}
30

31
/**
32
 * Assign Issue
33
 * Assign an issue to a user, picked from the users assignable to that issue. Leave the user empty to unassign the issue.
34
 */
35
export async function main(
36
  auth: RT.Jira,
37
  issue_id_or_key: string,
38
  account_id: DynSelect_account_id | undefined
39
) {
40
  const url = new URL(
41
    `https://${auth.domain}.atlassian.net/rest/api/3/issue/${issue_id_or_key}/assignee`
42
  )
43

44
  const response = await fetch(url, {
45
    method: "PUT",
46
    headers: {
47
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
48
      "Content-Type": "application/json",
49
      Accept: "application/json",
50
    },
51
    body: JSON.stringify({ accountId: account_id || null }),
52
  })
53

54
  if (!response.ok) {
55
    throw new Error(`${response.status} ${await response.text()}`)
56
  }
57

58
  if (response.status === 204) return { success: true }
59
  return await response.json()
60
}
61

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 532 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Assign issue
    8
     * Assigns an issue to a user.
    9
     */
    10
    export async function main(
    11
      auth: Jira,
    12
      issueIdOrKey: string,
    13
      body: {
    14
        accountId?: string;
    15
        accountType?: "atlassian" | "app" | "customer" | "unknown";
    16
        active?: boolean;
    17
        applicationRoles?: {
    18
          callback?: {};
    19
          items?: {
    20
            defaultGroups?: string[];
    21
            defaultGroupsDetails?: {
    22
              groupId?: string;
    23
              name?: string;
    24
              self?: string;
    25
            }[];
    26
            defined?: boolean;
    27
            groupDetails?: { groupId?: string; name?: string; self?: string }[];
    28
            groups?: string[];
    29
            hasUnlimitedSeats?: boolean;
    30
            key?: string;
    31
            name?: string;
    32
            numberOfSeats?: number;
    33
            platform?: boolean;
    34
            remainingSeats?: number;
    35
            selectedByDefault?: boolean;
    36
            userCount?: number;
    37
            userCountDescription?: string;
    38
          }[];
    39
          "max-results"?: number;
    40
          pagingCallback?: {};
    41
          size?: number;
    42
        };
    43
        avatarUrls?: {
    44
          "16x16"?: string;
    45
          "24x24"?: string;
    46
          "32x32"?: string;
    47
          "48x48"?: string;
    48
        };
    49
        displayName?: string;
    50
        emailAddress?: string;
    51
        expand?: string;
    52
        groups?: {
    53
          callback?: {};
    54
          items?: { groupId?: string; name?: string; self?: string }[];
    55
          "max-results"?: number;
    56
          pagingCallback?: {};
    57
          size?: number;
    58
        };
    59
        key?: string;
    60
        locale?: string;
    61
        name?: string;
    62
        self?: string;
    63
        timeZone?: string;
    64
      }
    65
    ) {
    66
      const url = new URL(
    67
        `https://${auth.domain}.atlassian.net/rest/api/2/issue/${issueIdOrKey}/assignee`
    68
      );
    69
    
    
    70
      const response = await fetch(url, {
    71
        method: "PUT",
    72
        headers: {
    73
          "Content-Type": "application/json",
    74
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    75
        },
    76
        body: JSON.stringify(body),
    77
      });
    78
      if (!response.ok) {
    79
        const text = await response.text();
    80
        throw new Error(`${response.status} ${text}`);
    81
      }
    82
      return await response.json();
    83
    }
    84