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

Commit

Permalink
Added skip-on-empty flag to skip release when no commits (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenverCoder1 authored Sep 30, 2021
1 parent 142a1c5 commit d2c0ce0
Showing 6 changed files with 77 additions and 7 deletions.
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -44,6 +44,13 @@ inputs:
description: Set to true if you want to have prerelease instead release
required: false
default: 'false'
skip-on-empty:
description: Set to true to skip creating a release when all commits are excluded
required: false
default: 'false'
outputs:
skipped:
description: Will be set to true if skip-on-empty is enabled and a release was not created, false otherwise
runs:
using: 'node12'
main: 'distribution/index.js'
20 changes: 17 additions & 3 deletions distribution/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion generate-release-notes.js
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@ async function generateReleaseNotes({
commitTemplate = '- {hash} {title}',
releaseTemplate = '{commits}\n\n{range}',
dateFormat = 'short',
sort = 'desc'
sort = 'desc',
skipOnEmpty = false
}) {
dateFormat = dateFormat.includes('%') ? 'format:' + dateFormat : dateFormat;
// Get commits between computed range
@@ -39,6 +40,10 @@ async function generateReleaseNotes({

const commitEntries = [];
if (commits.length === 0) {
if (skipOnEmpty) {
return;
}

commitEntries.push('_Maintenance release_');
} else {
for (const {hash, date, title} of commits) {
25 changes: 25 additions & 0 deletions generate-release-notes.test.js
Original file line number Diff line number Diff line change
@@ -156,3 +156,28 @@ test('generates changelog using reverse optios', async () => {
[\`v3.0.0..v3.1.0\`](https://github.com/fregante/release-with-changelog/compare/v3.0.0..v3.1.0)
`));
});

test('generates changelog with all commits excluded', async () => {
const output = await generateReleaseNotes({
range,
commitTemplate: '- {title}',
releaseTemplate: '{commits}',
exclude: 'a|e|i|o|u'
});

expect(output).toEqual(dedent(`
_Maintenance release_
`));
});

test('generates changelog with all commits excluded and skip-on-empty', async () => {
const output = await generateReleaseNotes({
range,
commitTemplate: '- {title}',
releaseTemplate: '{commits}',
exclude: 'a|e|i|o|u',
skipOnEmpty: true
});

expect(output).toEqual(undefined);
});
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ async function run() {
const reverseSort = core.getInput('reverse-sort');
const isDraft = core.getInput('draft') === 'true';
const isPrerelease = core.getInput('prerelease') === 'true';
const skipOnEmpty = core.getInput('skip-on-empty') === 'true';

// Fetch tags from remote
await execFile('git', ['fetch', 'origin', '+refs/tags/*:refs/tags/*']);
@@ -45,18 +46,27 @@ async function run() {

core.info('Computed range: ' + range);

const releaseNotes = await generateReleaseNotes({range, exclude, commitTemplate, releaseTemplate, dateFormat, reverseSort, skipOnEmpty});

// Skip creating release if no commits
// Explicit check to avoid matching an empty string https://github.com/fregante/release-with-changelog/pull/48#discussion_r719593452
if (releaseNotes === undefined) {
core.setOutput('skipped', true);
return core.info('Skipped creating release for tag `' + pushedTag + '`');
}

// Create a release with markdown content in body
const octokit = getOctokit(core.getInput('token'));
const createReleaseResponse = await octokit.repos.createRelease({
repo,
owner,
name: releaseTitle.replace('{tag}', pushedTag),
tag_name: pushedTag, // eslint-disable-line camelcase
body: await generateReleaseNotes({range, exclude, commitTemplate, releaseTemplate, dateFormat, reverseSort}),
body: releaseNotes,
draft: isDraft,
prerelease: isPrerelease
});

core.setOutput('skipped', false);
core.info('Created release `' + createReleaseResponse.data.id + '` for tag `' + pushedTag + '`');
} catch (error) {
core.setFailed(error.message);
11 changes: 10 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -129,9 +129,18 @@ Example: `true`

Set to `true` if you want to have prerelease instead release.

### skip-on-empty

Default: `false` <br>
Example: `true`

Set to `true` if you want to skip creating the release when all commits are excluded.

## Outputs

None.
### skipped

Will be set to `true` if skip-on-empty is enabled and a release was not created, `false` otherwise.

## Release notes for previous tags/releases

1 comment on commit d2c0ce0

@yakov116
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fregante ci failed.

Please sign in to comment.