0

Get Issue Types

by
Published Nov 2, 2023

List the issue types of a project, or every issue type the user can see when no project is chosen.

Script jira Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours 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_project_id = string
17

18
// Resolver: the site's projects (first 100).
19
export async function project_id(auth: RT.Jira) {
20
  const { values } = await get(
21
    auth,
22
    "/rest/api/3/project/search?maxResults=100"
23
  )
24
  return values.map((p: { id: string; key: string; name: string }) => ({
25
    value: p.id,
26
    label: `${p.name} (${p.key})`,
27
  }))
28
}
29

30
/**
31
 * Get Issue Types
32
 * List the issue types of a project, or every issue type the user can see when no project is chosen.
33
 */
34
export async function main(
35
  auth: RT.Jira,
36
  project_id: DynSelect_project_id | undefined
37
) {
38
  const url = new URL(
39
    `https://${auth.domain}.atlassian.net/rest/api/3/issuetype`
40
  )
41
  if (project_id != null && project_id !== "") {
42
    url.pathname += "/project"
43
    url.searchParams.append("projectId", project_id)
44
  }
45

46
  const response = await fetch(url, {
47
    method: "GET",
48
    headers: {
49
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
50
      Accept: "application/json",
51
    },
52
  })
53

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

58
  return await response.json()
59
}
60

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 525 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Get issue types for project
    8
     * Returns issue types for a project.
    9
    
    
    10
    This operation can be accessed anonymously.
    11
    
    
    12
    **[Permissions](#permissions) required:** *Browse projects* [project permission](https://confluence.atlassian.com/x/yodKLg) in the relevant project or *Administer Jira* [global permission](https://confluence.atlassian.com/x/x4dKLg).
    13
     */
    14
    export async function main(
    15
      auth: Jira,
    16
      projectId: string | undefined,
    17
      level: string | undefined
    18
    ) {
    19
      const url = new URL(
    20
        `https://${auth.domain}.atlassian.net/rest/api/2/issuetype/project`
    21
      );
    22
      for (const [k, v] of [
    23
        ["projectId", projectId],
    24
        ["level", level],
    25
      ]) {
    26
        if (v !== undefined && v !== "") {
    27
          url.searchParams.append(k, v);
    28
        }
    29
      }
    30
      const response = await fetch(url, {
    31
        method: "GET",
    32
        headers: {
    33
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    34
        },
    35
        body: undefined,
    36
      });
    37
      if (!response.ok) {
    38
        const text = await response.text();
    39
        throw new Error(`${response.status} ${text}`);
    40
      }
    41
      return await response.json();
    42
    }
    43