//native
/**
* Create Branch
* Create a branch from the head of from_branch (the repository's default branch when empty).
*/
export async function main(
auth: RT.Github,
owner: string,
repo: string,
branch: string,
from_branch: string | undefined
) {
const headers = {
Authorization: `Bearer ${auth.token}`,
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
const base = `https://api.github.com/repos/${owner}/${repo}`
if (!from_branch) {
const r = await fetch(base, { headers })
if (!r.ok) throw new Error(`${r.status} ${await r.text()}`)
from_branch = ((await r.json()) as { default_branch: string })
.default_branch
}
const ref = await fetch(`${base}/git/ref/heads/${from_branch}`, { headers })
if (!ref.ok) throw new Error(`${ref.status} ${await ref.text()}`)
const { object } = (await ref.json()) as { object: { sha: string } }
const response = await fetch(`${base}/git/refs`, {
method: "POST",
headers: { ...headers, "Content-Type": "application/json" },
body: JSON.stringify({ ref: `refs/heads/${branch}`, sha: object.sha }),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 2 hours ago