List branch restrictions

Returns a paginated list of all branch restrictions on the repository.

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