Created
July 15, 2023 07:30
-
-
Save maskaravivek/a477c2c98651bdfbda5b99a81b261c37 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 createGithubFileBlob = async (githubAccessToken, repoFullName, content, encoding = "utf-8") => { | |
| const blobResp = await fetch(`https://api.github.com/repos/${repoFullName}/git/blobs`, | |
| { | |
| method: 'POST', | |
| headers: { | |
| 'Accept': 'application/vnd.github+json', | |
| 'Authorization': `Bearer ${githubAccessToken}`, | |
| 'X-GitHub-Api-Version': '2022-11-28' | |
| }, | |
| body: JSON.stringify({ | |
| "content": content, | |
| "encoding": encoding | |
| }) | |
| }) | |
| const response = await blobResp.json() | |
| return response.sha | |
| } | |
| const getShaForBaseTree = async (githubAccessToken, repoFullName, branchName) => { | |
| const baseTreeResp = await fetch(`https://api.github.com/repos/${repoFullName}/git/trees/${branchName}`, | |
| { | |
| method: 'GET', | |
| headers: { | |
| 'Accept': 'application/vnd.github+json', | |
| 'Authorization': `Bearer ${githubAccessToken}`, | |
| 'X-GitHub-Api-Version': '2022-11-28' | |
| }, | |
| }) | |
| const response = await baseTreeResp.json() | |
| return response.sha | |
| } | |
| const getParentSha = async (githubAccessToken, repoFullName, branchName) => { | |
| const parentResp = await fetch(`https://api.github.com/repos/${repoFullName}/git/refs/heads/${branchName}`, | |
| { | |
| method: 'GET', | |
| headers: { | |
| 'Accept': 'application/vnd.github+json', | |
| 'Authorization': `Bearer ${githubAccessToken}`, | |
| 'X-GitHub-Api-Version': '2022-11-28' | |
| }, | |
| }) | |
| const response = await parentResp.json() | |
| return response.object.sha | |
| } | |
| 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 | |
| } | |
| const createGithubCommit = async (githubAccessToken, | |
| repoFullName, | |
| branchName, | |
| commitMessage, | |
| articleFiles) => { | |
| const tree = await createGithubRepoTree(githubAccessToken, repoFullName, branchName, articleFiles) | |
| const parentSha = await getParentSha(githubAccessToken, repoFullName, branchName) | |
| const payload = { | |
| "message": commitMessage, | |
| "tree": tree, | |
| "parents": [parentSha] | |
| } | |
| const response = await fetch(`https://api.github.com/repos/${repoFullName}/git/commits`, | |
| { | |
| method: 'POST', | |
| headers: { | |
| 'Accept': 'application/vnd.github+json', | |
| 'Authorization': `Bearer ${githubAccessToken}`, | |
| 'X-GitHub-Api-Version': '2022-11-28' | |
| }, | |
| body: JSON.stringify(payload) | |
| }) | |
| const commitResp = await response.json() | |
| const commitSha = commitResp.sha | |
| await updateGithubBranchRef(githubAccessToken, repoFullName, branchName, commitSha) | |
| } | |
| const updateGithubBranchRef = async (githubAccessToken, repoFullName, branchName, commitSha) => { | |
| const payload = { | |
| "sha": commitSha, | |
| "force": false | |
| } | |
| const response = await fetch(`https://api.github.com/repos/${repoFullName}/git/refs/heads/${branchName}`, | |
| { | |
| method: 'PATCH', | |
| headers: { | |
| 'Accept': 'application/vnd.github+json', | |
| 'Authorization': `Bearer ${githubAccessToken}`, | |
| 'X-GitHub-Api-Version': '2022-11-28' | |
| }, | |
| body: JSON.stringify(payload) | |
| }) | |
| const commitResp = await response.json() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment