Skip to content

Commit

Permalink
feat: added templates
Browse files Browse the repository at this point in the history
  • Loading branch information
derberg committed Jan 15, 2021
1 parent d6cf96e commit 27b973b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ jobs:
# This example ensures the subject doesn't start with an uppercase character.
subjectPattern: ^(?![A-Z]).+$
# Configure custom error message that should be passed to the logs to better describe the error thrown by subject pattern validation.
# You can pass template values to the error test
# - ${subject} will be replaced with the subject of the PR title
# - ${title} will be replaced with the PR title
# This example will return the following error of PR title "fix(core): Test":
# The subject "Test" found in the pull request title "fix(core): Test" didn't match the configured pattern.
# Please ensure that the subject doesn't start with an uppercase character.
subjectPatternError: |
The subject found in the pull request title didn't match the configured pattern.
The subject "${subject}" found in the pull request title "${title}" didn't match the configured pattern.
Please ensure that the subject doesn't start with an uppercase character.
# For work-in-progress PRs you can typically use draft pull requests
# from Github. However, private repositories on the free plan don't have
Expand Down
2 changes: 2 additions & 0 deletions src/SubjectPatternErrorParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = (subjectPatternError, subject, title) =>
subjectPatternError.replace('${subject}', subject).replace('${title}', title);
24 changes: 24 additions & 0 deletions src/SubjectPatternErrorParser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const subjectPatternErrorParser = require('./SubjectPatternErrorParser');

describe('parse pattern error', () => {
it('parse string without changes if template values not provided', () => {
const customError = 'this is test';
expect(subjectPatternErrorParser(customError)).toEqual(customError);
});

it('parses string properly by replacing one template value', () => {
expect(
subjectPatternErrorParser('this is ${subject} test', 'my subject')
).toEqual('this is my subject test');
});

it('parses string properly by replacing both template values', () => {
expect(
subjectPatternErrorParser(
'this ${title} is ${subject} test',
'my subject',
'my title'
)
).toEqual('this my title is my subject test');
});
});
7 changes: 6 additions & 1 deletion src/validatePrTitle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const conventionalCommitsConfig = require('conventional-changelog-conventionalcommits');
const conventionalCommitTypes = require('conventional-commit-types');
const parser = require('conventional-commits-parser').sync;
const subjectPatternErrorParser = require('./SubjectPatternErrorParser');

const defaultTypes = Object.keys(conventionalCommitTypes.types);

Expand Down Expand Up @@ -66,7 +67,11 @@ module.exports = async function validatePrTitle(
if (subjectPattern) {
const match = result.subject.match(new RegExp(subjectPattern));

if (subjectPatternError) throw new Error(subjectPatternError);
if (subjectPatternError) {
throw new Error(
subjectPatternErrorParser(subjectPatternError, result.subject, prTitle)
);
}

if (!match) {
throw new Error(
Expand Down
12 changes: 12 additions & 0 deletions src/validatePrTitle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ describe('description validation', () => {
).rejects.toThrow(customError);
});

it('throws custom error enriched with template values', async () => {
await expect(
validatePrTitle('fix: Foobar', {
subjectPattern: '^(?![A-Z]).+$',
subjectPatternError:
'The subject "${subject}" found in the pull request title "${title}" cannot start with an uppercase character.'
})
).rejects.toThrow(
'The subject "Foobar" found in the pull request title "fix: Foobar" cannot start with an uppercase character.'
);
});

it('throws for invalid subjects', async () => {
await expect(
validatePrTitle('fix: Foobar', {
Expand Down

0 comments on commit 27b973b

Please sign in to comment.