Skip to content

Instantly share code, notes, and snippets.

@queq1890
Last active January 21, 2021 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save queq1890/aea5f48e4954439ba17c23009eb10104 to your computer and use it in GitHub Desktop.
Save queq1890/aea5f48e4954439ba17c23009eb10104 to your computer and use it in GitHub Desktop.
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