Last active
January 21, 2021 09:11
-
-
Save queq1890/aea5f48e4954439ba17c23009eb10104 to your computer and use it in GitHub Desktop.
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; | |
fileName: string; | |
markDown: string; | |
pullRequest: { | |
title: string; | |
body: string; | |
}; | |
commitMessage?: string; | |
}; | |
const octokit = new Octokit({ | |
auth: process.env.GITHUB_TOKEN, | |
}); | |
const main = async ({ | |
owner, | |
repo, | |
baseBranch, | |
newBranch, | |
fileName, | |
markDown, | |
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 newTree = await octokit.git.createTree({ | |
owner, | |
repo, | |
base_tree: currentCommit.data.tree.sha, | |
tree: [ | |
{ | |
path: `contents/${fileName}.mdx`, | |
mode: '100644', | |
content: markDown, | |
}, | |
], | |
}); | |
const newCommit = await octokit.git.createCommit({ | |
owner, | |
repo, | |
message: commitMessage, | |
tree: newTree.data.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