Skip to content

Commit

Permalink
Support regex as major/minor match patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHatch committed Jan 23, 2021
1 parent 9e192e1 commit 9475102
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ inputs:
required: false
default: "v"
major_pattern:
description: "A string which, if present in a git commit, indicates that a change represents a major (breaking) change"
description: "A string which, if present in a git commit, indicates that a change represents a major (breaking) change. Wrap with '/' to match using a regular expression."
required: true
default: "(MAJOR)"
minor_pattern:
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. Wrap with '/' to match using a regular expression."
required: true
default: "(MINOR)"
format:
Expand Down
23 changes: 17 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1113,15 +1113,26 @@ const parseVersion = (tag) => {
return [major, minor, patch];
};

const createMatchTest = (pattern) => {

if (pattern.startsWith('/') && pattern.endsWith('/')) {
var regex = new RegExp(pattern.slice(1, -1));
return (l) => regex.test(l);
} else {
return (l) => l.includes(pattern);
}

};

async function run() {
try {
const remote = await cmd('git', 'remote');
const remoteExists = remote !== '';
const remotePrefix = remoteExists ? 'origin/' : '';

const branch = `${remotePrefix}${core.getInput('branch', { required: true })}`;
const majorPattern = core.getInput('major_pattern', { required: true });
const minorPattern = core.getInput('minor_pattern', { required: true });
const majorPattern = createMatchTest(core.getInput('major_pattern', { required: true }));
const minorPattern = createMatchTest(core.getInput('minor_pattern', { required: true }));
const changePath = core.getInput('change_path') || '';

const versionPattern = shortTags ? '*[0-9.]' : '[0-9]+\\.[0-9]+\\.[0-9]+'
Expand Down Expand Up @@ -1199,11 +1210,11 @@ async function run() {
history.forEach(line => {
if (currentTag) {
[major, minor, patch] = parseVersion(currentTag);
} else if (line.includes(majorPattern)) {
} else if (majorPattern(line)) {
major += 1;
minor = 0;
patch = 0;
} else if (line.includes(minorPattern)) {
} else if (minorPattern(line)) {
minor += 1;
patch = 0;
} else {
Expand All @@ -1218,8 +1229,8 @@ async function run() {
// Discover the change time from the history log by finding the oldest log
// that could set the version.

const majorIndex = history.findIndex(x => x.includes(majorPattern));
const minorIndex = history.findIndex(x => x.includes(minorPattern));
const majorIndex = history.findIndex(x => majorPattern(x));
const minorIndex = history.findIndex(x => minorPattern(x));

if (majorIndex !== -1) {
increment = history.length - (majorIndex + 1);
Expand Down
23 changes: 17 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,26 @@ const parseVersion = (tag) => {
return [major, minor, patch];
};

const createMatchTest = (pattern) => {

if (pattern.startsWith('/') && pattern.endsWith('/')) {
var regex = new RegExp(pattern.slice(1, -1));
return (l) => regex.test(l);
} else {
return (l) => l.includes(pattern);
}

};

async function run() {
try {
const remote = await cmd('git', 'remote');
const remoteExists = remote !== '';
const remotePrefix = remoteExists ? 'origin/' : '';

const branch = `${remotePrefix}${core.getInput('branch', { required: true })}`;
const majorPattern = core.getInput('major_pattern', { required: true });
const minorPattern = core.getInput('minor_pattern', { required: true });
const majorPattern = createMatchTest(core.getInput('major_pattern', { required: true }));
const minorPattern = createMatchTest(core.getInput('minor_pattern', { required: true }));
const changePath = core.getInput('change_path') || '';

const versionPattern = shortTags ? '*[0-9.]' : '[0-9]+\\.[0-9]+\\.[0-9]+'
Expand Down Expand Up @@ -178,11 +189,11 @@ async function run() {
history.forEach(line => {
if (currentTag) {
[major, minor, patch] = parseVersion(currentTag);
} else if (line.includes(majorPattern)) {
} else if (majorPattern(line)) {
major += 1;
minor = 0;
patch = 0;
} else if (line.includes(minorPattern)) {
} else if (minorPattern(line)) {
minor += 1;
patch = 0;
} else {
Expand All @@ -197,8 +208,8 @@ async function run() {
// Discover the change time from the history log by finding the oldest log
// that could set the version.

const majorIndex = history.findIndex(x => x.includes(majorPattern));
const minorIndex = history.findIndex(x => x.includes(minorPattern));
const majorIndex = history.findIndex(x => majorPattern(x));
const minorIndex = history.findIndex(x => minorPattern(x));

if (majorIndex !== -1) {
increment = history.length - (majorIndex + 1);
Expand Down
20 changes: 20 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,5 +513,25 @@ test('Increment not affected by matching tag', () => {
repo.exec('git tag 1.0.0');
expect(repo.runAction()).toMatch('Version is 0.0.1+1');

repo.clean();
});

test('Regular expressions can be used as major tag', () => {
const repo = createTestRepo({ tag_prefix: '', major_pattern: '/S[a-z]+Value/' }); // 0.0.1

repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit SomeValue'); // 0.0.1+1
expect(repo.runAction()).toMatch('Version is 1.0.0+0');

repo.clean();
});

test('Regular expressions can be used as minor tag', () => {
const repo = createTestRepo({ tag_prefix: '', minor_pattern: '/S[a-z]+Value/' }); // 0.0.1

repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit SomeValue'); // 0.0.1+1
expect(repo.runAction()).toMatch('Version is 0.1.0+0');

repo.clean();
});
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ it will be given the new version if the build were to be retriggered, for exampl
# The prefix to use to identify tags
tag_prefix: "v"
# A string which, if present in a git commit, indicates that a change represents a
# major (breaking) change
# major (breaking) change, supports regular expressions wrapped with '/'
major_pattern: "(MAJOR)"
# Same as above except indicating a minor change
# Same as above except indicating a minor change, supports regular expressions wrapped with '/'
minor_pattern: "(MINOR)"
# A string to determine the format of the version output
format: "${major}.${minor}.${patch}-prerelease.${increment}"
Expand Down

0 comments on commit 9475102

Please sign in to comment.