List repository forks

Returns a paginated list of all the forks of the specified repository.

Script bitbucket Verified

by hugo697 ยท 10/24/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 375 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * List repository forks
7
 * Returns a paginated list of all the forks of the specified
8
repository.
9
 */
10
export async function main(
11
  auth: Bitbucket,
12
  repo_slug: string,
13
  workspace: string,
14
  role: "admin" | "contributor" | "member" | "owner" | undefined,
15
  q: string | undefined,
16
  sort: string | undefined
17
) {
18
  const url = new URL(
19
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/forks`
20
  );
21
  for (const [k, v] of [
22
    ["role", role],
23
    ["q", q],
24
    ["sort", sort],
25
  ]) {
26
    if (v !== undefined && v !== "") {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43