Update issue field option
One script reply has been approved by the moderators Verified

Updates or creates an option for a select list issue field.

Created by hugo697 879 days ago
Submitted by hugo697 Typescript (fetch-only)
Verified 327 days ago
1
type Jira = {
2
  username: string;
3
  password: string;
4
  domain: string;
5
};
6
/**
7
 * Update issue field option
8
 * Updates or creates an option for a select list issue field.
9
 */
10
export async function main(
11
  auth: Jira,
12
  fieldKey: string,
13
  optionId: string,
14
  body: {
15
    config?: {
16
      attributes?: ("notSelectable" | "defaultValue")[];
17
      scope?: {
18
        global?: { attributes?: ("notSelectable" | "defaultValue")[] };
19
        projects?: number[];
20
        projects2?: {
21
          attributes?: ("notSelectable" | "defaultValue")[];
22
          id?: number;
23
        }[];
24
      };
25
    };
26
    id: number;
27
    properties?: { [k: string]: unknown };
28
    value: string;
29
  }
30
) {
31
  const url = new URL(
32
    `https://${auth.domain}.atlassian.net/rest/api/2/field/${fieldKey}/option/${optionId}`
33
  );
34

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