Edits history of script submission #23221 for ' List Project Statuses (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 Statuses
     * List the options of a project's Status field (e.g. Todo, In Progress, Done) with their ids. Classic tokens and OAuth connections need the read:project scope.
     */
    export async function main(
      auth: RT.Github,
      owner: string,
      project_number: DynSelect_project_number
    ) {
      const data = await graphql(
        auth,
        `
          query ($owner: String!, $number: Int!) {
            repositoryOwner(login: $owner) {
              ... on ProjectV2Owner {
                projectV2(number: $number) {
                  field(name: "Status") {
                    ... on ProjectV2SingleSelectField {
                      id
                      options {
                        id
                        name
                        color
                        description
                      }
                    }
                  }
                }
              }
            }
          }
        `,
        { owner, number: Number(project_number) }
      )
      const field = data.repositoryOwner?.projectV2?.field
      if (!field) throw new Error(`Project ${project_number} has no Status field`)
      return field.options
    }
    

    Submitted by hugo989 2 hours ago