Support short version tags; fix tests (MAJOR)

This commit is contained in:
Paul Hatcherian 2019-12-09 22:23:58 -05:00
parent bbab98392b
commit d5ce851d69
4 changed files with 97 additions and 50 deletions

View file

@ -15,7 +15,7 @@ jobs:
- name: NPM Install - name: NPM Install
run: npm ci run: npm ci
- name: Test - name: Test
run: npm test run: git config --global user.name "Test User" && git config --global user.email "test@example.com" && npm test
- name: Package - name: Package
run: npm run package run: npm run package
- name: Run Action - name: Run Action

123
dist/index.js vendored
View file

@ -640,82 +640,117 @@ const eol = __webpack_require__(87).EOL;
const cmd = async (command, ...args) => { const cmd = async (command, ...args) => {
let output = ''; let output = '';
const options = {}; const options = {
silent: true
};
options.listeners = { options.listeners = {
silent: true,
stdout: (data) => { output += data.toString(); } stdout: (data) => { output += data.toString(); }
}; };
await exec.exec(command, args, options) await exec.exec(command, args, options)
.catch(err => core.error(err)); .catch(err => { core.error(`${command} ${args.join(' ')} failed: ${err}`); throw err; });
return output; return output;
}; };
async function run() { async function run() {
try { try {
const remotePrefix = 'origin/'; const remote = await cmd('git', 'remote');
const remoteExists = remote !== '';
const remotePrefix = remoteExists ? 'origin/' : '';
const releasePattern = `${remotePrefix}${core.getInput('release_branch', { required: true })}/*`; const tagPrefix = core.getInput('tag_prefix', { required: true });
const branch = `${remotePrefix}${core.getInput('branch', { required: true })}`;
const majorPattern = core.getInput('major_pattern', { required: true }); const majorPattern = core.getInput('major_pattern', { required: true });
const minorPattern = core.getInput('minor_pattern', { required: true }); const minorPattern = core.getInput('minor_pattern', { required: true });
const mainBranch = `${remotePrefix}${core.getInput('main_branch', { required: true })}`;
let major = 0, minor = 0, patch = 0; const releasePattern = `refs/tags/${tagPrefix}*`;
let major = 0, minor = 0, patch = 0, increment = 0;
let branches = await cmd( let tag = (await cmd(
'git', 'git',
`branch`, `for-each-ref`,
`-r`,
`--list`,
`--format='%(refname:short)'`, `--format='%(refname:short)'`,
`--sort=-committerdate`, `--sort=-committerdate`,
releasePattern releasePattern
); )).split(eol)[0].trim().replace(/'/g, "");
var root; let root;
if (branches === '') { if (tag === '') {
// no release branches yet, use the initial commit as the root const isEmpty = (await cmd('git', `status`)).includes('No commits yet');
root = await cmd('git', `rev-list`, `--max-parents=0`, mainBranch); 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');
return;
} else {
// no release tags yet, use the initial commit as the root
root = '';
}
} else { } else {
// find the merge base between the last // parse the version tag
var releaseBranch = branches.split(eol)[0]; let tagParts = tag.split('/');
var versionValues = releaseBranch.split('/')[1].split('.'); let versionValues = tagParts[tagParts.length - 1]
major = parseInt(versionValues[0]); .substr(tagPrefix.length)
minor = parseInt(versionValues[1]); .split('.');
patch = parseInt(versionValues[2]);
root = await cmd('git', `merge-base`, releaseBranch, mainBranch); major = parseInt(versionValues[0]);
minor = versionValues.length > 1 ? parseInt(versionValues[1]) : 0;
patch = versionValues.length > 2 ? parseInt(versionValues[2]) : 0;
if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
throw `Invalid tag ${tag}`;
}
root = await cmd('git', `merge-base`, tag, branch);
} }
root = root.trim(); root = root.trim();
let history = (await cmd('git', 'log', '--pretty="%s"', root, mainBranch)).split(eol); const log = await cmd(
'git',
'log',
'--pretty="%s"',
'--author-date-order',
root === '' ? branch : `${root}..${branch}`);
patch++; let history = log
var increment = history.length; .trim()
for (var i = 0; i < history.length; i++) { .split(eol)
if (history[i].indexOf(majorPattern) !== -1) { .reverse();
major++;
minor = 0; // Discover the change time from the history log by finding the oldest log
patch = 0; // that could set the version.
increment = i + 1;
break; const majorIndex = history.findIndex(x => x.includes(majorPattern));
} else if (history[i].indexOf(minorPattern) !== -1) { const minorIndex = history.findIndex(x => x.includes(minorPattern));
minor++;
patch = 0; if (majorIndex !== -1) {
increment = i + 1; increment = history.length - (majorIndex + 1);
break; patch = 0;
} minor = 0;
major++;
} else if (minorIndex !== -1) {
increment = history.length - (minorIndex + 1);
patch = 0;
minor++;
} else {
increment = history.length - 1;
patch++;
} }
let version = `${major}.${minor}.${patch}`; let version = `${major}.${minor}.${patch}`;
core.info(`Version is ${version}+${increment}`); core.info(`Version is ${version}+${increment}`);
core.setOutput("version", version); core.setOutput("version", version);
core.setOutput("major", major); core.setOutput("major", major.toString());
core.setOutput("minor", minor); core.setOutput("minor", minor.toString());
core.setOutput("patch", patch); core.setOutput("patch", patch.toString());
core.setOutput("increment", increment); core.setOutput("increment", increment.toString());
} catch (error) { } catch (error) {
console.log(error); core.error(error);
core.setFailed(error.message); core.setFailed(error.message);
} }
} }

