0

List Branches

by
Published today

List a repository's branches. Paginate with page.

Script github Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
/**
4
 * List Branches
5
 * List a repository's branches. Paginate with page.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  owner: string,
10
  repo: string,
11
  protected_only: boolean | undefined,
12
  per_page: number = 30,
13
  page: number = 1
14
) {
15
  const url = new URL(`https://api.github.com/repos/${owner}/${repo}/branches`)
16
  for (const [k, v] of Object.entries({
17
    protected: protected_only,
18
    per_page,
19
    page,
20
  })) {
21
    if (v !== undefined) url.searchParams.append(k, String(v))
22
  }
23

24
  const response = await fetch(url, {
25
    headers: {
26
      Authorization: `Bearer ${auth.token}`,
27
      Accept: "application/vnd.github+json",
28
      "X-GitHub-Api-Version": "2022-11-28",
29
    },
30
  })
31

32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35

36
  return await response.json()
37
}
38