0
List workspaces for the current user
One script reply has been approved by the moderators Verified

Returns an object for each workspace the caller is a member of, and their effective role - the highest level of privilege the caller has.

Created by hugo697 172 days ago Viewed 3622 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 172 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * List workspaces for the current user
7
 * Returns an object for each workspace the caller is a member of, and
8
their effective role - the highest level of privilege the caller has.
9
 */
10
export async function main(
11
  auth: Bitbucket,
12
  q: string | undefined,
13
  sort: string | undefined
14
) {
15
  const url = new URL(
16
    `https://api.bitbucket.org/2.0/user/permissions/workspaces`
17
  );
18
  for (const [k, v] of [
19
    ["q", q],
20
    ["sort", sort],
21
  ]) {
22
    if (v !== undefined && v !== "") {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "GET",
28
    headers: {
29
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
30
    },
31
    body: undefined,
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39