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

feat: support for scope-required input #307

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ See [Conventional Commits](https://www.conventionalcommits.org/) for sample titl
**Optional.** URL to be used when linking the "Details" in the actions overview.
> Default: `"https://www.conventionalcommits.org/en/v1.0.0/#summary"`.

### `scope-required`

**Optional.** Set to `true` if you want to fail in case the scope isn't set.
> Default: `false`.

## Outputs

### `success`
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ inputs:
description: URL to be used when linking the "Details" in the actions overview.
required: false
default: https://www.conventionalcommits.org/en/v1.0.0/#summary
scope-required:
description: Set to true if you want to fail in case the scope isn't set.
required: false
default: false
type: boolean
runs:
using: docker
image: Dockerfile
Expand Down
7 changes: 6 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ async function run() {
let successState = core.getInput('success-state');
let failureState = core.getInput('failure-state');
let targetUrl = core.getInput('target-url');
let scopeRequired = core.getInput('scope-required');
const installPresetPackage = core.getInput('preset');
const requirePresetPackage = npa(installPresetPackage).name;

Expand All @@ -28,7 +29,11 @@ async function run() {
let error = null;
try {
await installPreset(installPresetPackage);
await validateTitle(requirePresetPackage, contextPullRequest.title);
await validateTitle(
requirePresetPackage,
contextPullRequest.title,
scopeRequired
);
} catch (err) {
error = err;
}
Expand Down
8 changes: 7 additions & 1 deletion src/validateTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function isFunction(functionToCheck) {
return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]';
}

module.exports = async function validateTitle(preset, title) {
module.exports = async function validateTitle(preset, title, scopeRequired) {
let conventionalChangelogConfig = require(preset);
if (isFunction(conventionalChangelogConfig)) {
conventionalChangelogConfig = await conventionalChangelogConfig();
Expand All @@ -32,4 +32,10 @@ module.exports = async function validateTitle(preset, title) {
)}.`
);
}

if (scopeRequired && !result.scope) {
throw new Error(
`No scope found in pull request title "${title}" but it's required.`
);
}
};
6 changes: 6 additions & 0 deletions src/validateTitle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ it('throws for PR titles with an unknown type', async () => {
/Unknown release type "foo" found in pull request title "foo: Bar"./
);
});

it('throws for PR titles without a scope when its required', async () => {
await expect(validateTitle(preset, 'feat: Add feature', true)).rejects.toThrow(
/No scope found in pull request title "feat: Add feature" but it's required./
);
});