mirror of
https://github.com/PaulHatch/semantic-version.git
synced 2025-12-27 13:08:17 +00:00
Add format input for version output templating
This commit is contained in:
parent
d5ce851d69
commit
142e038c41
3 changed files with 52 additions and 20 deletions
|
|
@ -20,6 +20,10 @@ inputs:
|
||||||
description: "a string which, if present in a git commit, indicates that a change represents a minor (feature) change"
|
description: "a string which, if present in a git commit, indicates that a change represents a minor (feature) change"
|
||||||
required: true
|
required: true
|
||||||
default: "(MINOR)"
|
default: "(MINOR)"
|
||||||
|
format:
|
||||||
|
description: "Pattern to use when formatting output version"
|
||||||
|
required: true
|
||||||
|
default: "${major}.${minor}.${patch}"
|
||||||
|
|
||||||
outputs:
|
outputs:
|
||||||
major:
|
major:
|
||||||
|
|
|
||||||
31
index.js
31
index.js
|
|
@ -16,6 +16,22 @@ const cmd = async (command, ...args) => {
|
||||||
return output;
|
return output;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const setOutput = (major, minor, patch, increment) => {
|
||||||
|
const format = core.getInput('format', { required: true });
|
||||||
|
var version = format
|
||||||
|
.replace('${major}', major)
|
||||||
|
.replace('${minor}', minor)
|
||||||
|
.replace('${patch}', patch)
|
||||||
|
.replace('${increment}', increment);
|
||||||
|
|
||||||
|
core.info(`Version is ${major}.${minor}.${patch}+${increment}`);
|
||||||
|
core.setOutput("version", version);
|
||||||
|
core.setOutput("major", major.toString());
|
||||||
|
core.setOutput("minor", minor.toString());
|
||||||
|
core.setOutput("patch", patch.toString());
|
||||||
|
core.setOutput("increment", increment.toString());
|
||||||
|
};
|
||||||
|
|
||||||
async function run() {
|
async function run() {
|
||||||
try {
|
try {
|
||||||
const remote = await cmd('git', 'remote');
|
const remote = await cmd('git', 'remote');
|
||||||
|
|
@ -43,12 +59,7 @@ async function run() {
|
||||||
const isEmpty = (await cmd('git', `status`)).includes('No commits yet');
|
const isEmpty = (await cmd('git', `status`)).includes('No commits yet');
|
||||||
if (isEmpty) {
|
if (isEmpty) {
|
||||||
// empty repo
|
// empty repo
|
||||||
core.info('Version is 0.0.0+0');
|
setOutput('0', '0', '0', '0');
|
||||||
core.setOutput("version", '0.0.0+0');
|
|
||||||
core.setOutput("major", '0');
|
|
||||||
core.setOutput("minor", '0');
|
|
||||||
core.setOutput("patch", '0');
|
|
||||||
core.setOutput("increment", '0');
|
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
// no release tags yet, use the initial commit as the root
|
// no release tags yet, use the initial commit as the root
|
||||||
|
|
@ -105,13 +116,7 @@ async function run() {
|
||||||
patch++;
|
patch++;
|
||||||
}
|
}
|
||||||
|
|
||||||
let version = `${major}.${minor}.${patch}`;
|
setOutput(major, minor, patch, increment);
|
||||||
core.info(`Version is ${version}+${increment}`);
|
|
||||||
core.setOutput("version", version);
|
|
||||||
core.setOutput("major", major.toString());
|
|
||||||
core.setOutput("minor", minor.toString());
|
|
||||||
core.setOutput("patch", patch.toString());
|
|
||||||
core.setOutput("increment", increment.toString());
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.error(error);
|
core.error(error);
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,26 @@ process.env['INPUT_BRANCH'] = "master";
|
||||||
process.env['INPUT_TAG_PREFIX'] = "v";
|
process.env['INPUT_TAG_PREFIX'] = "v";
|
||||||
process.env['INPUT_MAJOR_PATTERN'] = "(MAJOR)";
|
process.env['INPUT_MAJOR_PATTERN'] = "(MAJOR)";
|
||||||
process.env['INPUT_MINOR_PATTERN'] = "(MINOR)";
|
process.env['INPUT_MINOR_PATTERN'] = "(MINOR)";
|
||||||
|
process.env['INPUT_FORMAT'] = "${major}.${minor}.${patch}";
|
||||||
|
|
||||||
// Creates a randomly named git repository and returns a function to execute commands in it
|
// Creates a randomly named git repository and returns a function to execute commands in it
|
||||||
const createTestRepo = () => {
|
const createTestRepo = (inputs) => {
|
||||||
const repoDirectory = `/tmp/test${Math.random().toString(36).substring(2, 15)}`;
|
const repoDirectory = `/tmp/test${Math.random().toString(36).substring(2, 15)}`;
|
||||||
cp.execSync(`mkdir ${repoDirectory} && git init ${repoDirectory}`);
|
cp.execSync(`mkdir ${repoDirectory} && git init ${repoDirectory}`);
|
||||||
// Configure up git user
|
|
||||||
cp.execSync(`git config user.name "Test User"`)
|
|
||||||
cp.execSync(`git config user.email "test@example.com"`);
|
|
||||||
|
|
||||||
const run = (command) => execute(repoDirectory, command);
|
let env = {};
|
||||||
|
if (inputs !== undefined) {
|
||||||
|
for (let key in inputs) {
|
||||||
|
env[`INPUT_${key.toUpperCase()}`] = inputs[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const run = (command) => execute(repoDirectory, command, env);
|
||||||
|
|
||||||
|
// Configure up git user
|
||||||
|
run(`git config user.name "Test User"`);
|
||||||
|
run(`git config user.email "test@example.com"`);
|
||||||
|
|
||||||
let i = 1;
|
let i = 1;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
@ -32,9 +42,9 @@ const createTestRepo = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Executes a set of commands in the specified directory
|
// Executes a set of commands in the specified directory
|
||||||
const execute = (workingDirectory, command) => {
|
const execute = (workingDirectory, command, env) => {
|
||||||
try {
|
try {
|
||||||
return String(cp.execSync(command, { env: process.env, cwd: workingDirectory }));
|
return String(cp.execSync(command, { env: { ...process.env, ...env }, cwd: workingDirectory }));
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.error(String(e.stdout));
|
console.error(String(e.stdout));
|
||||||
|
|
@ -240,5 +250,18 @@ test('Version tags do not require all three version numbers', () => {
|
||||||
|
|
||||||
expect(result).toMatch('Version is 1.0.1+0');
|
expect(result).toMatch('Version is 1.0.1+0');
|
||||||
|
|
||||||
|
repo.clean();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Format input is respected', () => {
|
||||||
|
const repo = createTestRepo({ format: 'M${major}m${minor}p${patch}i${increment}' }); // M0m0p0i0
|
||||||
|
|
||||||
|
repo.makeCommit('Initial Commit'); // M1m2p3i0
|
||||||
|
repo.exec('git tag v1.2.3');
|
||||||
|
repo.makeCommit(`Second Commit`); // M1m2p4i0
|
||||||
|
const result = repo.runAction();
|
||||||
|
|
||||||
|
expect(result).toMatch('M1m2p4i0');
|
||||||
|
|
||||||
repo.clean();
|
repo.clean();
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue