From 959e71ff1cb1b40dbe712d14184fa360b7625226 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Fri, 18 Dec 2020 20:11:55 -0500 Subject: [PATCH] Add support for disabling short tags --- action.yml | 4 ++++ dist/index.js | 6 ++++-- index.js | 6 ++++-- index.test.js | 18 +++++++++++++++++- readme.md | 5 +++++ 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index f0e9871..f8a0c32 100644 --- a/action.yml +++ b/action.yml @@ -30,6 +30,10 @@ inputs: namespace: description: "Use to create a named sub-version. This value will be appended to tags created for this version." required: false + short_tags: + description: "If false, only full versions, i.e. 'v1.0.0', will be supported as tags. If true, tags will support truncated minor and patch versions such as 'v1' (default)." + required: true + default: "true" outputs: major: description: "Current major number" diff --git a/dist/index.js b/dist/index.js index 6be6eeb..e475104 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1025,6 +1025,7 @@ const eol = '\n'; const tagPrefix = core.getInput('tag_prefix') || ''; const namespace = core.getInput('namespace') || ''; +const shortTags = core.getInput('short_tags') === 'true'; const cmd = async (command, ...args) => { let output = '', errors = ''; @@ -1061,7 +1062,7 @@ const setOutput = (major, minor, patch, increment, changed, branch, namespace) = } let tag; - if (major === 0 || patch !== 0) { + if (!shortTags || major === 0 || patch !== 0) { // Always tag pre-release/major version 0 as full version tag = `${tagPrefix}${major}.${minor}.${patch}`; } else if (minor !== 0) { @@ -1122,7 +1123,8 @@ async function run() { const minorPattern = core.getInput('minor_pattern', { required: true }); const changePath = core.getInput('change_path') || ''; - const releasePattern = namespace === '' ? `${tagPrefix}*[0-9.]` : `${tagPrefix}*[0-9.]-${namespace}`; + const versionPattern = shortTags ? '*[0-9.]' : '[0-9]+\\.[0-9]+\\.[0-9]+' + const releasePattern = namespace === '' ? `${tagPrefix}${versionPattern}` : `${tagPrefix}${versionPattern}-${namespace}`; let major = 0, minor = 0, patch = 0, increment = 0; let changed = true; diff --git a/index.js b/index.js index 835632a..4ece3c0 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const eol = '\n'; const tagPrefix = core.getInput('tag_prefix') || ''; const namespace = core.getInput('namespace') || ''; +const shortTags = core.getInput('short_tags') === 'true'; const cmd = async (command, ...args) => { let output = '', errors = ''; @@ -40,7 +41,7 @@ const setOutput = (major, minor, patch, increment, changed, branch, namespace) = } let tag; - if (major === 0 || patch !== 0) { + if (!shortTags || major === 0 || patch !== 0) { // Always tag pre-release/major version 0 as full version tag = `${tagPrefix}${major}.${minor}.${patch}`; } else if (minor !== 0) { @@ -101,7 +102,8 @@ async function run() { const minorPattern = core.getInput('minor_pattern', { required: true }); const changePath = core.getInput('change_path') || ''; - const releasePattern = namespace === '' ? `${tagPrefix}*[0-9.]` : `${tagPrefix}*[0-9.]-${namespace}`; + const versionPattern = shortTags ? '*[0-9.]' : '[0-9]+\\.[0-9]+\\.[0-9]+' + const releasePattern = namespace === '' ? `${tagPrefix}${versionPattern}` : `${tagPrefix}${versionPattern}-${namespace}`; let major = 0, minor = 0, patch = 0, increment = 0; let changed = true; diff --git a/index.test.js b/index.test.js index 027a189..b0b8ff5 100644 --- a/index.test.js +++ b/index.test.js @@ -10,7 +10,8 @@ const defaultInputs = { tag_prefix: "v", major_pattern: "(MAJOR)", minor_pattern: "(MINOR)", - format: "${major}.${minor}.${patch}" + format: "${major}.${minor}.${patch}", + short_tags: true }; // Creates a randomly named git repository and returns a function to execute commands in it @@ -445,5 +446,20 @@ test('Current tag is used', () => { expect(result).toMatch('Version is 7.6.5+0'); + repo.clean(); +}); + +test('Short tag can be switched off', () => { + const repo = createTestRepo({ tag_prefix: '', short_tags: 'false' }); // 0.0.0 + + repo.makeCommit('Initial Commit'); + repo.makeCommit('Second Commit'); + repo.makeCommit('Third Commit'); + repo.exec('git tag 7'); + + const result = repo.runAction(); + + expect(result).toMatch('Version is 0.0.1+2'); + repo.clean(); }); \ No newline at end of file diff --git a/readme.md b/readme.md index 5895702..5b7e140 100644 --- a/readme.md +++ b/readme.md @@ -66,6 +66,8 @@ it will be given the new version if the build were to be retriggered, for exampl ```yaml - uses: paulhatch/semantic-version@v3.1.2 with: + # The prefix to use to identify tags + branch: "master" # 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 @@ -80,6 +82,9 @@ it will be given the new version if the build were to be retriggered, for exampl change_path: "src/my-service" # Named version, will be used as suffix for name version tag namespace: project-b + # Indicate whether short tags like 'v1' should be supported. If false only full + # tags like 'v1.0.0' will be recognized. + short_tags: true ``` ## Using Multiple Versions in the Same Repository