mirror of
https://github.com/ditkrg/project-version-check.git
synced 2026-01-22 22:06:42 +00:00
Delete commit.js and put everything into index.js
Signed-off-by: Shakar <5h4k4r.b4kr@gmail.com>
This commit is contained in:
parent
36fbfe3356
commit
81f74c5925
81
commit.js
81
commit.js
@ -1,81 +0,0 @@
|
|||||||
const axios = require('axios');
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export async function commitChanges(filePath) {
|
|
||||||
const commitMessage = 'Commit message here';
|
|
||||||
const newContent = 'New content to be added';
|
|
||||||
|
|
||||||
|
|
||||||
// Get the repository owner and name
|
|
||||||
const repoFullName = process.env.GITHUB_REPOSITORY;
|
|
||||||
const [owner, repo] = repoFullName.split('/');
|
|
||||||
|
|
||||||
// Get the current branch
|
|
||||||
const branch = process.env.GITHUB_REF.replace('refs/heads/', '');
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Get the current commit SHA for the 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
|
|
||||||
const blobResponse = await axios.post(
|
|
||||||
`https://api.github.com/repos/${owner}/${repo}/git/blobs`,
|
|
||||||
{
|
|
||||||
content: newContent,
|
|
||||||
encoding: 'utf-8',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
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,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
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],
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
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,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log('Changes committed successfully!');
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error committing changes:', error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
commitChanges();
|
|
||||||
172
index.js
172
index.js
@ -1,60 +1,140 @@
|
|||||||
const core = require('@actions/core')
|
const core = require('@actions/core')
|
||||||
const github = require('@actions/github')
|
const github = require('@actions/github')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
import commitChanges from './commit.js';
|
|
||||||
|
|
||||||
try {
|
async function run() {
|
||||||
console.log(`CWD: ${process.cwd()}`)
|
try {
|
||||||
const label = core.getInput('label');
|
console.log(`CWD: ${process.cwd()}`)
|
||||||
const filePath = `${process.cwd()}/${core.getInput('filePath')}`;
|
const label = core.getInput('label');
|
||||||
|
const filePath = `${process.cwd()}/${core.getInput('filePath')}`;
|
||||||
|
|
||||||
|
|
||||||
console.log(`Label: ${label}`)
|
console.log(`Label: ${label}`)
|
||||||
console.log(`File path: ${filePath}`)
|
console.log(`File path: ${filePath}`)
|
||||||
|
|
||||||
core.setOutput("label", label);
|
core.setOutput("label", label);
|
||||||
|
|
||||||
// if (project == 'dotnet')
|
// if (project == 'dotnet')
|
||||||
// filePath = `${filePath}/.csproj`;
|
// filePath = `${filePath}/.csproj`;
|
||||||
// else if (project == 'nodejs')
|
// else if (project == 'nodejs')
|
||||||
// filePath = `${filePath}/package.json`;
|
// filePath = `${filePath}/package.json`;
|
||||||
|
|
||||||
const packageJson = require(filePath);
|
const packageJson = require(filePath);
|
||||||
const version = packageJson.version;
|
const version = packageJson.version;
|
||||||
|
|
||||||
|
|
||||||
// the version is in semantic format, so we can split it by dot
|
// the version is in semantic format, so we can split it by dot
|
||||||
const versionParts = version.split('.');
|
const versionParts = version.split('.');
|
||||||
if (label === 'major') {
|
if (label === 'major') {
|
||||||
versionParts[0] = parseInt(versionParts[0]) + 1;
|
versionParts[0] = parseInt(versionParts[0]) + 1;
|
||||||
versionParts[1] = 0;
|
versionParts[1] = 0;
|
||||||
versionParts[2] = 0;
|
versionParts[2] = 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (label == 'minor') {
|
||||||
|
versionParts[1] = parseInt(versionParts[1]) + 1;
|
||||||
|
versionParts[2] = 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (label == 'patch')
|
||||||
|
versionParts[2] = parseInt(versionParts[2]) + 1;
|
||||||
|
|
||||||
|
|
||||||
|
console.log(versionParts)
|
||||||
|
// increment the patch version
|
||||||
|
// join the parts back together
|
||||||
|
const newVersion = `v${versionParts.join('.')}`;
|
||||||
|
|
||||||
|
|
||||||
|
console.log(`Old version: ${version}. New version: ${newVersion}`)
|
||||||
|
|
||||||
|
packageJson.version = newVersion;
|
||||||
|
|
||||||
|
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, 2));
|
||||||
|
|
||||||
|
await commitChanges();
|
||||||
|
|
||||||
|
const payload = JSON.stringify(github.context.payload, undefined, 2)
|
||||||
|
|
||||||
|
// console.log(`The event payload: ${payload}`);
|
||||||
|
} catch (error) {
|
||||||
|
core.setFailed(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Region functions
|
||||||
|
async function commitChanges(filePath) {
|
||||||
|
const commitMessage = 'Commit message here';
|
||||||
|
const newContent = 'New content to be added';
|
||||||
|
|
||||||
|
|
||||||
|
// Get the repository owner and name
|
||||||
|
const repoFullName = process.env.GITHUB_REPOSITORY;
|
||||||
|
const [owner, repo] = repoFullName.split('/');
|
||||||
|
|
||||||
|
// Get the current branch
|
||||||
|
const branch = process.env.GITHUB_REF.replace('refs/heads/', '');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get the current commit SHA for the 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
|
||||||
|
const blobResponse = await axios.post(
|
||||||
|
`https://api.github.com/repos/${owner}/${repo}/git/blobs`,
|
||||||
|
{
|
||||||
|
content: newContent,
|
||||||
|
encoding: 'utf-8',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
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],
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log('Changes committed successfully!');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error committing changes:', error);
|
||||||
}
|
}
|
||||||
else if (label == 'minor') {
|
|
||||||
versionParts[1] = parseInt(versionParts[1]) + 1;
|
|
||||||
versionParts[2] = 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (label == 'patch')
|
|
||||||
versionParts[2] = parseInt(versionParts[2]) + 1;
|
|
||||||
|
|
||||||
|
|
||||||
console.log(versionParts)
|
|
||||||
// increment the patch version
|
|
||||||
// join the parts back together
|
|
||||||
const newVersion = `v${versionParts.join('.')}`;
|
|
||||||
|
|
||||||
|
|
||||||
console.log(`Old version: ${version}. New version: ${newVersion}`)
|
|
||||||
|
|
||||||
packageJson.version = newVersion;
|
|
||||||
|
|
||||||
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, 2));
|
|
||||||
commitChanges();
|
|
||||||
const payload = JSON.stringify(github.context.payload, undefined, 2)
|
|
||||||
|
|
||||||
// console.log(`The event payload: ${payload}`);
|
|
||||||
} catch (error) {
|
|
||||||
core.setFailed(error.message);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user