Skip to content

Commit

Permalink
feat: Add ability to use multiple scopes (#85)
Browse files Browse the repository at this point in the history
* Add tests

* Add support for multiple scopes

* Handle plurals & trim scopes

* Update tests
  • Loading branch information
amromusharaf authored Feb 10, 2021
1 parent add5e66 commit d6aabb6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/validatePrTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ module.exports = async function validatePrTitle(
.join('\n')}`;
}

function isUnknownScope(s) {
return scopes && !scopes.includes(s);
}

if (!result.type) {
throw new Error(
`No release type found in pull request title "${prTitle}". Add a prefix to indicate what kind of release this pull request corresponds to (see https://www.conventionalcommits.org/).\n\n${printAvailableTypes()}`
Expand All @@ -54,11 +58,17 @@ module.exports = async function validatePrTitle(
);
}

if (scopes && result.scope && !scopes.includes(result.scope)) {
const givenScopes = result.scope
? result.scope.split(',').map((scope) => scope.trim())
: undefined;
const unknownScopes = givenScopes ? givenScopes.filter(isUnknownScope) : [];
if (scopes && unknownScopes.length > 0) {
throw new Error(
`Unknown scope "${
result.scope
}" found in pull request title "${prTitle}". Use one of the available scopes: ${scopes.join(
`Unknown ${
unknownScopes.length > 1 ? 'scopes' : 'scope'
} "${unknownScopes.join(
','
)}" found in pull request title "${prTitle}". Use one of the available scopes: ${scopes.join(
', '
)}.`
);
Expand Down
14 changes: 14 additions & 0 deletions src/validatePrTitle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ describe('defined scopes', () => {
await validatePrTitle('fix(core): Bar', {scopes: ['core']});
});

it('allows multiple matching scopes', async () => {
await validatePrTitle('fix(core,e2e): Bar', {
scopes: ['core', 'e2e', 'web']
});
});

it('throws when an unknown scope is detected within multiple scopes', async () => {
await expect(
validatePrTitle('fix(core,e2e,foo,bar): Bar', {scopes: ['foo', 'core']})
).rejects.toThrow(
'Unknown scopes "e2e,bar" found in pull request title "fix(core,e2e,foo,bar): Bar". Use one of the available scopes: foo, core.'
);
});

it('throws when an unknown scope is detected', async () => {
await expect(
validatePrTitle('fix(core): Bar', {scopes: ['foo']})
Expand Down

0 comments on commit d6aabb6

Please sign in to comment.