Edits history of script submission #23212 for ' List Branches (github)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Branches
     * List a repository's branches. Paginate with page.
     */
    export async function main(
      auth: RT.Github,
      owner: string,
      repo: string,
      protected_only: boolean | undefined,
      per_page: number = 30,
      page: number = 1
    ) {
      const url = new URL(`https://api.github.com/repos/${owner}/${repo}/branches`)
      for (const [k, v] of Object.entries({
        protected: protected_only,
        per_page,
        page,
      })) {
        if (v !== undefined) 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