Last active
May 27, 2023 01:55
-
-
Save queq1890/125e74e1fa33397d2508a6ca4badac83 to your computer and use it in GitHub Desktop.
Create a new pull request with empty commit using @octokit/rest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Octokit } from '@octokit/rest'; | |
type Option = { | |
owner: string; | |
repo: string; | |
baseBranch: string; | |
newBranch: string; | |
pullRequest: { | |
title: string; | |
body: string; | |
}; | |
commitMessage?: string; | |
}; | |
const octokit = new Octokit({ | |
auth: process.env.GITHUB_TOKEN, | |
}); | |
const main = async ({ | |
owner, | |
repo, | |
baseBranch, | |
newBranch, | |
pullRequest: { body, title }, | |
commitMessage = 'WIP', | |
}: Option) => { | |
const baseBranchRef = await octokit.git.getRef({ | |
owner, | |
repo, | |
ref: `heads/${baseBranch}`, | |
}); | |
const newBranchRef = await octokit.git.createRef({ | |
owner, | |
repo, | |
ref: `refs/heads/${newBranch}`, | |
sha: baseBranchRef.data.object.sha, | |
}); | |
const currentCommit = await octokit.git.getCommit({ | |
owner, | |
repo, | |
commit_sha: newBranchRef.data.object.sha, | |
}); | |
const newCommit = await octokit.git.createCommit({ | |
owner, | |
repo, | |
message: commitMessage, | |
tree: currentCommit.data.tree.sha, | |
parents: [currentCommit.data.sha], | |
}); | |
await octokit.git.updateRef({ | |
owner, | |
repo, | |
ref: `heads/${newBranch}`, | |
sha: newCommit.data.sha, | |
}); | |
await octokit.pulls.create({ | |
owner, | |
repo, | |
head: newBranch, | |
base: baseBranch, | |
title, | |
body, | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment