Add format input for version output templating

This commit is contained in:
Paul Hatcherian 2019-12-10 00:16:53 -05:00
parent d5ce851d69
commit 142e038c41
3 changed files with 52 additions and 20 deletions

View file

@ -20,6 +20,10 @@ inputs:
description: "a string which, if present in a git commit, indicates that a change represents a minor (feature) change"
required: true
default: "(MINOR)"
format:
description: "Pattern to use when formatting output version"
required: true
default: "${major}.${minor}.${patch}"
outputs:
major:

View file

@ -16,6 +16,22 @@ const cmd = async (command, ...args) => {
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() {
try {
const remote = await cmd('git', 'remote');
@ -43,12 +59,7 @@ async function run() {
const isEmpty = (await cmd('git', `status`)).includes('No commits yet');
if (isEmpty) {
// empty repo
core.info('Version is 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');
setOutput('0', '0', '0', '0');
return;
} else {
// no release tags yet, use the initial commit as the root
@ -105,13 +116,7 @@ async function run() {
patch++;
}
let version = `${major}.${minor}.${patch}`;
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());
setOutput(major, minor, patch, increment);
} catch (error) {
core.error(error);

View file

@ -7,16 +7,26 @@ process.env['INPUT_BRANCH'] = "master";
process.env['INPUT_TAG_PREFIX'] = "v";
process.env['INPUT_MAJOR_PATTERN'] = "(MAJOR)";
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
const createTestRepo = () => {
const createTestRepo = (inputs) => {
const repoDirectory = `/tmp/test${Math.random().toString(36).substring(2, 15)}`;
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;
return {
@ -32,9 +42,9 @@ const createTestRepo = () => {
};
// Executes a set of commands in the specified directory
const execute = (workingDirectory, command) => {
const execute = (workingDirectory, command, env) => {
try {
return String(cp.execSync(command, { env: process.env, cwd: workingDirectory }));
return String(cp.execSync(command, { env: { ...process.env, ...env }, cwd: workingDirectory }));
}
catch (e) {
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');
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();
})