0

Sync Fork Branch with Upstream

by
Published today

Merge the upstream repository's changes into a branch of a fork (fast-forward or merge commit).

Script github Verified

The script

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

3
/**
4
 * Sync Fork Branch with Upstream
5
 * Merge the upstream repository's changes into a branch of a fork (fast-forward or merge commit).
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  owner: string,
10
  repo: string,
11
  branch: string
12
) {
13
  const response = await fetch(
14
    `https://api.github.com/repos/${owner}/${repo}/merge-upstream`,
15
    {
16
      method: "POST",
17
      headers: {
18
        Authorization: `Bearer ${auth.token}`,
19
        "Content-Type": "application/json",
20
        Accept: "application/vnd.github+json",
21
        "X-GitHub-Api-Version": "2022-11-28",
22
      },
23
      body: JSON.stringify({ branch }),
24
    }
25
  )
26

27
  if (!response.ok) {
28
    throw new Error(`${response.status} ${await response.text()}`)
29
  }
30

31
  return await response.json()
32
}
33