Edits history of script submission #23220 for ' List Project Items (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
    }
    
    export type DynSelect_project_number = string
    
    // Dropdown of the owner's projects; fill owner first.
    export async function project_number(auth: RT.Github, owner: string) {
      if (!auth?.token || !owner) return []
      const data = await graphql(
        auth,
        `
          query ($owner: String!) {
            repositoryOwner(login: $owner) {
              ... on ProjectV2Owner {
                projectsV2(first: 100) {
                  nodes {
                    number
                    title
                  }
                }
              }
            }
          }
        `,
        { owner }
      )
      return (data.repositoryOwner?.projectsV2?.nodes ?? []).map((p: any) => ({
        value: String(p.number),
        label: `#${p.number} ${p.title}`,
      }))
    }
    
    /**
     * List Project Items
     * List the items (issues, pull requests, draft issues) of a project with their Status. Paginate with after = pageInfo.endCursor. Classic tokens and OAuth connections need the read:project scope.
     */
    export async function main(
      auth: RT.Github,
      owner: string,
      project_number: DynSelect_project_number,
      first: number = 50,
      after: string | undefined
    ) {
      const data = await graphql(
        auth,
        `
          query ($owner: String!, $number: Int!, $first: Int!, $after: String) {
            repositoryOwner(login: $owner) {
              ... on ProjectV2Owner {
                projectV2(number: $number) {
                  items(first: $first, after: $after) {
                    nodes {
                      id
                      type
                      isArchived
                      status: fieldValueByName(name: "Status") {
                        ... on ProjectV2ItemFieldSingleSelectValue {
                          name
                          optionId
                        }
                      }
                      content {
                        ... on Issue {
                          number
                          title
                          state
                          url
                          repository {
                            nameWithOwner
                          }
                        }
                        ... on PullRequest {
                          number
                          title
                          state
                          url
                          repository {
                            nameWithOwner
                          }
                        }
                        ... on DraftIssue {
                          title
                        }
                      }
                    }
                    pageInfo {
                      endCursor
                      hasNextPage
                    }
                  }
                }
              }
            }
          }
        `,
        { owner, number: Number(project_number), first, after: after || null }
      )
      const project = data.repositoryOwner?.projectV2
      if (!project)
        throw new Error(`Project ${project_number} not found for ${owner}`)
      return project.items
    }
    

    Submitted by hugo989 2 hours ago