0
List public repositories
One script reply has been approved by the moderators Verified

Returns a paginated list of all public repositories.

This endpoint also supports filtering and sorting of the results. See filtering and sorting for more details.

Created by hugo697 179 days ago Viewed 4195 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 179 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * List public repositories
7
 * Returns a paginated list of all public repositories.
8

9
This endpoint also supports filtering and sorting of the results. See
10
filtering and sorting for more details.
11
 */
12
export async function main(
13
  auth: Bitbucket,
14
  after: string | undefined,
15
  role: "admin" | "contributor" | "member" | "owner" | undefined,
16
  q: string | undefined,
17
  sort: string | undefined
18
) {
19
  const url = new URL(`https://api.bitbucket.org/2.0/repositories`);
20
  for (const [k, v] of [
21
    ["after", after],
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