Skip to content

Commit 6dc50d5

Browse files
committed
feat: support providing regexps for types
1 parent a46a7c8 commit 6dc50d5

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ feat(ui): Add `Button` component
6363
```yml
6464
with:
6565
# Configure which types are allowed (newline-delimited).
66+
# These are regex patterns auto-wrapped in `^ $`.
6667
# Default: https://github.com/commitizen/conventional-commit-types
6768
types: |
6869
fix
6970
feat
71+
JIRA-\d+
7072
# Configure which scopes are allowed (newline-delimited).
7173
# These are regex patterns auto-wrapped in `^ $`.
7274
scopes: |

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ branding:
99
color: 'green'
1010
inputs:
1111
types:
12-
description: "Provide custom types (newline delimited) if you don't want the default ones from https://www.conventionalcommits.org."
12+
description: "Provide custom types (newline delimited) if you don't want the default ones from https://www.conventionalcommits.org. These are regex patterns auto-wrapped in `^ $`."
1313
required: false
1414
scopes:
1515
description: 'Configure which scopes are allowed (newline delimited). These are regex patterns auto-wrapped in `^ $`.'

src/validatePrTitle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default async function validatePrTitle(
7171
raiseError(`No subject found in pull request title "${prTitle}".`);
7272
}
7373

74-
if (!types.includes(result.type)) {
74+
if (!types.some((type) => new RegExp(`^${type}$`).test(result.type))) {
7575
raiseError(
7676
`Unknown release type "${
7777
result.type

src/validatePrTitle.test.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,86 @@ it('throws for PR titles with an unknown type', async () => {
3939
);
4040
});
4141

42+
describe('regex types', () => {
43+
const headerPattern = /^([\w-]*)(?:\(([\w$.\-*/ ]*)\))?: (.*)$/;
44+
45+
it('allows a regex matching type', async () => {
46+
await validatePrTitle('JIRA-123: Bar', {
47+
types: ['JIRA-\\d+'],
48+
headerPattern
49+
});
50+
});
51+
52+
it('can be used for dynamic Jira keys', async () => {
53+
const inputs = ['JIRA-123', 'P-123', 'INT-31', 'CONF-0'];
54+
55+
for (let index = 0; index < inputs.length; index++) {
56+
await validatePrTitle(`${inputs[index]}: did the thing`, {
57+
types: ['[A-Z]+-\\d+'],
58+
headerPattern
59+
});
60+
}
61+
});
62+
63+
it('throws for PR titles without a type', async () => {
64+
await expect(
65+
validatePrTitle('Fix JIRA-123 bug', {
66+
types: ['JIRA-\\d+'],
67+
headerPattern
68+
})
69+
).rejects.toThrow(
70+
'No release type found in pull request title "Fix JIRA-123 bug".'
71+
);
72+
});
73+
74+
it('throws for PR titles with only a type', async () => {
75+
await expect(
76+
validatePrTitle('JIRA-123:', {
77+
types: ['JIRA-\\d+'],
78+
headerPattern
79+
})
80+
).rejects.toThrow(
81+
'No release type found in pull request title "JIRA-123:".'
82+
);
83+
});
84+
85+
it('throws for PR titles without a subject', async () => {
86+
await expect(
87+
validatePrTitle('JIRA-123: ', {
88+
types: ['JIRA-\\d+'],
89+
headerPattern
90+
})
91+
).rejects.toThrow('No subject found in pull request title "JIRA-123: ".');
92+
});
93+
94+
it('throws for PR titles that do not match the regex', async () => {
95+
await expect(
96+
validatePrTitle('CONF-123: ', {
97+
types: ['JIRA-\\d+'],
98+
headerPattern
99+
})
100+
).rejects.toThrow('No subject found in pull request title "CONF-123: ".');
101+
});
102+
103+
it('throws when an unknown type is detected for auto-wrapping regex', async () => {
104+
await expect(
105+
validatePrTitle('JIRA-123A: Bar', {
106+
types: ['JIRA-\\d+'],
107+
headerPattern
108+
})
109+
).rejects.toThrow(
110+
'Unknown release type "JIRA-123A" found in pull request title "JIRA-123A: Bar". \n\nAvailable types:\n - JIRA-\\d+'
111+
);
112+
});
113+
114+
it('allows scopes when using a regex for the type', async () => {
115+
await validatePrTitle('JIRA-123(core): Bar', {
116+
types: ['JIRA-\\d+'],
117+
headerPattern
118+
});
119+
});
120+
});
121+
42122
describe('defined scopes', () => {
43123
it('allows a missing scope by default', async () => {
44124
await validatePrTitle('fix: Bar');

0 commit comments

Comments
 (0)