Skip to content

Commit 19bf182

Browse files
authored
fix: Allow validation to pass when subjectPatternError is provided (#79)
1 parent 439d2d1 commit 19bf182

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/validatePrTitle.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,29 @@ module.exports = async function validatePrTitle(
6464
);
6565
}
6666

67-
if (subjectPattern) {
68-
const match = result.subject.match(new RegExp(subjectPattern));
69-
67+
function throwSubjectPatternError(message) {
7068
if (subjectPatternError) {
71-
throw new Error(
72-
formatMessage(subjectPatternError, {
73-
subject: result.subject,
74-
title: prTitle
75-
})
76-
);
69+
message = formatMessage(subjectPatternError, {
70+
subject: result.subject,
71+
title: prTitle
72+
});
7773
}
7874

75+
throw new Error(message);
76+
}
77+
78+
if (subjectPattern) {
79+
const match = result.subject.match(new RegExp(subjectPattern));
80+
7981
if (!match) {
80-
throw new Error(
82+
throwSubjectPatternError(
8183
`The subject "${result.subject}" found in pull request title "${prTitle}" doesn't match the configured pattern "${subjectPattern}".`
8284
);
8385
}
8486

8587
const matchedPart = match[0];
8688
if (matchedPart.length !== result.subject.length) {
87-
throw new Error(
89+
throwSubjectPatternError(
8890
`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.`
8991
);
9092
}

src/validatePrTitle.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ describe('description validation', () => {
108108
await validatePrTitle('fix: sK!"§4123');
109109
});
110110

111+
it('can pass the validation when `subjectPatternError` is configured', async () => {
112+
await validatePrTitle('fix: foobar', {
113+
subjectPattern: '^(?![A-Z]).+$',
114+
subjectPatternError:
115+
'The subject found in the pull request title cannot start with an uppercase character.'
116+
});
117+
});
118+
111119
it('uses the `subjectPatternError` if available when the `subjectPattern` does not match', async () => {
112120
const customError =
113121
'The subject found in the pull request title cannot start with an uppercase character.';

0 commit comments

Comments
 (0)