List commit statuses for a pull request

Returns all statuses (e.g. build results) for the given pull request.

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