Edits history of script submission #23375 for ' Get Issue Types (jira)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    async function get(auth: RT.Jira, path: string) {
      const response = await fetch(`https://${auth.domain}.atlassian.net${path}`, {
        headers: {
          Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      return await response.json()
    }
    
    export type DynSelect_project_id = string
    
    // Resolver: the site's projects (first 100).
    export async function project_id(auth: RT.Jira) {
      const { values } = await get(
        auth,
        "/rest/api/3/project/search?maxResults=100"
      )
      return values.map((p: { id: string; key: string; name: string }) => ({
        value: p.id,
        label: `${p.name} (${p.key})`,
      }))
    }
    
    /**
     * Get Issue Types
     * List the issue types of a project, or every issue type the user can see when no project is chosen.
     */
    export async function main(
      auth: RT.Jira,
      project_id: DynSelect_project_id | undefined
    ) {
      const url = new URL(
        `https://${auth.domain}.atlassian.net/rest/api/3/issuetype`
      )
      if (project_id != null && project_id !== "") {
        url.pathname += "/project"
        url.searchParams.append("projectId", project_id)
      }
    
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 hours ago