From 858da7b39c5e1cc0e80ca5b2e2beea36cfb312fb Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Tue, 6 Oct 2020 08:34:17 -0400 Subject: [PATCH 01/14] Run test action for Windows in addition to Linux --- .github/workflows/test.yml | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 94599fc..0008655 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -4,15 +4,34 @@ on: push: branches: - master - - "releases/*" + - "feature/*" jobs: - test: - name: "Test and Run" + test-linux: + name: "Test and Run, Linux" runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v1 + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: NPM Install + run: npm ci + - name: Test + run: npm test + - name: Package + run: npm run package + - name: Run Action + uses: ./ + id: run + test-windows: + name: "Test and Run, Windows" + runs-on: windows-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 - name: NPM Install run: npm ci - name: Test From 596215aec56c4ecbba5bfb5e870d53d82ddd6b7a Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Tue, 6 Oct 2020 08:35:24 -0400 Subject: [PATCH 02/14] Use quoted matching, do not use exec's args array --- dist/index.js | 4 ++-- index.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 4b71d02..2bf9bd5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1035,7 +1035,7 @@ const cmd = async (command, ...args) => { stderr: (data) => { errors += data.toString(); } }; - await exec.exec(command, args, options) + await exec.exec(command + args.map(a => ` ${a}`).join(' '), [], options) .catch(err => { core.info(`The command '${command} ${args.join(' ')}' failed: ${err}`); }); if (errors !== '') { @@ -1121,7 +1121,7 @@ async function run() { `describe`, `--tags`, `--abbrev=0`, - `--match=${releasePattern}`, + `--match="${releasePattern}"`, `${branch}~1` )).trim(); } diff --git a/index.js b/index.js index 29a6684..7250f2f 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ const cmd = async (command, ...args) => { stderr: (data) => { errors += data.toString(); } }; - await exec.exec(command, args, options) + await exec.exec(command + args.map(a => ` ${a}`).join(' '), [], options) .catch(err => { core.info(`The command '${command} ${args.join(' ')}' failed: ${err}`); }); if (errors !== '') { @@ -100,7 +100,7 @@ async function run() { `describe`, `--tags`, `--abbrev=0`, - `--match=${releasePattern}`, + `--match="${releasePattern}"`, `${branch}~1` )).trim(); } From 800ea493f76c983a6bddca7dd81c1cb4a0d33650 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Tue, 6 Oct 2020 08:40:21 -0400 Subject: [PATCH 03/14] Split commands so that tests can run on Windows --- index.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.test.js b/index.test.js index c596113..31ed4c0 100644 --- a/index.test.js +++ b/index.test.js @@ -14,7 +14,8 @@ const defaultInputs = { // Creates a randomly named git repository and returns a function to execute commands in it const createTestRepo = (inputs) => { const repoDirectory = `/tmp/test${Math.random().toString(36).substring(2, 15)}`; - cp.execSync(`mkdir ${repoDirectory} && git init ${repoDirectory}`); + cp.execSync(`mkdir ${repoDirectory}`); + cp.execSync(`git init ${repoDirectory}`); const run = (command, extraInputs) => { const allInputs = Object.assign({ ...defaultInputs }, inputs, extraInputs); From bcb0692ebb1bcc8720d441c34059af1edffe4769 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Tue, 6 Oct 2020 20:45:54 -0400 Subject: [PATCH 04/14] Use OS specific temp folder for test repos --- index.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.test.js b/index.test.js index 31ed4c0..662133e 100644 --- a/index.test.js +++ b/index.test.js @@ -1,6 +1,7 @@ const cp = require('child_process'); const path = require('path'); const process = require('process'); +const os = require('os'); // Action input variables const defaultInputs = { @@ -13,7 +14,7 @@ const defaultInputs = { // Creates a randomly named git repository and returns a function to execute commands in it const createTestRepo = (inputs) => { - const repoDirectory = `/tmp/test${Math.random().toString(36).substring(2, 15)}`; + const repoDirectory = path.join(os.tmpdir(), `test${Math.random().toString(36).substring(2, 15)}`); cp.execSync(`mkdir ${repoDirectory}`); cp.execSync(`git init ${repoDirectory}`); From 0383dec6433c4169251c4a24029e42439717c1c1 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Tue, 6 Oct 2020 20:50:09 -0400 Subject: [PATCH 05/14] Use double quote for test commit messages --- index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.test.js b/index.test.js index 662133e..29fefbd 100644 --- a/index.test.js +++ b/index.test.js @@ -38,7 +38,7 @@ const createTestRepo = (inputs) => { makeCommit: (msg, path) => { run(`touch ${path !== undefined ? path.trim('/') + '/' : ''}test${i++}`); run(`git add --all`); - run(`git commit -m '${msg}'`); + run(`git commit -m "${msg}"`); }, runAction: (inputs) => run(`node ${path.join(__dirname, 'index.js')}`, inputs), exec: run From 48bb665d5ce2a1746df0a3eac01f286172f8355f Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Tue, 6 Oct 2020 20:56:32 -0400 Subject: [PATCH 06/14] Remove quotes for match value --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7250f2f..4c17069 100644 --- a/index.js +++ b/index.js @@ -100,7 +100,7 @@ async function run() { `describe`, `--tags`, `--abbrev=0`, - `--match="${releasePattern}"`, + `--match=${releasePattern}`, `${branch}~1` )).trim(); } From ae095094f732a512f67dcd2455e3bfb0014f902c Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Tue, 6 Oct 2020 21:29:12 -0400 Subject: [PATCH 07/14] Revert args mode --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 4c17069..29a6684 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ const cmd = async (command, ...args) => { stderr: (data) => { errors += data.toString(); } }; - await exec.exec(command + args.map(a => ` ${a}`).join(' '), [], options) + await exec.exec(command, args, options) .catch(err => { core.info(`The command '${command} ${args.join(' ')}' failed: ${err}`); }); if (errors !== '') { From 8ce630d670bf289a422a0aa4184604d2967d85e5 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Wed, 7 Oct 2020 07:52:11 -0400 Subject: [PATCH 08/14] Cleanup using os temp path --- index.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.test.js b/index.test.js index 29fefbd..d2e7ad8 100644 --- a/index.test.js +++ b/index.test.js @@ -34,7 +34,7 @@ const createTestRepo = (inputs) => { let i = 1; return { - clean: () => execute('/tmp', `rm -rf ${repoDirectory}`), + clean: () => execute(os.tmpdir(), `rm -rf ${repoDirectory}`), makeCommit: (msg, path) => { run(`touch ${path !== undefined ? path.trim('/') + '/' : ''}test${i++}`); run(`git add --all`); From 866aab0885444d92cc4728dbc19c55f98b3b3637 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Wed, 7 Oct 2020 08:02:42 -0400 Subject: [PATCH 09/14] Ignore exec failures (these are handled in code) --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 29a6684..26b9f5a 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,8 @@ const cmd = async (command, ...args) => { }; options.listeners = { stdout: (data) => { output += data.toString(); }, - stderr: (data) => { errors += data.toString(); } + stderr: (data) => { errors += data.toString(); }, + ignoreReturnCode: true }; await exec.exec(command, args, options) From 4160681a29f437220d973fb33eb9cf5115287f97 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Wed, 7 Oct 2020 08:38:48 -0400 Subject: [PATCH 10/14] Run commands sliently --- index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 26b9f5a..1f3d36c 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,8 @@ const cmd = async (command, ...args) => { options.listeners = { stdout: (data) => { output += data.toString(); }, stderr: (data) => { errors += data.toString(); }, - ignoreReturnCode: true + ignoreReturnCode: true, + silent: true }; await exec.exec(command, args, options) @@ -104,6 +105,17 @@ async function run() { `--match=${releasePattern}`, `${branch}~1` )).trim(); + + if (tag === '') { + tag = (await cmd( + 'git', + `tag`, + `--list ${releasePattern}`, + `--points-at ${branch}` + )).trim(); + } + + } catch (err) { tag = ''; From 5c67e25f43ae1614dd8ecbe2d1c6494d0ab2878d Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Wed, 7 Oct 2020 18:26:03 -0400 Subject: [PATCH 11/14] Use the current tag, if present, for version --- dist/index.js | 55 +++++++++++++++++++++++++++++++----------------- index.js | 58 +++++++++++++++++++++++++++------------------------ index.test.js | 17 ++++++++++++++- 3 files changed, 83 insertions(+), 47 deletions(-) diff --git a/dist/index.js b/dist/index.js index 2bf9bd5..e9c3c01 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1024,6 +1024,7 @@ const exec = __webpack_require__(986); const eol = __webpack_require__(87).EOL; const tagPrefix = core.getInput('tag_prefix') || ''; +const namespace = core.getInput('namespace') || ''; const cmd = async (command, ...args) => { let output = '', errors = ''; @@ -1032,10 +1033,12 @@ const cmd = async (command, ...args) => { }; options.listeners = { stdout: (data) => { output += data.toString(); }, - stderr: (data) => { errors += data.toString(); } + stderr: (data) => { errors += data.toString(); }, + ignoreReturnCode: true, + silent: true }; - await exec.exec(command + args.map(a => ` ${a}`).join(' '), [], options) + await exec.exec(command, args, options) .catch(err => { core.info(`The command '${command} ${args.join(' ')}' failed: ${err}`); }); if (errors !== '') { @@ -1088,6 +1091,26 @@ const setOutput = (major, minor, patch, increment, changed, branch, namespace) = }; +const parseVersion = (tag) => { + + console.log(tag); + let tagParts = tag.split('/'); + let versionValues = tagParts[tagParts.length - 1] + .substr(tagPrefix.length) + .slice(0, namespace === '' ? 999 : -(namespace.length + 1)) + .split('.'); + + let major = parseInt(versionValues[0]); + let minor = versionValues.length > 1 ? parseInt(versionValues[1]) : 0; + let patch = versionValues.length > 2 ? parseInt(versionValues[2]) : 0; + + if (isNaN(major) || isNaN(minor) || isNaN(patch)) { + throw `Invalid tag ${tag} (${versionValues})`; + } + + return [major, minor, patch]; +}; + async function run() { try { const remote = await cmd('git', 'remote'); @@ -1098,7 +1121,6 @@ async function run() { const majorPattern = core.getInput('major_pattern', { required: true }); const minorPattern = core.getInput('minor_pattern', { required: true }); const changePath = core.getInput('change_path') || ''; - const namespace = core.getInput('namespace') || ''; const releasePattern = namespace === '' ? `${tagPrefix}*[0-9.]` : `${tagPrefix}*[0-9.]-${namespace}`; let major = 0, minor = 0, patch = 0, increment = 0; @@ -1112,7 +1134,14 @@ async function run() { return; } - //let commit = (await cmd('git', 'rev-parse', 'HEAD')).trim(); + let currentTag = (await cmd( + `git tag --points-at ${branch} ${releasePattern}` + )).trim(); + + if (currentTag) { + [major, minor, patch] = parseVersion(currentTag); + setOutput(major, minor, patch, 0, false, branch, namespace); + } let tag = ''; try { @@ -1121,7 +1150,7 @@ async function run() { `describe`, `--tags`, `--abbrev=0`, - `--match="${releasePattern}"`, + `--match=${releasePattern}`, `${branch}~1` )).trim(); } @@ -1138,20 +1167,7 @@ async function run() { root = ''; } else { // parse the version tag - let tagParts = tag.split('/'); - let versionValues = tagParts[tagParts.length - 1] - .substr(tagPrefix.length) - .slice(0, namespace === '' ? 999 : -(namespace.length + 1)) - .split('.'); - - 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)) { - core.setFailed(`Invalid tag ${tag} (${versionValues})`); - return; - } + [major, minor, patch] = parseVersion(tag); root = await cmd('git', `merge-base`, tag, branch); } @@ -1211,6 +1227,7 @@ async function run() { run(); + /***/ }), /***/ 129: diff --git a/index.js b/index.js index 1f3d36c..59a9811 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const exec = require("@actions/exec"); const eol = require('os').EOL; const tagPrefix = core.getInput('tag_prefix') || ''; +const namespace = core.getInput('namespace') || ''; const cmd = async (command, ...args) => { let output = '', errors = ''; @@ -69,6 +70,26 @@ const setOutput = (major, minor, patch, increment, changed, branch, namespace) = }; +const parseVersion = (tag) => { + + console.log(tag); + let tagParts = tag.split('/'); + let versionValues = tagParts[tagParts.length - 1] + .substr(tagPrefix.length) + .slice(0, namespace === '' ? 999 : -(namespace.length + 1)) + .split('.'); + + let major = parseInt(versionValues[0]); + let minor = versionValues.length > 1 ? parseInt(versionValues[1]) : 0; + let patch = versionValues.length > 2 ? parseInt(versionValues[2]) : 0; + + if (isNaN(major) || isNaN(minor) || isNaN(patch)) { + throw `Invalid tag ${tag} (${versionValues})`; + } + + return [major, minor, patch]; +}; + async function run() { try { const remote = await cmd('git', 'remote'); @@ -79,7 +100,6 @@ async function run() { const majorPattern = core.getInput('major_pattern', { required: true }); const minorPattern = core.getInput('minor_pattern', { required: true }); const changePath = core.getInput('change_path') || ''; - const namespace = core.getInput('namespace') || ''; const releasePattern = namespace === '' ? `${tagPrefix}*[0-9.]` : `${tagPrefix}*[0-9.]-${namespace}`; let major = 0, minor = 0, patch = 0, increment = 0; @@ -93,7 +113,14 @@ async function run() { return; } - //let commit = (await cmd('git', 'rev-parse', 'HEAD')).trim(); + let currentTag = (await cmd( + `git tag --points-at ${branch} ${releasePattern}` + )).trim(); + + if (currentTag) { + [major, minor, patch] = parseVersion(currentTag); + setOutput(major, minor, patch, 0, false, branch, namespace); + } let tag = ''; try { @@ -105,17 +132,6 @@ async function run() { `--match=${releasePattern}`, `${branch}~1` )).trim(); - - if (tag === '') { - tag = (await cmd( - 'git', - `tag`, - `--list ${releasePattern}`, - `--points-at ${branch}` - )).trim(); - } - - } catch (err) { tag = ''; @@ -130,20 +146,7 @@ async function run() { root = ''; } else { // parse the version tag - let tagParts = tag.split('/'); - let versionValues = tagParts[tagParts.length - 1] - .substr(tagPrefix.length) - .slice(0, namespace === '' ? 999 : -(namespace.length + 1)) - .split('.'); - - 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)) { - core.setFailed(`Invalid tag ${tag} (${versionValues})`); - return; - } + [major, minor, patch] = parseVersion(tag); root = await cmd('git', `merge-base`, tag, branch); } @@ -201,3 +204,4 @@ async function run() { } run(); + diff --git a/index.test.js b/index.test.js index d2e7ad8..9f801db 100644 --- a/index.test.js +++ b/index.test.js @@ -86,7 +86,7 @@ test('Tagging does not break version', () => { repo.exec('git tag v0.0.1') const result = repo.runAction(); - expect(result).toMatch('Version is 0.0.1+1'); + expect(result).toMatch('Version is 0.0.1+0'); repo.clean(); }); @@ -424,5 +424,20 @@ test('Commits inside path are counted', () => { expect(result).toMatch('Version is 0.0.1+2'); + repo.clean(); +}); + +test('Current tag is used', () => { + const repo = createTestRepo({ tag_prefix: '' }); // 0.0.0 + + repo.makeCommit('Initial Commit'); + repo.makeCommit('Second Commit'); + repo.makeCommit('Third Commit'); + repo.exec('git tag 7.6.5'); + + const result = repo.runAction(); + + expect(result).toMatch('Version is 7.6.5+0'); + repo.clean(); }); \ No newline at end of file From 65140edf778e3dc8f0237d3194b676979f3e8a2b Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Wed, 7 Oct 2020 18:33:21 -0400 Subject: [PATCH 12/14] Update test to support Windows --- index.test.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.test.js b/index.test.js index 9f801db..54a451b 100644 --- a/index.test.js +++ b/index.test.js @@ -36,7 +36,11 @@ const createTestRepo = (inputs) => { return { clean: () => execute(os.tmpdir(), `rm -rf ${repoDirectory}`), makeCommit: (msg, path) => { - run(`touch ${path !== undefined ? path.trim('/') + '/' : ''}test${i++}`); + if (process.platform === "win32") { + run(`fsutil file createnew ${path !== undefined ? path.trim('/') + '/' : ''}test${i++} 0`); + } else { + run(`touch ${path !== undefined ? path.trim('/') + '/' : ''}test${i++}`); + } run(`git add --all`); run(`git commit -m "${msg}"`); }, From b38e4671e9a4910de3a3dd65fcd737c56412fc76 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Wed, 7 Oct 2020 19:36:05 -0400 Subject: [PATCH 13/14] Windows support, history results always use \n --- dist/index.js | 2 +- index.js | 2 +- index.test.js | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index e9c3c01..4b916cf 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1021,7 +1021,7 @@ exports.issueCommand = issueCommand; const core = __webpack_require__(470); const exec = __webpack_require__(986); -const eol = __webpack_require__(87).EOL; +const eol = '\n'; const tagPrefix = core.getInput('tag_prefix') || ''; const namespace = core.getInput('namespace') || ''; diff --git a/index.js b/index.js index 59a9811..c5695c8 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ const core = require('@actions/core'); const exec = require("@actions/exec"); -const eol = require('os').EOL; +const eol = '\n'; const tagPrefix = core.getInput('tag_prefix') || ''; const namespace = core.getInput('namespace') || ''; diff --git a/index.test.js b/index.test.js index 54a451b..027a189 100644 --- a/index.test.js +++ b/index.test.js @@ -2,6 +2,7 @@ const cp = require('child_process'); const path = require('path'); const process = require('process'); const os = require('os'); +const windows = process.platform === "win32"; // Action input variables const defaultInputs = { @@ -34,9 +35,9 @@ const createTestRepo = (inputs) => { let i = 1; return { - clean: () => execute(os.tmpdir(), `rm -rf ${repoDirectory}`), + clean: () => execute(os.tmpdir(), windows ? `rmdir /s /q ${repoDirectory}` : `rm -rf ${repoDirectory}`), makeCommit: (msg, path) => { - if (process.platform === "win32") { + if (windows) { run(`fsutil file createnew ${path !== undefined ? path.trim('/') + '/' : ''}test${i++} 0`); } else { run(`touch ${path !== undefined ? path.trim('/') + '/' : ''}test${i++}`); @@ -282,7 +283,8 @@ test('Tag order comes from commit order, not tag create order', () => { repo.makeCommit('Second Commit'); // 0.0.1+1 repo.makeCommit('Third Commit'); // 0.0.1+2 repo.exec('git tag v2.0.0'); - repo.exec('sleep 2'); + // Can't timeout in this context on Windows, ping localhost to delay + repo.exec(windows ? 'ping 127.0.0.1 -n 2' : 'sleep 2'); repo.exec('git tag v1.0.0 HEAD~1'); repo.makeCommit('Fourth Commit'); // 0.0.1+2 From ebcb352d0b94905f5da69e51cfbbc361592b5719 Mon Sep 17 00:00:00 2001 From: Paul Hatcherian <1835615+PaulHatch@users.noreply.github.com> Date: Wed, 7 Oct 2020 19:46:11 -0400 Subject: [PATCH 14/14] Fix, exit when current commit tag found --- dist/index.js | 1 + index.js | 1 + 2 files changed, 2 insertions(+) diff --git a/dist/index.js b/dist/index.js index 4b916cf..6be6eeb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1141,6 +1141,7 @@ async function run() { if (currentTag) { [major, minor, patch] = parseVersion(currentTag); setOutput(major, minor, patch, 0, false, branch, namespace); + return; } let tag = ''; diff --git a/index.js b/index.js index c5695c8..835632a 100644 --- a/index.js +++ b/index.js @@ -120,6 +120,7 @@ async function run() { if (currentTag) { [major, minor, patch] = parseVersion(currentTag); setOutput(major, minor, patch, 0, false, branch, namespace); + return; } let tag = '';