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

Returns the branches and tags in the repository.

Created by hugo697 174 days ago Viewed 3744 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 174 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * List branches and tags
7
 * Returns the branches and tags in the 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`
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