Created
July 15, 2023 07:30
-
-
Save maskaravivek/69d7d0f615dd5874181c2f857d156938 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
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