From 637ccd91a385e79fd07ae339b695e64e67c067d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= Date: Tue, 16 Nov 2021 13:46:22 +0100 Subject: [PATCH] feat(action): strip component prefix from tag (#7) --- action.yml | 4 +++ src/index.js | 2 ++ src/run.js | 11 ++++-- src/run.test.js | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index 6f91fb9..11c3bb9 100644 --- a/action.yml +++ b/action.yml @@ -36,6 +36,10 @@ inputs: description: 'String representation of an array of objects with file/property keys. If provided the action will update those files/properties with the version that is being released.' required: false default: false + strip-component-from-tag: + description: 'If component prefix should be removed from the tag when returning it or using it in a version file' + required: false + default: false commit-message: description: 'The message in the commit for version updates.' required: false diff --git a/src/index.js b/src/index.js index 1dc71fa..c190a0d 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,7 @@ const tagBranch = core.getInput('tag-branch'); const currentComponentTag = core.getInput('current-tag'); const currentMajor = core.getInput('current-major'); const updateVersionsIn = core.getInput('update-versions-in'); +const stripComponentPrefixFromTag = core.getInput('strip-component-from-tag'); const commitMessage = core.getInput('commit-message'); const commitAuthor = core.getInput('commit-author'); const commitAuthorEmail = core.getInput('commit-author-email'); @@ -34,6 +35,7 @@ try { currentMajor, preReleaseName, updateVersionsIn, + stripComponentPrefixFromTag, commitMessage, commitAuthor, commitAuthorEmail, diff --git a/src/run.js b/src/run.js index a532850..d2381fa 100644 --- a/src/run.js +++ b/src/run.js @@ -25,6 +25,7 @@ async function run( currentMajor, preReleaseName, updateVersionsIn, + stripComponentPrefixFromTag, commitMessage, commitAuthor, commitAuthorEmail, @@ -49,6 +50,7 @@ async function run( currentMajor, preReleaseName, updateVersionsIn, + stripComponentPrefixFromTag, commitMessage, commitAuthor, commitAuthorEmail, @@ -94,12 +96,17 @@ async function run( return core.setFailed('Tag creation failed'); } + let effectiveTag = tag; + if (stripComponentPrefixFromTag) { + effectiveTag = tag.replace(componentPrefix, ''); + } + if (!dryRun) { // update version filess before the tag is made if (updateVersionsIn != false) { await versionFileUpdater.updateVersionInFileAndCommit( updateVersionsIn, - tag, + effectiveTag, branchToTag, commitMessage, commitAuthor, @@ -112,7 +119,7 @@ async function run( console.log(`🚀 New tag '${tag}' created in ${branchToTag}`); } - core.setOutput('tag', tag); + core.setOutput('tag', effectiveTag); } module.exports = { diff --git a/src/run.test.js b/src/run.test.js index 97e9de4..b305254 100644 --- a/src/run.test.js +++ b/src/run.test.js @@ -1,5 +1,7 @@ const core = require('@actions/core'); const github = require('@actions/github'); +const fs = require('fs'); +const actions = require('@actions/exec'); const { run } = require('./run'); @@ -200,3 +202,90 @@ describe('mode product', () => { expect(core.setOutput).toHaveBeenCalledWith('tag', 'v0.23.2'); }); }); + +describe('version file updater', () => { + let octokitMock, owner, repo; + + const params = { + componentPrefix: 'hello-', + mode: 'component', + type: 'final', + tagBranch: 'main', + dryRun: false, + updateVersionsIn: '[{"file": "test/file.yaml", "property": "app.tag" }]', + stripComponentPrefixFromTag: true, + }; + + beforeEach(() => { + [owner, repo] = 'test-org/test-repo'.split('/'); + + octokitMock = { + repos: { + listTags: jest.fn().mockReturnValue({ + data: [{ name: 'hello-v0.97.0' }], + }), + getBranch: jest.fn().mockReturnValue({ + data: { + name: 'main', + commit: { + sha: 'sha1234', + }, + }, + }), + }, + git: { + createTag: jest.fn().mockReturnValue({ data: { sha: 'sha5678' } }), + createRef: jest.fn().mockReturnValue({ data: { sha: 'ref12345' } }), + }, + }; + + fs.writeFile = jest.fn(); + actions.exec = jest.fn(); + }); + + test('component prefix is stripped from version file', async () => { + // GIVEN a version and a component prefix + const version = 'v0.99.0'; + const expectedVersion = 'v0.100.0'; + const componentPrefix = 'hello-'; + octokitMock.repos.listTags = jest.fn().mockReturnValue({ + data: [{ name: componentPrefix.concat(version) }], + }); + // AND we WANT to strip the component prefix from the tag + params.stripComponentPrefixFromTag = true; + + // WHEN the updater is executed + await run(octokitMock, owner, repo, params); + + // THEN version file was written + expect(fs.writeFile).toHaveBeenCalledTimes(1); + + // AND the component prefix in the version file was stripped + const updatedContent = fs.writeFile.mock.calls[0][1]; + expect(updatedContent).toContain(expectedVersion); + expect(updatedContent).not.toContain(componentPrefix); + }); + + test('component prefix is not stripped from version file', async () => { + // GIVEN a version and a component prefix + const version = 'v0.99.0'; + const expectedVersion = 'v0.100.0'; + const componentPrefix = 'hello-'; + octokitMock.repos.listTags = jest.fn().mockReturnValue({ + data: [{ name: componentPrefix.concat(version) }], + }); + // AND we DO NOT WANT to strip the component prefix from the tag + params.stripComponentPrefixFromTag = false; + + // WHEN we run the action + await run(octokitMock, owner, repo, params); + + // THEN version file was written + expect(fs.writeFile).toHaveBeenCalledTimes(1); + + // AND the component prefix in the version file was NOT stripped + const updatedContent = fs.writeFile.mock.calls[0][1]; + expect(updatedContent).toContain(expectedVersion); + expect(updatedContent).toContain(componentPrefix); + }); +});