0
Get a branch
One script reply has been approved by the moderators Verified

Returns a branch object within the specified repository.

This call requires authentication. Private repositories require the caller to authenticate with an account that has appropriate authorization.

For Git, the branch name should not include any prefixes (e.g. refs/heads).

Created by hugo697 198 days ago Viewed 5925 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 198 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Get a branch
7
 * Returns a branch object within the specified repository.
8

9
This call requires authentication. Private repositories require the
10
caller to authenticate with an account that has appropriate
11
authorization.
12

13
For Git, the branch name should not include any prefixes (e.g.
14
refs/heads).
15
 */
16
export async function main(
17
  auth: Bitbucket,
18
  name: string,
19
  repo_slug: string,
20
  workspace: string
21
) {
22
  const url = new URL(
23
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/refs/branches/${name}`
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