semantic-branch-version/index.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

const core = require('@actions/core');
2020-07-04 23:47:33 +08:00
const execlib = require('@actions/exec');
try {
2020-07-05 14:46:24 +08:00
getBranchName()
2020-07-05 14:49:01 +08:00
.then(name => {
core.setOutput("branchname", name)
2020-07-05 15:18:35 +08:00
const versionNumber = extractVersion(name);
if (versionNumber) {
2020-07-05 15:50:20 +08:00
core.setOutput('VERSION', versionNumber.version);
core.setOutput('MAJOR_VERSION', versionNumber.major);
core.setOutput('MINOR_VERSION', versionNumber.minor);
core.setOutput('PATCH_VERSION', versionNumber.patch);
2020-07-05 15:18:35 +08:00
}
2020-07-05 14:49:01 +08:00
});
2020-07-05 00:45:30 +08:00
} catch (error) {
core.setFailed(error.message);
}
async function getBranchName() {
let output = '';
2020-07-04 23:47:33 +08:00
const options = {
listeners: {
2020-07-05 14:42:39 +08:00
stdout: (data) => {
output += data.toString();
}
2020-07-05 01:03:59 +08:00
}
2020-07-04 23:47:33 +08:00
};
await execlib.exec('git', ['branch', '--show-current'], options);
2020-07-05 00:45:30 +08:00
return output;
2020-07-05 15:18:35 +08:00
}
function extractVersion(branch) {
const regexp = /([0-9]\.[0-9]\.[0-9])|([0-9]\.[0-9])|([0-9])/g;
const matches = branch.match(regexp);
2020-07-05 15:50:20 +08:00
const versions = null;
if (matches) {
const version = matches.shift();
var versionParts = version.split('.');
var major = versionParts[0];
var minor = versionParts.length >= 2 ? versionParts[1] : null;
var patch = versionParts.length >= 3 ? versionParts[2] : null;
versions = {
version: version,
major: major,
minor: minor,
patch: patch
}
}
2020-07-05 15:50:20 +08:00
return versions
}