Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
feat(action): strip component prefix from tag (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrogr authored Nov 16, 2021
1 parent c5b0108 commit 637ccd9
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -34,6 +35,7 @@ try {
currentMajor,
preReleaseName,
updateVersionsIn,
stripComponentPrefixFromTag,
commitMessage,
commitAuthor,
commitAuthorEmail,
Expand Down
11 changes: 9 additions & 2 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async function run(
currentMajor,
preReleaseName,
updateVersionsIn,
stripComponentPrefixFromTag,
commitMessage,
commitAuthor,
commitAuthorEmail,
Expand All @@ -49,6 +50,7 @@ async function run(
currentMajor,
preReleaseName,
updateVersionsIn,
stripComponentPrefixFromTag,
commitMessage,
commitAuthor,
commitAuthorEmail,
Expand Down Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down
89 changes: 89 additions & 0 deletions src/run.test.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -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);
});
});

0 comments on commit 637ccd9

Please sign in to comment.