gh-action-bump-version/index.js

168 lines
6.7 KiB
JavaScript
Raw Normal View History

2021-05-20 20:54:50 +00:00
const { Toolkit } = require('actions-toolkit');
const { execSync } = require('child_process');
2019-10-26 19:06:00 +00:00
2020-01-13 14:04:50 +00:00
// Change working directory if user defined PACKAGEJSON_DIR
if (process.env.PACKAGEJSON_DIR) {
2021-05-20 20:54:50 +00:00
process.env.GITHUB_WORKSPACE = `${process.env.GITHUB_WORKSPACE}/${process.env.PACKAGEJSON_DIR}`;
process.chdir(process.env.GITHUB_WORKSPACE);
2020-01-13 14:04:50 +00:00
}
2019-10-26 19:06:00 +00:00
// Run your GitHub Action!
2021-05-20 20:54:50 +00:00
Toolkit.run(async (tools) => {
const pkg = tools.getPackageJSON();
const event = tools.context.payload;
2019-10-26 19:06:00 +00:00
if (!event.commits) {
2021-05-20 20:54:50 +00:00
console.log("Couldn't find any commits in this event, incrementing patch version...");
}
2021-05-20 20:54:50 +00:00
const messages = event.commits ? event.commits.map((commit) => commit.message + '\n' + commit.body) : [];
2019-10-26 19:06:00 +00:00
2021-05-20 20:54:50 +00:00
const commitMessage = process.env['INPUT_COMMIT-MESSAGE'] || 'ci: version bump to {{version}}';
console.log('messages:', messages);
2021-03-15 15:58:12 +00:00
const commitMessageRegex = new RegExp(commitMessage.replace(/{{version}}/g, 'v\\d+\\.\\d+\\.\\d+'), 'ig');
2021-05-20 20:54:50 +00:00
const isVersionBump = messages.find((message) => commitMessageRegex.test(message)) !== undefined;
2019-10-26 19:06:00 +00:00
if (isVersionBump) {
2021-05-20 20:54:50 +00:00
tools.exit.success('No action necessary!');
return;
2019-10-26 19:06:00 +00:00
}
2021-05-20 20:54:50 +00:00
const majorWords = process.env['INPUT_MAJOR-WORDING'].split(',');
const minorWords = process.env['INPUT_MINOR-WORDING'].split(',');
const preReleaseWords = process.env['INPUT_RC-WORDING'].split(',');
2021-07-01 09:58:41 +00:00
const patchWords = process.env['INPUT_PATCH-WORDING'].split(',');
2020-10-06 15:50:52 +00:00
let version = process.env.INPUT_DEFAULT;
2021-05-20 20:54:50 +00:00
let foundWord = null;
let preid = process.env.INPUT_PREID;
2021-07-01 10:08:46 +00:00
tools.log('Version is set to Begining: ' + version);
2021-05-20 20:54:50 +00:00
if (
messages.some(
(message) => /^([a-zA-Z]+)(\(.+\))?(\!)\:/.test(message) || majorWords.some((word) => message.includes(word)),
)
) {
version = 'major';
2021-07-01 09:50:22 +00:00
tools.log('Version is set to major');
2021-05-20 20:54:50 +00:00
} else if (messages.some((message) => minorWords.some((word) => message.includes(word)))) {
version = 'minor';
2021-07-01 09:50:22 +00:00
tools.log('Version is set to minor');
2021-05-20 20:54:50 +00:00
} else if (
messages.some((message) =>
preReleaseWords.some((word) => {
if (message.includes(word)) {
foundWord = word;
return true;
} else {
return false;
}
}),
)
) {
preid = foundWord.split('-')[1];
version = 'prerelease';
2021-07-01 09:50:22 +00:00
tools.log('Version is set to prerelease2');
} else if (Array.isArray(patchWords) && patchWords.length) {
2021-07-01 10:08:46 +00:00
tools.log('PatchWords is set : ' + patchWords);
2021-05-20 20:54:50 +00:00
if (!messages.some((message) => patchWords.some((word) => message.includes(word)))) {
2021-07-01 10:08:46 +00:00
tools.log('Messages found on the patchwords : ' + messages);
2021-05-20 20:54:50 +00:00
version = null;
2021-07-01 09:50:22 +00:00
tools.log('Version is set to null 2');
2020-10-09 16:55:11 +00:00
}
2021-07-01 09:43:08 +00:00
} else if (process.env.INPUT_DEFAULT === 'prerelease'){
2021-07-01 09:50:22 +00:00
tools.log('Version is set to prerelease');
2021-07-01 09:43:08 +00:00
version = 'prerelease';
2020-10-09 16:55:11 +00:00
}
// case: if default=prerelease, but rc-wording is also set
// then unset it and do not run, when no rc words found in message
2021-07-01 09:35:07 +00:00
tools.log('Before Login');
2021-07-01 09:50:22 +00:00
tools.log(version); // null
tools.log('PreID: ' + preid); //prc
2021-07-01 09:35:07 +00:00
tools.log(process.env.INPUT_DEFAULT);
2021-07-01 08:53:11 +00:00
if (version === 'prerelease' && !messages.some((message) => preReleaseWords.some((word) => message.includes(word))) && process.env.INPUT_DEFAULT != 'prerelease') {
2021-07-01 09:35:07 +00:00
tools.log('Version is set to null');
version = null;
}
2021-07-01 10:08:46 +00:00
if (version === 'prerelease' || process.env.INPUT_DEFAULT === 'prerelease' && preid) {
2021-07-01 09:35:07 +00:00
tools.log('Version is prerelease and it contains a preid');
2021-05-20 20:54:50 +00:00
version = `${version} --preid=${preid}`;
}
2020-10-09 16:55:11 +00:00
if (version === null) {
2021-05-20 20:54:50 +00:00
tools.exit.success('No version keywords found, skipping bump.');
return;
2019-10-26 19:06:00 +00:00
}
try {
2021-05-20 20:54:50 +00:00
const current = pkg.version.toString();
2019-10-26 19:42:48 +00:00
// set git user
2021-05-20 20:54:50 +00:00
await tools.runInWorkspace('git', [
'config',
'user.name',
`"${process.env.GITHUB_USER || 'Automated Version Bump'}"`,
]);
await tools.runInWorkspace('git', [
'config',
'user.email',
`"${process.env.GITHUB_EMAIL || 'gh-action-bump-version@users.noreply.github.com'}"`,
]);
2019-10-26 19:42:48 +00:00
2021-05-20 20:54:50 +00:00
let currentBranch = /refs\/[a-zA-Z]+\/(.*)/.exec(process.env.GITHUB_REF)[1];
let isPullRequest = false;
2020-09-26 12:13:48 +00:00
if (process.env.GITHUB_HEAD_REF) {
2020-09-26 12:34:59 +00:00
// Comes from a pull request
2021-05-20 20:54:50 +00:00
currentBranch = process.env.GITHUB_HEAD_REF;
isPullRequest = true;
2020-09-26 12:13:48 +00:00
}
2020-11-25 14:47:18 +00:00
if (process.env['INPUT_TARGET-BRANCH']) {
// We want to override the branch that we are pulling / pushing to
2021-05-20 20:54:50 +00:00
currentBranch = process.env['INPUT_TARGET-BRANCH'];
}
2021-05-20 20:54:50 +00:00
console.log('currentBranch:', currentBranch);
// do it in the current checked out github branch (DETACHED HEAD)
// important for further usage of the package.json version
2021-05-20 20:54:50 +00:00
await tools.runInWorkspace('npm', ['version', '--allow-same-version=true', '--git-tag-version=false', current]);
console.log('current:', current, '/', 'version:', version);
let newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim().replace(/^v/, '');
newVersion = `${process.env['INPUT_TAG-PREFIX']}${newVersion}`;
2021-05-20 20:54:50 +00:00
await tools.runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]);
// now go to the actual branch to perform the same versioning
2020-09-30 17:41:15 +00:00
if (isPullRequest) {
// First fetch to get updated local version of branch
2021-05-20 20:54:50 +00:00
await tools.runInWorkspace('git', ['fetch']);
2020-09-30 17:41:15 +00:00
}
2021-05-20 20:54:50 +00:00
await tools.runInWorkspace('git', ['checkout', currentBranch]);
await tools.runInWorkspace('npm', ['version', '--allow-same-version=true', '--git-tag-version=false', current]);
console.log('current:', current, '/', 'version:', version);
newVersion = execSync(`npm version --git-tag-version=false ${version}`).toString().trim().replace(/^v/, '');
2021-05-20 20:54:50 +00:00
newVersion = `${process.env['INPUT_TAG-PREFIX']}${newVersion}`;
console.log(`::set-output name=newTag::${newVersion}`);
2020-04-03 23:39:59 +00:00
try {
// to support "actions/checkout@v1"
2021-05-20 20:54:50 +00:00
await tools.runInWorkspace('git', ['commit', '-a', '-m', commitMessage.replace(/{{version}}/g, newVersion)]);
2020-04-03 23:39:59 +00:00
} catch (e) {
2021-05-20 20:54:50 +00:00
console.warn(
'git commit failed because you are using "actions/checkout@v2"; ' +
'but that doesnt matter because you dont need that git commit, thats only for "actions/checkout@v1"',
);
2020-04-03 23:39:59 +00:00
}
2020-04-03 11:04:35 +00:00
2021-05-20 20:54:50 +00:00
const remoteRepo = `https://${process.env.GITHUB_ACTOR}:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git`;
2020-09-06 11:13:52 +00:00
if (process.env['INPUT_SKIP-TAG'] !== 'true') {
2021-05-20 20:54:50 +00:00
await tools.runInWorkspace('git', ['tag', newVersion]);
await tools.runInWorkspace('git', ['push', remoteRepo, '--follow-tags']);
await tools.runInWorkspace('git', ['push', remoteRepo, '--tags']);
} else {
2021-05-20 20:54:50 +00:00
await tools.runInWorkspace('git', ['push', remoteRepo]);
2020-09-06 11:13:52 +00:00
}
2019-10-26 19:06:00 +00:00
} catch (e) {
2021-05-20 20:54:50 +00:00
tools.log.fatal(e);
tools.exit.failure('Failed to bump version');
2019-10-26 19:06:00 +00:00
}
2021-05-20 20:54:50 +00:00
tools.exit.success('Version bumped!');
});