Delete a branch

Delete a branch in the specified repository. The main branch is not allowed to be deleted and will return a 400 response. The branch name should not include any prefixes (e.g. refs/heads).

Script bitbucket Verified

by hugo697 ยท 10/24/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 375 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Delete a branch
7
 * Delete a branch in the specified repository.
8

9
The main branch is not allowed to be deleted and will return a 400
10
response.
11

12
The branch name should not include any prefixes (e.g.
13
refs/heads).
14
 */
15
export async function main(
16
  auth: Bitbucket,
17
  name: string,
18
  repo_slug: string,
19
  workspace: string
20
) {
21
  const url = new URL(
22
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/refs/branches/${name}`
23
  );
24

25
  const response = await fetch(url, {
26
    method: "DELETE",
27
    headers: {
28
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
29
    },
30
    body: undefined,
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.text();
37
}
38