Skip to content

Commit

Permalink
(mathieudutour#145) add default_draft_bump to stay backward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-hartmond-mw committed Apr 21, 2023
1 parent d87bb15 commit 32ca813
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ jobs:

- **default_bump** _(optional)_ - Which type of bump to use when [none is explicitly provided](#bumping) when commiting to a release branch (default: `patch`). You can also set `false` to avoid generating a new tag when none is explicitly provided. Can be `patch, minor or major`.
- **default_prerelease_bump** _(optional)_ - Which type of bump to use when [none is explicitly provided](#bumping) when commiting to a prerelease branch (default: `prerelease`). You can also set `false` to avoid generating a new tag when none is explicitly provided. Can be `prerelease, prepatch, preminor or premajor`.
- **default_draft_bump** _(optional)_ - Which type of bump to use when [none is explicitly provided](#bumping) when commiting to a prerelease branch with no previous prerelease version (default: `prerelease`). You can also set `false` to avoid generating a new tag when none is explicitly provided. Can be `prerelease, patch, minor or major`.
- **custom_tag** _(optional)_ - Custom tag name. If specified, it overrides bump settings.
- **create_annotated_tag** _(optional)_ - Boolean to create an annotated rather than a lightweight one (default: `false`).
- **tag_prefix** _(optional)_ - A prefix to the tag name (default: `v`).
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ inputs:
description: "Which type of bump to use when none explicitly provided when commiting to a release branch (default: `patch`)."
required: false
default: "patch"
default_draft_bump:
description: |
Which type of bump to use when none explicitly provided when commiting to a prerelease branch with no previous prerelease version (default: same as `default_prerelease_bump`).
Defaults to prerelease for backward-compatibility with previous behaviour. Might make more sense to set it to the same as `default_bump`.
required: false
default_prerelease_bump:
description: "Which type of bump to use when none explicitly provided when commiting to a prerelease branch (default: `prerelease`)."
required: false
Expand Down
8 changes: 6 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default async function main() {
const defaultPreReleaseBump = core.getInput('default_prerelease_bump') as
| ReleaseType
| 'false';
const defaultDraftBump = core.getInput('default_draft_bump') as ReleaseType | 'false' || defaultPreReleaseBump;
const tagPrefix = core.getInput('tag_prefix');
const customTag = core.getInput('custom_tag');
const releaseBranches = core.getInput('release_branches');
Expand Down Expand Up @@ -120,6 +121,7 @@ export default async function main() {
);
core.setOutput('previous_version', previousVersion.version);
core.setOutput('previous_tag', previousTag.name);
const previousWasPrerelease = previousVersion.prerelease.length != 0;

commits = await getCommits(previousTag.commit.sha, commitRef);

Expand All @@ -136,6 +138,8 @@ export default async function main() {
// Determine if we should continue with tag creation based on main vs prerelease branch
let shouldContinue = true;
if (isPrerelease) {
if (!bump && !previousWasPrerelease && defaultDraftBump === 'false')
shouldContinue = false;
if (!bump && defaultPreReleaseBump === 'false') {
shouldContinue = false;
}
Expand All @@ -155,9 +159,9 @@ export default async function main() {

// If we don't have an automatic bump for the prerelease, just set our bump as the default
if (isPrerelease && !bump) {
if (previousVersion.prerelease.length == 0)
if (!previousWasPrerelease)
// previous version is a prerelease -> draft a new version with the default bump and make it a prerelease
bump = defaultBump;
bump = defaultDraftBump;
else
bump = defaultPreReleaseBump;
}
Expand Down
101 changes: 96 additions & 5 deletions tests/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,8 @@ describe('github-tag-action', () => {
expect(mockSetFailed).not.toBeCalled();
});

/** 1.3.0 commit =[minor, preminor]=> 1.4.0-pre.0 */
it('does create prerelease tag respecting default_bump', async () => {
/** 1.3.0 commit =[minor, minor, prerelease]=> 1.4.0-pre.0 */
it('does create prerelease tag respecting default_draft_bump', async () => {
/*
* Given
*/
Expand All @@ -555,6 +555,7 @@ describe('github-tag-action', () => {
* When
*/
setInput('default_bump', 'minor');
setInput('default_draft_bump', 'minor');
setInput('default_prerelease_bump', 'prerelease');
await action();

Expand All @@ -569,8 +570,8 @@ describe('github-tag-action', () => {
expect(mockSetFailed).not.toBeCalled();
});

/** 1.3.0-pre.0 + commit =[minor, prerelease]=> 1.3.0-pre.1 */
it('does update prerelease tag respecting default_bump', async () => {
/** 1.3.0-pre.0 + commit =[minor, minor, prerelease]=> 1.3.0-pre.1 */
it('does update prerelease tag respecting default_draft_bump', async () => {
/*
* Given
*/
Expand Down Expand Up @@ -603,6 +604,7 @@ describe('github-tag-action', () => {
* When
*/
setInput('default_bump', 'minor');
setInput('default_draft_bump', 'minor');
setInput('default_prerelease_bump', 'prerelease');
await action();

Expand All @@ -617,7 +619,7 @@ describe('github-tag-action', () => {
expect(mockSetFailed).not.toBeCalled();
});

/** 1.3.0-pre.0 + commit =[minor, preminor]=> 1.4.0-pre.0 */
/** 1.3.0-pre.0 + commit =[minor, minor, preminor]=> 1.4.0-pre.0 */
it('does update prerelease tag with preminor', async () => {
/*
* Given
Expand Down Expand Up @@ -651,6 +653,7 @@ describe('github-tag-action', () => {
* When
*/
setInput('default_bump', 'minor');
setInput('default_draft_bump', 'minor');
setInput('default_prerelease_bump', 'preminor');
await action();

Expand All @@ -665,6 +668,94 @@ describe('github-tag-action', () => {
expect(mockSetFailed).not.toBeCalled();
});

/**
* 1.2.3 commit =[minor, -, prerelease]=> 1.2.4-pre.0
* according to semver, a prerelease increment on a non-prerelease version drafts a new minor version
*/
it('default_draft_bump defaults to default_prerelease_bump (prerelease)', async () => {
/*
* Given
*/
const commits = [{ message: 'this is a commit', hash: null }];
jest
.spyOn(utils, 'getCommits')
.mockImplementation(async (sha) => commits);

const validTags = [
{
name: 'v1.2.3',
commit: { sha: '012345', url: '' },
zipball_url: '',
tarball_url: 'string',
node_id: 'string',
},
];
jest
.spyOn(utils, 'getValidTags')
.mockImplementation(async () => validTags);

/*
* When
*/
setInput('default_bump', 'minor');
setInput('default_prerelease_bump', 'prerelease');
await action();

/*
* Then
*/
expect(mockCreateTag).toHaveBeenCalledWith(
'v1.2.4-prerelease.0', // prerelease drafts patch upgrades
expect.any(Boolean),
expect.any(String)
);
expect(mockSetFailed).not.toBeCalled();
});

/**
* 1.2.3 commit =[minor, -, prerelease]=> 1.2.4-pre.0
* according to semver, a prerelease increment on a non-prerelease version drafts a new minor version
*/
it('default_draft_bump defaults to default_prerelease_bump (preminor)', async () => {
/*
* Given
*/
const commits = [{ message: 'this is a commit', hash: null }];
jest
.spyOn(utils, 'getCommits')
.mockImplementation(async (sha) => commits);

const validTags = [
{
name: 'v1.2.3',
commit: { sha: '012345', url: '' },
zipball_url: '',
tarball_url: 'string',
node_id: 'string',
},
];
jest
.spyOn(utils, 'getValidTags')
.mockImplementation(async () => validTags);

/*
* When
*/
setInput('default_bump', 'minor');
setInput('default_prerelease_bump', 'preminor');
await action();

/*
* Then
*/
expect(mockCreateTag).toHaveBeenCalledWith(
'v1.3.0-prerelease.0', // prerelease drafts patch upgrades
expect.any(Boolean),
expect.any(String)
);
expect(mockSetFailed).not.toBeCalled();
});

it('does create prepatch tag', async () => {
/*
* Given
Expand Down

0 comments on commit 32ca813

Please sign in to comment.