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

Returns a list of workspaces accessible by the authenticated user.

Results may be further filtered or sorted by workspace or permission by adding the following query string parameters:

  • q=slug="bbworkspace1" or q=is_private=true
  • sort=created_on

Note that the query parameter values need to be URL escaped so that = would become %3D.

The collaborator role is being removed from the Bitbucket Cloud API. For more information, see the deprecation announcement.

Created by hugo697 198 days ago Viewed 5906 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 198 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * List workspaces for user
7
 * Returns a list of workspaces accessible by the authenticated user.
8

9
Results may be further filtered or sorted by
10
workspace or permission by adding the following query string parameters:
11

12
* `q=slug="bbworkspace1"` or `q=is_private=true`
13
* `sort=created_on`
14

15
Note that the query parameter values need to be URL escaped so that `=`
16
would become `%3D`.
17

18
**The `collaborator` role is being removed from the Bitbucket Cloud API. For more information,
19
see the deprecation announcement.**
20
 */
21
export async function main(
22
  auth: Bitbucket,
23
  role: "owner" | "collaborator" | "member" | undefined,
24
  q: string | undefined,
25
  sort: string | undefined
26
) {
27
  const url = new URL(`https://api.bitbucket.org/2.0/workspaces`);
28
  for (const [k, v] of [
29
    ["role", role],
30
    ["q", q],
31
    ["sort", sort],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "GET",
39
    headers: {
40
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
41
    },
42
    body: undefined,
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.json();
49
}
50