View file

@ -62,8 +62,8 @@ async function run() {
.split('.'); .split('.');
major = parseInt(versionValues[0]); major = parseInt(versionValues[0]);
minor = parseInt(versionValues[1]); minor = versionValues.length > 1 ? parseInt(versionValues[1]) : 0;
patch = parseInt(versionValues[2]); patch = versionValues.length > 2 ? parseInt(versionValues[2]) : 0;
if (isNaN(major) || isNaN(minor) || isNaN(patch)) { if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
throw `Invalid tag ${tag}`; throw `Invalid tag ${tag}`;

View file

@ -50,7 +50,7 @@ test('Empty repository version is correct', () => {
expect(result).toMatch('Version is 0.0.0+0'); expect(result).toMatch('Version is 0.0.0+0');
repo.clean(); repo.clean();
}) });
test('Repository with commits shows increment', () => { test('Repository with commits shows increment', () => {
const repo = createTestRepo(); // 0.0.0+0 const repo = createTestRepo(); // 0.0.0+0
@ -62,7 +62,7 @@ test('Repository with commits shows increment', () => {
expect(result).toMatch('Version is 0.0.1+1'); expect(result).toMatch('Version is 0.0.1+1');
repo.clean(); repo.clean();
}) });
test('Minor update bumps minor version and resets increment', () => { test('Minor update bumps minor version and resets increment', () => {
const repo = createTestRepo(); // 0.0.0+0 const repo = createTestRepo(); // 0.0.0+0
@ -103,7 +103,6 @@ test('Multiple major commits are idempotent', () => {
repo.clean(); repo.clean();
}); });
test('Minor commits after a major commit are ignored', () => { test('Minor commits after a major commit are ignored', () => {
const repo = createTestRepo(); // 0.0.0+0 const repo = createTestRepo(); // 0.0.0+0
@ -230,3 +229,16 @@ test('Merged tags do not affect version', () => {
repo.clean(); repo.clean();
}); });
test('Version tags do not require all three version numbers', () => {
const repo = createTestRepo(); // 0.0.0+0
repo.makeCommit('Initial Commit (MAJOR)'); // 1.0.0+0
repo.exec('git tag v1');
repo.makeCommit(`Second Commit`); // 1.0.1+0
const result = repo.runAction();
expect(result).toMatch('Version is 1.0.1+0');
repo.clean();
})