From d6aabb6fedc5f57cec60b16db8595a92f1e263ab Mon Sep 17 00:00:00 2001 From: amromusharaf <38303753+amromusharaf@users.noreply.github.com> Date: Wed, 10 Feb 2021 02:08:08 -0500 Subject: [PATCH] feat: Add ability to use multiple scopes (#85) * Add tests * Add support for multiple scopes * Handle plurals & trim scopes * Update tests --- src/validatePrTitle.js | 18 ++++++++++++++---- src/validatePrTitle.test.js | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/validatePrTitle.js b/src/validatePrTitle.js index 00ad99f81..4a8cb5c00 100644 --- a/src/validatePrTitle.js +++ b/src/validatePrTitle.js @@ -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()}` @@ -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( ', ' )}.` ); diff --git a/src/validatePrTitle.test.js b/src/validatePrTitle.test.js index cfdb443e1..716312317 100644 --- a/src/validatePrTitle.test.js +++ b/src/validatePrTitle.test.js @@ -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']})