Edits history of script submission #23222 for ' List Projects (github)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    async function graphql(auth: RT.Github, query: string, variables: object) {
      const response = await fetch("https://api.github.com/graphql", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify({ query, variables }),
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { data, errors } = (await response.json()) as {
        data: any
        errors?: unknown[]
      }
      if (errors?.length) throw new Error(JSON.stringify(errors))
      return data
    }
    
    /**
     * List Projects
     * List the GitHub Projects (v2) of a user or organization. Paginate with after = pageInfo.endCursor. Classic tokens and OAuth connections need the read:project scope.
     */
    export async function main(
      auth: RT.Github,
      owner: string,
      first: number = 30,
      after: string | undefined
    ) {
      const data = await graphql(
        auth,
        `
          query ($owner: String!, $first: Int!, $after: String) {
            repositoryOwner(login: $owner) {
              ... on ProjectV2Owner {
                projectsV2(first: $first, after: $after) {
                  nodes {
                    id
                    number
                    title
                    shortDescription
                    url
                    closed
                    public
                    updatedAt
                  }
                  pageInfo {
                    endCursor
                    hasNextPage
                  }
                }
              }
            }
          }
        `,
        { owner, first, after: after || null }
      )
      if (!data.repositoryOwner) throw new Error(`Owner ${owner} not found`)
      return data.repositoryOwner.projectsV2
    }
    

    Submitted by hugo989 2 hours ago