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();
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)