diff --git a/action.yml b/action.yml index fcc2ff4..d0111b4 100644 --- a/action.yml +++ b/action.yml @@ -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: diff --git a/dist/index.js b/dist/index.js index 23c0ac1..783b3ed 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1113,6 +1113,17 @@ 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'); @@ -1120,8 +1131,8 @@ async function run() { 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]+' @@ -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 { @@ -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); diff --git a/index.js b/index.js index 252f0ec..724245d 100644 --- a/index.js +++ b/index.js @@ -92,6 +92,17 @@ 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'); @@ -99,8 +110,8 @@ async function run() { 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]+' @@ -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 { @@ -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); diff --git a/index.test.js b/index.test.js index 4ad0519..531120e 100644 --- a/index.test.js +++ b/index.test.js @@ -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(); }); \ No newline at end of file diff --git a/readme.md b/readme.md index faffeae..872ccfc 100644 --- a/readme.md +++ b/readme.md @@ -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}"