0
List commit statuses for a commit
One script reply has been approved by the moderators Verified

Returns all statuses (e.g. build results) for a specific commit.

Created by hugo697 645 days ago Viewed 22515 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 645 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * List commit statuses for a commit
7
 * Returns all statuses (e.g. build results) for a specific commit.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  commit: string,
12
  repo_slug: string,
13
  workspace: string,
14
  q: string | undefined,
15
  sort: string | undefined
16
) {
17
  const url = new URL(
18
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/commit/${commit}/statuses`
19
  );
20
  for (const [k, v] of [
21
    ["q", q],
22
    ["sort", sort],
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