Edits history of script submission #23377 for ' Get All Projects (jira)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Get All Projects
     * List the projects visible to the user, optionally filtered by a name or key fragment. Paginated with `start_at` / `max_results`; `expand` accepts a comma-separated list of `description`, `issueTypes`, `lead`, `projectKeys`, `insight`.
     */
    export async function main(
      auth: RT.Jira,
      query: string | undefined,
      expand: string | undefined,
      start_at: number | undefined,
      max_results: number | undefined
    ) {
      const url = new URL(
        `https://${auth.domain}.atlassian.net/rest/api/3/project/search`
      )
      if (query != null && query !== "") {
        url.searchParams.append("query", query)
      }
      if (expand != null && expand !== "") {
        url.searchParams.append("expand", expand)
      }
      if (start_at != null) {
        url.searchParams.append("startAt", String(start_at))
      }
      if (max_results != null) {
        url.searchParams.append("maxResults", String(max_results))
      }
    
      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