Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow validation to pass when subjectPatternError is provided #79

Merged
merged 1 commit into from
Jan 19, 2021
Merged
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
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