mirror of
https://github.com/ditkrg/project-version-check.git
synced 2026-01-22 22:06:42 +00:00
Use oktokit
Signed-off-by: Shakar Bakr <5h4k4r.b4kr@gmail.com>
This commit is contained in:
parent
aab85ff1bd
commit
539c7861d4
145
index.js
145
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,34 +95,46 @@ 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`,
|
||||
{
|
||||
|
||||
const blobResponse = await oktokit.request(`POST /repos/${owner}/${repo}/git/blobs`, {
|
||||
owner,
|
||||
repo,
|
||||
content: newContent,
|
||||
encoding: 'utf-8',
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Accept': 'application/vnd.github.v3+json',
|
||||
'Authorization': `Bearer ${githubToken}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
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`,
|
||||
{
|
||||
const treeResponse = await oktokit.request(`POST /repos/${owner}/${repo}/git/trees`, {
|
||||
owner,
|
||||
repo,
|
||||
base_tree: baseTreeSha,
|
||||
tree: [
|
||||
{
|
||||
@ -125,51 +143,78 @@ async function commitChanges(file, filePath) {
|
||||
type: 'blob',
|
||||
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')
|
||||
const newTreeSha = treeResponse.data.sha;
|
||||
|
||||
// Create a new commit
|
||||
const commitResponse = await axios.post(
|
||||
`https://api.github.com/repos/${owner}/${repo}/git/commits`,
|
||||
{
|
||||
const commitResponse = await oktokit.request(`POST /repos/${owner}/${repo}/git/commits`, {
|
||||
owner,
|
||||
repo,
|
||||
message: commitMessage,
|
||||
tree: newTreeSha,
|
||||
parents: [baseTreeSha],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Accept': 'application/vnd.github+json',
|
||||
'Authorization': `Bearer ${githubToken}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
})
|
||||
|
||||
// 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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user