Edits history of script submission #23226 for ' List Repository Collaborators (github)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Repository Collaborators
     * List the collaborators of a repository, including organization members with access. Requires push access to the repository.
     */
    export async function main(
      auth: RT.Github,
      owner: string,
      repo: string,
      affiliation: "outside" | "direct" | "all" | undefined,
      permission: "pull" | "triage" | "push" | "maintain" | "admin" | undefined,
      per_page: number = 30,
      page: number = 1
    ) {
      const url = new URL(
        `https://api.github.com/repos/${owner}/${repo}/collaborators`
      )
      for (const [k, v] of Object.entries({
        affiliation,
        permission,
        per_page,
        page,
      })) {
        if (v !== undefined && v !== "") url.searchParams.append(k, String(v))
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/vnd.github+json",
          "X-GitHub-Api-Version": "2022-11-28",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago