0

Merge Pull Request

by
Published 4 days ago

Merge a pull request with a merge commit, squash or rebase. Set sha to make the merge fail if the head moved.

Script github Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 days ago
1
//native
2

3
/**
4
 * Merge Pull Request
5
 * Merge a pull request with a merge commit, squash or rebase. Set sha to make the merge fail if the head moved.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  owner: string,
10
  repo: string,
11
  pull_number: number,
12
  merge_method: "merge" | "squash" | "rebase" | undefined,
13
  commit_title: string | undefined,
14
  commit_message: string | undefined,
15
  sha: string | undefined
16
) {
17
  const response = await fetch(
18
    `https://api.github.com/repos/${owner}/${repo}/pulls/${pull_number}/merge`,
19
    {
20
      method: "PUT",
21
      headers: {
22
        Authorization: `Bearer ${auth.token}`,
23
        "Content-Type": "application/json",
24
        Accept: "application/vnd.github+json",
25
        "X-GitHub-Api-Version": "2022-11-28",
26
      },
27
      body: JSON.stringify({
28
        merge_method: merge_method || undefined,
29
        commit_title: commit_title || undefined,
30
        commit_message: commit_message || undefined,
31
        sha: sha || undefined,
32
      }),
33
    }
34
  )
35

36
  if (!response.ok) {
37
    throw new Error(`${response.status} ${await response.text()}`)
38
  }
39

40
  return await response.json()
41
}
42