Skip to content

Commit

Permalink
fix: Allow validation to pass when subjectPatternError is provided (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn authored Jan 19, 2021
1 parent 439d2d1 commit 19bf182
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/validatePrTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,29 @@ module.exports = async function validatePrTitle(
);
}

if (subjectPattern) {
const match = result.subject.match(new RegExp(subjectPattern));

function throwSubjectPatternError(message) {
if (subjectPatternError) {
throw new Error(
formatMessage(subjectPatternError, {
subject: result.subject,
title: prTitle
})
);
message = formatMessage(subjectPatternError, {
subject: result.subject,
title: prTitle
});
}

throw new Error(message);
}

if (subjectPattern) {
const match = result.subject.match(new RegExp(subjectPattern));

if (!match) {
throw new Error(
throwSubjectPatternError(
`The subject "${result.subject}" found in pull request title "${prTitle}" doesn't match the configured pattern "${subjectPattern}".`
);
}

const matchedPart = match[0];
if (matchedPart.length !== result.subject.length) {
throw new Error(
throwSubjectPatternError(
`The subject "${result.subject}" found in pull request title "${prTitle}" isn't an exact match for the configured pattern "${subjectPattern}". Please provide a subject that matches the whole pattern exactly.`
);
}
Expand Down
8 changes: 8 additions & 0 deletions src/validatePrTitle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ describe('description validation', () => {
await validatePrTitle('fix: sK!"§4123');
});

it('can pass the validation when `subjectPatternError` is configured', async () => {
await validatePrTitle('fix: foobar', {
subjectPattern: '^(?![A-Z]).+$',
subjectPatternError:
'The subject found in the pull request title cannot start with an uppercase character.'
});
});

it('uses the `subjectPatternError` if available when the `subjectPattern` does not match', async () => {
const customError =
'The subject found in the pull request title cannot start with an uppercase character.';
Expand Down

0 comments on commit 19bf182

Please sign in to comment.