Create custom field options (context)

Creates options and, where the custom select field is of the type Select List (cascading), cascading options for a custom select 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 custom field options (context)
8
 * Creates options and, where the custom select field is of the type Select List (cascading), cascading options for a custom select field.
9
 */
10
export async function main(
11
  auth: Jira,
12
  fieldId: string,
13
  contextId: string,
14
  body: { options?: { disabled?: boolean; optionId?: string; value: string }[] }
15
) {
16
  const url = new URL(
17
    `https://${auth.domain}.atlassian.net/rest/api/2/field/${fieldId}/context/${contextId}/option`
18
  );
19

20
  const response = await fetch(url, {
21
    method: "POST",
22
    headers: {
23
      "Content-Type": "application/json",
24
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
25
    },
26
    body: JSON.stringify(body),
27
  });
28
  if (!response.ok) {
29
    const text = await response.text();
30
    throw new Error(`${response.status} ${text}`);
31
  }
32
  return await response.json();
33
}
34