0
List repositories in a workspace
One script reply has been approved by the moderators Verified

Returns a paginated list of all repositories owned by the specified workspace.

The result can be narrowed down based on the authenticated user's role.

E.g. with ?role=contributor, only those repositories that the authenticated user has write access to are returned (this includes any repo the user is an admin on, as that implies write access).

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

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

10
The result can be narrowed down based on the authenticated user's role.
11

12
E.g. with `?role=contributor`, only those repositories that the
13
authenticated user has write access to are returned (this includes any
14
repo the user is an admin on, as that implies write access).
15

16
This endpoint also supports filtering and sorting of the results. See
17
filtering and sorting for more details.
18
 */
19
export async function main(
20
  auth: Bitbucket,
21
  workspace: string,
22
  role: "admin" | "contributor" | "member" | "owner" | undefined,
23
  q: string | undefined,
24
  sort: string | undefined
25
) {
26
  const url = new URL(
27
    `https://api.bitbucket.org/2.0/repositories/${workspace}`
28
  );
29
  for (const [k, v] of [
30
    ["role", role],
31
    ["q", q],
32
    ["sort", sort],
33
  ]) {
34
    if (v !== undefined && v !== "") {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
42
    },
43
    body: undefined,
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51