Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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