From ac2e147b3551b543620bcca5cde98e2e9da71263 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Sat, 6 Feb 2021 12:35:25 -0500 Subject: [PATCH 1/2] Support and default to HEAD ref for branch (MAJOR) --- action.yml | 4 ++-- dist/index.js | 10 +++++++++- index.js | 6 +++++- index.test.js | 16 ++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/action.yml b/action.yml index fb67ceb..19a4173 100644 --- a/action.yml +++ b/action.yml @@ -5,9 +5,9 @@ branding: color: "blue" inputs: branch: - description: "The branch name" + description: "Set to specify a specific branch, default is the current HEAD" required: true - default: "master" + default: "HEAD" tag_prefix: description: "The prefix to use to identify tags" required: false diff --git a/dist/index.js b/dist/index.js index 044ada7..13419e9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1072,6 +1072,10 @@ const setOutput = (major, minor, patch, increment, changed, branch, namespace) = tag = `${tagPrefix}${major}`; } + if (namespace !== '') { + tag += `-${namespace}` + } + const repository = process.env.GITHUB_REPOSITORY; if (!changed) { @@ -1130,11 +1134,15 @@ async function run() { const remoteExists = remote !== ''; const remotePrefix = remoteExists ? 'origin/' : ''; - const branch = `${remotePrefix}${core.getInput('branch', { required: true })}`; + let branch = `${remotePrefix}${core.getInput('branch', { 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') || ''; + if (branch === 'HEAD') { + branch = (await cmd('git', 'rev-parse', 'HEAD')).trim(); + } + 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; diff --git a/index.js b/index.js index 1acd438..34ddeb0 100644 --- a/index.js +++ b/index.js @@ -113,11 +113,15 @@ async function run() { const remoteExists = remote !== ''; const remotePrefix = remoteExists ? 'origin/' : ''; - const branch = `${remotePrefix}${core.getInput('branch', { required: true })}`; + let branch = `${remotePrefix}${core.getInput('branch', { 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') || ''; + if (branch === 'HEAD') { + branch = (await cmd('git', 'rev-parse', 'HEAD')).trim(); + } + 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; diff --git a/index.test.js b/index.test.js index 63f2717..a922661 100644 --- a/index.test.js +++ b/index.test.js @@ -85,6 +85,22 @@ test('Repository with commits shows increment', () => { repo.clean(); }); +test('Repository show commit for checked out commit', () => { + const repo = createTestRepo({ branch: 'HEAD' }); // 0.0.0+0 + + repo.makeCommit('Initial Commit'); // 0.0.1+0 + repo.makeCommit(`Second Commit`); // 0.0.1+1 + let result = repo.runAction(); + expect(result).toMatch('Version is 0.0.1+1'); + + repo.exec(`git checkout HEAD~1`); // 0.0.1+1 + result = repo.runAction(); + expect(result).toMatch('Version is 0.0.1+0'); + + + repo.clean(); +}); + test('Tagging does not break version', () => { const repo = createTestRepo(); // 0.0.0+0 From 01ccd0e687639e2f7840af8fc4baa5adc4472d48 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Mon, 8 Feb 2021 20:52:41 -0500 Subject: [PATCH 2/2] Remove origin prefix from branch name --- dist/index.js | 8 ++------ index.js | 8 ++------ index.test.js | 4 ++-- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/dist/index.js b/dist/index.js index 13419e9..b2acdaf 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1130,11 +1130,7 @@ const createMatchTest = (pattern) => { async function run() { try { - const remote = await cmd('git', 'remote'); - const remoteExists = remote !== ''; - const remotePrefix = remoteExists ? 'origin/' : ''; - - let branch = `${remotePrefix}${core.getInput('branch', { required: true })}`; + let branch = core.getInput('branch', { 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') || ''; @@ -1177,7 +1173,7 @@ async function run() { let root; if (tag === '') { - if (remoteExists) { + if (await cmd('git', 'remote') !== '') { core.warning('No tags are present for this repository. If this is unexpected, check to ensure that tags have been pulled from the remote.'); } // no release tags yet, use the initial commit as the root diff --git a/index.js b/index.js index 34ddeb0..16489d2 100644 --- a/index.js +++ b/index.js @@ -109,11 +109,7 @@ const createMatchTest = (pattern) => { async function run() { try { - const remote = await cmd('git', 'remote'); - const remoteExists = remote !== ''; - const remotePrefix = remoteExists ? 'origin/' : ''; - - let branch = `${remotePrefix}${core.getInput('branch', { required: true })}`; + let branch = core.getInput('branch', { 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') || ''; @@ -156,7 +152,7 @@ async function run() { let root; if (tag === '') { - if (remoteExists) { + if (await cmd('git', 'remote') !== '') { core.warning('No tags are present for this repository. If this is unexpected, check to ensure that tags have been pulled from the remote.'); } // no release tags yet, use the initial commit as the root diff --git a/index.test.js b/index.test.js index a922661..55d7371 100644 --- a/index.test.js +++ b/index.test.js @@ -6,7 +6,7 @@ const windows = process.platform === "win32"; // Action input variables const defaultInputs = { - branch: "master", + branch: "HEAD", tag_prefix: "v", major_pattern: "(MAJOR)", minor_pattern: "(MINOR)", @@ -86,7 +86,7 @@ test('Repository with commits shows increment', () => { }); test('Repository show commit for checked out commit', () => { - const repo = createTestRepo({ branch: 'HEAD' }); // 0.0.0+0 + const repo = createTestRepo(); // 0.0.0+0 repo.makeCommit('Initial Commit'); // 0.0.1+0 repo.makeCommit(`Second Commit`); // 0.0.1+1