0
Create a build status for a commit
One script reply has been approved by the moderators Verified

Creates a new build status against the specified commit.

Created by hugo697 126 days ago Viewed 25 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 126 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Create a build status for a commit
7
 * Creates a new build status against the specified commit.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  commit: string,
12
  repo_slug: string,
13
  workspace: string,
14
  body: { type: string; [k: string]: unknown } & {
15
    links?: {
16
      self?: { href?: string; name?: string };
17
      commit?: { href?: string; name?: string };
18
    };
19
    uuid?: string;
20
    key?: string;
21
    refname?: string;
22
    url?: string;
23
    state?: "STOPPED" | "FAILED" | "INPROGRESS" | "SUCCESSFUL";
24
    name?: string;
25
    description?: string;
26
    created_on?: string;
27
    updated_on?: string;
28
    [k: string]: unknown;
29
  }
30
) {
31
  const url = new URL(
32
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/commit/${commit}/statuses/build`
33
  );
34

35
  const response = await fetch(url, {
36
    method: "POST",
37
    headers: {
38
      "Content-Type": "application/json",
39
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49