Create issue field option

Creates an option for a select list issue field.

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
 * Create issue field option
8
 * Creates an option for a select list issue field.
9
 */
10
export async function main(
11
  auth: Jira,
12
  fieldKey: string,
13
  body: {
14
    config?: {
15
      attributes?: ("notSelectable" | "defaultValue")[];
16
      scope?: {
17
        global?: { attributes?: ("notSelectable" | "defaultValue")[] };
18
        projects?: number[];
19
        projects2?: {
20
          attributes?: ("notSelectable" | "defaultValue")[];
21
          id?: number;
22
        }[];
23
      };
24
    };
25
    properties?: { [k: string]: unknown };
26
    value: string;
27
    [k: string]: unknown;
28
  }
29
) {
30
  const url = new URL(
31
    `https://${auth.domain}.atlassian.net/rest/api/2/field/${fieldKey}/option`
32
  );
33

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