0
List open branches
One script reply has been approved by the moderators Verified

Returns a list of all open branches within the specified repository.

Created by hugo697 172 days ago Viewed 3620 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 open branches
7
 * Returns a list of all open branches within the specified repository.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  repo_slug: string,
12
  workspace: string,
13
  q: string | undefined,
14
  sort: string | undefined
15
) {
16
  const url = new URL(
17
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/refs/branches`
18
  );
19
  for (const [k, v] of [
20
    ["q", q],
21
    ["sort", sort],
22
  ]) {
23
    if (v !== undefined && v !== "") {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "GET",
29
    headers: {
30
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
31
    },
32
    body: undefined,
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40