0

Remove custom field context from projects

by
Published Nov 2, 2023

Removes a custom field context from projects. A custom field context without any projects applies to all projects. Removing all projects from a custom field context would result in it applying to all projects. If any project in the request is not assigned to the context, or the operation would result in two global contexts for the field, the operation fails. **[Permissions](#permissions) required:** *Administer Jira* [global permission](https://confluence.atlassian.com/x/x4dKLg).

Script jira Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 416 days ago
1
type Jira = {
2
  username: string;
3
  password: string;
4
  domain: string;
5
};
6
/**
7
 * Remove custom field context from projects
8
 * Removes a custom field context from projects.
9

10
A custom field context without any projects applies to all projects. Removing all projects from a custom field context would result in it applying to all projects.
11

12
If any project in the request is not assigned to the context, or the operation would result in two global contexts for the field, the operation fails.
13

14
**[Permissions](#permissions) required:** *Administer Jira* [global permission](https://confluence.atlassian.com/x/x4dKLg).
15
 */
16
export async function main(
17
  auth: Jira,
18
  fieldId: string,
19
  contextId: string,
20
  body: { projectIds: string[] }
21
) {
22
  const url = new URL(
23
    `https://${auth.domain}.atlassian.net/rest/api/2/field/${fieldId}/context/${contextId}/project/remove`
24
  );
25

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