Use oktokit

Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
This commit is contained in:
Shakar Bakr 2023-10-04 15:04:54 +03:00
parent aab85ff1bd
commit 539c7861d4
No known key found for this signature in database
GPG Key ID: DA55A26823AE3C28

167
index.js
View File

@ -7,6 +7,7 @@ const convert = require('xml-js');
run(); run();
async function run() { async function run() {
try { try {
console.log(`CWD: ${process.cwd()}`) console.log(`CWD: ${process.cwd()}`)
console.log(`filePath: ${core.getInput('filePath')}`) console.log(`filePath: ${core.getInput('filePath')}`)
@ -82,6 +83,11 @@ async function commitChanges(file, filePath) {
// Get the current branch // Get the current branch
const branch = process.env.GITHUB_REF.replace('refs/heads/', ''); 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}`, 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/blobs`,
`https://api.github.com/repos/${owner}/${repo}/git/trees`, `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}`) `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`)
try { try {
// Get the current commit SHA for the branch // Get the current commit SHA for the branch
const branchResponse = await axios.get( const branchResponse = await oktokit.request(`GET /repos/${owner}/${repo}/branches/${branch}`, {
`https://api.github.com/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; const baseTreeSha = branchResponse.data.commit.sha;
// Create a new blob with the updated content // Create a new blob with the updated content
console.log('Branch') console.log('Branch')
const blobResponse = await axios.post(
`https://api.github.com/repos/${owner}/${repo}/git/blobs`, const blobResponse = await oktokit.request(`POST /repos/${owner}/${repo}/git/blobs`, {
{ owner,
content: newContent, repo,
encoding: 'utf-8', content: newContent,
}, encoding: 'utf-8'
{ })
headers: { // const blobResponse = await axios.post(
'Accept': 'application/vnd.github.v3+json', // `https://api.github.com/repos/${owner}/${repo}/git/blobs`,
'Authorization': `Bearer ${githubToken}`, // {
}, // content: newContent,
} // encoding: 'utf-8',
); // },
// {
// headers: {
// 'Accept': 'application/vnd.github.v3+json',
// 'Authorization': `Bearer ${githubToken}`,
// },
// }
// );
console.log('Blob Created') console.log('Blob Created')
console.log('File path: ', filePath) console.log('File path: ', filePath)
const newBlobSha = blobResponse.data.sha; const newBlobSha = blobResponse.data.sha;
// Create a new tree with the updated blob // Create a new tree with the updated blob
const treeResponse = await axios.post( const treeResponse = await oktokit.request(`POST /repos/${owner}/${repo}/git/trees`, {
`https://api.github.com/repos/${owner}/${repo}/git/trees`, owner,
{ repo,
base_tree: baseTreeSha, base_tree: baseTreeSha,
tree: [ tree: [
{ {
path: filePath, path: filePath,
mode: '100644', mode: '100644',
type: 'blob', type: 'blob',
sha: newBlobSha, sha: newBlobSha,
},
],
},
{
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${githubToken}`
}, },
} ]
); })
// 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') console.log('Tree Created')
const newTreeSha = treeResponse.data.sha; const newTreeSha = treeResponse.data.sha;
// Create a new commit // Create a new commit
const commitResponse = await axios.post( const commitResponse = await oktokit.request(`POST /repos/${owner}/${repo}/git/commits`, {
`https://api.github.com/repos/${owner}/${repo}/git/commits`, owner,
{ repo,
message: commitMessage, message: commitMessage,
tree: newTreeSha, tree: newTreeSha,
parents: [baseTreeSha], parents: [baseTreeSha],
}, })
{
headers: { // const commitResponse = await axios.post(
'Accept': 'application/vnd.github+json', // `https://api.github.com/repos/${owner}/${repo}/git/commits`,
'Authorization': `Bearer ${githubToken}`, // {
}, // message: commitMessage,
} // tree: newTreeSha,
); // parents: [baseTreeSha],
// },
// {
// headers: {
// 'Accept': 'application/vnd.github+json',
// 'Authorization': `Bearer ${githubToken}`,
// },
// }
// );
console.log('Commit Created') console.log('Commit Created')
const newCommitSha = commitResponse.data.sha; const newCommitSha = commitResponse.data.sha;
// Update the branch reference // Update the branch reference
await axios.patch( await oktokit.request(`PATCH /repos/${owner}/${repo}/git/refs/heads/${branch}`, {
`https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`, owner,
{ repo,
sha: newCommitSha, sha: newCommitSha
}, })
{
headers: { // await axios.patch(
'Accept': 'application/vnd.github+json', // `https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`,
'Authorization': `Bearer ${githubToken}`, // {
}, // sha: newCommitSha,
} // },
); // {
// headers: {
// 'Accept': 'application/vnd.github+json',
// 'Authorization': `Bearer ${githubToken}`,
// },
// }
// );
console.log('Branch Updated') console.log('Branch Updated')
} catch (error) { } catch (error) {
console.log(error) console.log(error)