Merge a pull request

Merges the 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
 * Merge a pull request
7
 * Merges the pull request.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  pull_request_id: string,
12
  repo_slug: string,
13
  workspace: string,
14
  async: string | undefined,
15
  body: {
16
    type: string;
17
    message?: string;
18
    close_source_branch?: boolean;
19
    merge_strategy?: "merge_commit" | "squash" | "fast_forward";
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(
24
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/pullrequests/${pull_request_id}/merge`
25
  );
26
  for (const [k, v] of [["async", async]]) {
27
    if (v !== undefined && v !== "") {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "POST",
33
    headers: {
34
      "Content-Type": "application/json",
35
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
36
    },
37
    body: JSON.stringify(body),
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45