Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maskaravivek/69d7d0f615dd5874181c2f857d156938 to your computer and use it in GitHub Desktop.
Save maskaravivek/69d7d0f615dd5874181c2f857d156938 to your computer and use it in GitHub Desktop.
const createGithubRepoTree = async (
githubAccessToken,
repoFullName,
branchName,
articleFiles
) => {
const shaForBaseTree = await getShaForBaseTree(
githubAccessToken,
repoFullName,
branchName
);
const tree = [];
for (var i = 0; i < articleFiles.length; i++) {
const fileSha = await createGithubFileBlob(
githubAccessToken,
repoFullName,
articleFiles[i].content,
articleFiles[i].encoding
);
tree.push({
path: articleFiles[i].path.substring(1),
mode: "100644",
type: "blob",
sha: fileSha,
});
}
const payload = {
base_tree: shaForBaseTree,
tree: tree,
};
const treeResp = await fetch(
`https://api.github.com/repos/${repoFullName}/git/trees`,
{
method: "POST",
headers: {
Accept: "application/vnd.github+json",
Authorization: `Bearer ${githubAccessToken}`,
"X-GitHub-Api-Version": "2022-11-28",
},
body: JSON.stringify(payload),
}
);
const response = await treeResp.json();
return response.sha;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment