From 539c7861d432bfec3a549f9330e7a06783aae19f Mon Sep 17 00:00:00 2001 From: Shakar Bakr <5h4k4r.b4kr@gmail.com> Date: Wed, 4 Oct 2023 15:04:54 +0300 Subject: [PATCH] Use oktokit Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com> --- index.js | 167 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 106 insertions(+), 61 deletions(-) diff --git a/index.js b/index.js index c9c5b60..188701d 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const convert = require('xml-js'); run(); async function run() { + try { console.log(`CWD: ${process.cwd()}`) console.log(`filePath: ${core.getInput('filePath')}`) @@ -82,6 +83,11 @@ async function commitChanges(file, filePath) { // Get the current branch const branch = process.env.GITHUB_REF.replace('refs/heads/', ''); + const oktokit = new Oktokit({ + auth: githubToken + }) + + console.log(`https://api.github.com/repos/${owner}/${repo}/branches/${branch}`, `https://api.github.com/repos/${owner}/${repo}/git/blobs`, `https://api.github.com/repos/${owner}/${repo}/git/trees`, @@ -89,87 +95,126 @@ async function commitChanges(file, filePath) { `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`) try { // Get the current commit SHA for the branch - const branchResponse = await axios.get( - `https://api.github.com/repos/${owner}/${repo}/branches/${branch}` - ); + const branchResponse = await oktokit.request(`GET /repos/${owner}/${repo}/branches/${branch}`, { + owner, + repo, + branch + }) + // const branchResponse = await axios.get( + // `https://api.github.com/repos/${owner}/${repo}/branches/${branch}` + // ); const baseTreeSha = branchResponse.data.commit.sha; // Create a new blob with the updated content console.log('Branch') - const blobResponse = await axios.post( - `https://api.github.com/repos/${owner}/${repo}/git/blobs`, - { - content: newContent, - encoding: 'utf-8', - }, - { - headers: { - 'Accept': 'application/vnd.github.v3+json', - 'Authorization': `Bearer ${githubToken}`, - }, - } - ); + + const blobResponse = await oktokit.request(`POST /repos/${owner}/${repo}/git/blobs`, { + owner, + repo, + content: newContent, + encoding: 'utf-8' + }) + // const blobResponse = await axios.post( + // `https://api.github.com/repos/${owner}/${repo}/git/blobs`, + // { + // content: newContent, + // encoding: 'utf-8', + // }, + // { + // headers: { + // 'Accept': 'application/vnd.github.v3+json', + // 'Authorization': `Bearer ${githubToken}`, + // }, + // } + // ); console.log('Blob Created') console.log('File path: ', filePath) const newBlobSha = blobResponse.data.sha; // Create a new tree with the updated blob - const treeResponse = await axios.post( - `https://api.github.com/repos/${owner}/${repo}/git/trees`, - { - base_tree: baseTreeSha, - tree: [ - { - path: filePath, - mode: '100644', - type: 'blob', - sha: newBlobSha, - }, - ], - }, - { - headers: { - 'Accept': 'application/vnd.github+json', - 'Authorization': `Bearer ${githubToken}` + const treeResponse = await oktokit.request(`POST /repos/${owner}/${repo}/git/trees`, { + owner, + repo, + base_tree: baseTreeSha, + tree: [ + { + path: filePath, + mode: '100644', + type: 'blob', + sha: newBlobSha, }, - } - ); + ] + }) + // const treeResponse = await axios.post( + // `https://api.github.com/repos/${owner}/${repo}/git/trees`, + // { + // base_tree: baseTreeSha, + // tree: [ + // { + // path: filePath, + // mode: '100644', + // type: 'blob', + // sha: newBlobSha, + // }, + // ], + // }, + // { + // headers: { + // 'Accept': 'application/vnd.github+json', + // 'Authorization': `Bearer ${githubToken}` + // }, + // } + // ); console.log('Tree Created') const newTreeSha = treeResponse.data.sha; // Create a new commit - const commitResponse = await axios.post( - `https://api.github.com/repos/${owner}/${repo}/git/commits`, - { - message: commitMessage, - tree: newTreeSha, - parents: [baseTreeSha], - }, - { - headers: { - 'Accept': 'application/vnd.github+json', - 'Authorization': `Bearer ${githubToken}`, - }, - } - ); + const commitResponse = await oktokit.request(`POST /repos/${owner}/${repo}/git/commits`, { + owner, + repo, + message: commitMessage, + tree: newTreeSha, + parents: [baseTreeSha], + }) + + // const commitResponse = await axios.post( + // `https://api.github.com/repos/${owner}/${repo}/git/commits`, + // { + // message: commitMessage, + // tree: newTreeSha, + // parents: [baseTreeSha], + // }, + // { + // headers: { + // 'Accept': 'application/vnd.github+json', + // 'Authorization': `Bearer ${githubToken}`, + // }, + // } + // ); console.log('Commit Created') const newCommitSha = commitResponse.data.sha; // Update the branch reference - await axios.patch( - `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`, - { - sha: newCommitSha, - }, - { - headers: { - 'Accept': 'application/vnd.github+json', - 'Authorization': `Bearer ${githubToken}`, - }, - } - ); + await oktokit.request(`PATCH /repos/${owner}/${repo}/git/refs/heads/${branch}`, { + owner, + repo, + sha: newCommitSha + }) + + // await axios.patch( + // `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`, + // { + // sha: newCommitSha, + // }, + // { + // headers: { + // 'Accept': 'application/vnd.github+json', + // 'Authorization': `Bearer ${githubToken}`, + // }, + // } + // ); console.log('Branch Updated') } catch (error) { console.log(error)