Edits history of script submission #23005 for ' List Issues (linear)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_team_id = string
    
    // Dropdown of the workspace's teams.
    export async function team_id(auth: RT.Linear) {
      const response = await fetch("https://api.linear.app/graphql", {
        method: "POST",
        headers: {
          Authorization: auth.apiKey,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({
          query: "{ teams(first: 250) { nodes { id key name } } }",
        }),
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { data } = (await response.json()) as {
        data: { teams: { nodes: { id: string; key: string; name: string }[] } }
      }
      return data.teams.nodes.map((t) => ({
        value: t.id,
        label: `${t.name} (${t.key})`,
      }))
    }
    
    /**
     * List Issues
     * List issues, optionally filtered by team, project, workflow state, assignee or label names. Paginate with after = pageInfo.endCursor.
     */
    export async function main(
      auth: RT.Linear,
      team_id: DynSelect_team_id | undefined,
      project_id: string | undefined,
      state_id: string | undefined,
      assignee_id: string | undefined,
      label_names: string[] | undefined,
      order_by: "createdAt" | "updatedAt" = "updatedAt",
      include_archived: boolean = false,
      first: number = 50,
      after: string | undefined
    ) {
      const filter: { [key: string]: any } = {}
      if (team_id) filter.team = { id: { eq: team_id } }
      if (project_id) filter.project = { id: { eq: project_id } }
      if (state_id) filter.state = { id: { eq: state_id } }
      if (assignee_id) filter.assignee = { id: { eq: assignee_id } }
      if (label_names?.length)
        filter.labels = { some: { name: { in: label_names } } }
    
      const response = await fetch("https://api.linear.app/graphql", {
        method: "POST",
        headers: {
          Authorization: auth.apiKey,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({
          query: `query ($filter: IssueFilter, $orderBy: PaginationOrderBy, $includeArchived: Boolean, $first: Int, $after: String) { issues(filter: $filter, orderBy: $orderBy, includeArchived: $includeArchived, first: $first, after: $after) { nodes { id identifier title description url priority priorityLabel estimate dueDate createdAt updatedAt archivedAt state { id name type } assignee { id name email } team { id key name } project { id name } cycle { id number } parent { id identifier } labels { nodes { id name } } } pageInfo { hasNextPage endCursor } } }`,
          variables: {
            filter,
            orderBy: order_by,
            includeArchived: include_archived,
            first,
            after: after || undefined,
          },
        }),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      const { data, errors } = await response.json()
      if (errors) {
        throw new Error(JSON.stringify(errors))
      }
    
      return data.issues
    }
    

    Submitted by hugo989 1 hour ago