Skip to content

Commit 956659a

Browse files
authored
feat: Add options to pass custom regex to conventional-commits-parser (#177)
1 parent 6872cf1 commit 956659a

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ The action works without configuration, however you can provide options for cust
6565
The subject "{subject}" found in the pull request title "{title}"
6666
didn't match the configured pattern. Please ensure that the subject
6767
doesn't start with an uppercase character.
68-
# For work-in-progress PRs you can typically use draft pull requests
69-
# from GitHub. However, private repositories on the free plan don't have
70-
# this option and therefore this action allows you to opt-in to using the
71-
# special "[WIP]" prefix to indicate this state. This will avoid the
72-
# validation of the PR title and the pull request checks remain pending.
73-
# Note that a second check will be reported if this is enabled.
74-
wip: true
7568
# When using "Squash and merge" on a PR with only one commit, GitHub
7669
# will suggest using that commit message instead of the PR title for the
7770
# merge commit, and it's easy to commit this by mistake. Enable this option
@@ -89,6 +82,20 @@ The action works without configuration, however you can provide options for cust
8982
ignoreLabels: |
9083
bot
9184
ignore-semantic-pull-request
85+
# If you're using a format for the PR title that differs from the traditional Conventional
86+
# Commits spec, you can use these options to customize the parsing of the type, scope and
87+
# subject. The `headerPattern` should contain a regex where the capturing groups in parentheses
88+
# correspond to the parts listed in `headerPatternCorrespondence`.
89+
# See: https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser#headerpattern
90+
headerPattern: '^(\w*)(?:\(([\w$.\-*/ ]*)\))?: (.*)$'
91+
headerPatternCorrespondence: type, scope, subject
92+
# For work-in-progress PRs you can typically use draft pull requests
93+
# from GitHub. However, private repositories on the free plan don't have
94+
# this option and therefore this action allows you to opt-in to using the
95+
# special "[WIP]" prefix to indicate this state. This will avoid the
96+
# validation of the PR title and the pull request checks remain pending.
97+
# Note that a second check will be reported if this is enabled.
98+
wip: true
9299
```
93100
94101
## Event triggers

action.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ inputs:
2323
subjectPatternError:
2424
description: "If `subjectPattern` is configured, you can use this property to override the default error message that is shown when the pattern doesn't match. The variables `subject` and `title` can be used within the message."
2525
required: false
26-
wip:
27-
description: "For work-in-progress PRs you can typically use draft pull requests from Github. However, private repositories on the free plan don't have this option and therefore this action allows you to opt-in to using the special '[WIP]' prefix to indicate this state. This will avoid the validation of the PR title and the pull request checks remain pending. Note that a second check will be reported if this is enabled."
28-
required: false
2926
validateSingleCommit:
3027
description: "When using \"Squash and merge\" on a PR with only one commit, GitHub will suggest using that commit message instead of the PR title for the merge commit, and it's easy to commit this by mistake. Enable this option to also validate the commit message for one commit PRs."
3128
required: false
@@ -38,3 +35,12 @@ inputs:
3835
ignoreLabels:
3936
description: "If the PR contains one of these labels, the validation is skipped. Multiple labels can be separated by newlines. If you want to rerun the validation when labels change, you might want to use the `labeled` and `unlabeled` event triggers in your workflow."
4037
required: false
38+
headerPattern:
39+
description: "If you're using a format for the PR title that differs from the traditional Conventional Commits spec, you can use this to customize the parsing of the type, scope and subject. The `headerPattern` should contain a regex where the capturing groups in parentheses correspond to the parts listed in `headerPatternCorrespondence`. For more details see: https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser#headerpattern"
40+
required: false
41+
headerPatternCorrespondence:
42+
description: "If `headerPattern` is configured, you can use this to define which capturing groups correspond to the type, scope and subject."
43+
required: false
44+
wip:
45+
description: "For work-in-progress PRs you can typically use draft pull requests from Github. However, private repositories on the free plan don't have this option and therefore this action allows you to opt-in to using the special '[WIP]' prefix to indicate this state. This will avoid the validation of the PR title and the pull request checks remain pending. Note that a second check will be reported if this is enabled."
46+
required: false

src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ module.exports = async function run() {
1212
wip,
1313
subjectPattern,
1414
subjectPatternError,
15+
headerPattern,
16+
headerPatternCorrespondence,
1517
validateSingleCommit,
1618
validateSingleCommitMatchesPrTitle,
1719
githubBaseUrl,
@@ -66,7 +68,9 @@ module.exports = async function run() {
6668
scopes,
6769
requireScope,
6870
subjectPattern,
69-
subjectPatternError
71+
subjectPatternError,
72+
headerPattern,
73+
headerPatternCorrespondence
7074
});
7175

7276
if (validateSingleCommit) {
@@ -105,7 +109,9 @@ module.exports = async function run() {
105109
scopes,
106110
requireScope,
107111
subjectPattern,
108-
subjectPatternError
112+
subjectPatternError,
113+
headerPattern,
114+
headerPatternCorrespondence
109115
});
110116
} catch (error) {
111117
throw new Error(

src/parseConfig.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ module.exports = function parseConfig() {
2828
);
2929
}
3030

31+
let headerPattern;
32+
if (process.env.INPUT_HEADERPATTERN) {
33+
headerPattern = ConfigParser.parseString(process.env.INPUT_HEADERPATTERN);
34+
}
35+
36+
let headerPatternCorrespondence;
37+
if (process.env.INPUT_HEADERPATTERNCORRESPONDENCE) {
38+
headerPatternCorrespondence = ConfigParser.parseString(
39+
process.env.INPUT_HEADERPATTERNCORRESPONDENCE
40+
);
41+
}
42+
3143
let wip;
3244
if (process.env.INPUT_WIP) {
3345
wip = ConfigParser.parseBoolean(process.env.INPUT_WIP);
@@ -64,6 +76,8 @@ module.exports = function parseConfig() {
6476
wip,
6577
subjectPattern,
6678
subjectPatternError,
79+
headerPattern,
80+
headerPatternCorrespondence,
6781
validateSingleCommit,
6882
validateSingleCommitMatchesPrTitle,
6983
githubBaseUrl,

src/validatePrTitle.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,25 @@ const defaultTypes = Object.keys(conventionalCommitTypes.types);
77

88
module.exports = async function validatePrTitle(
99
prTitle,
10-
{types, scopes, requireScope, subjectPattern, subjectPatternError} = {}
10+
{
11+
types,
12+
scopes,
13+
requireScope,
14+
subjectPattern,
15+
subjectPatternError,
16+
headerPattern,
17+
headerPatternCorrespondence
18+
} = {}
1119
) {
1220
if (!types) types = defaultTypes;
1321

1422
const {parserOpts} = await conventionalCommitsConfig();
23+
if (headerPattern) {
24+
parserOpts.headerPattern = headerPattern;
25+
}
26+
if (headerPatternCorrespondence) {
27+
parserOpts.headerCorrespondence = headerPatternCorrespondence;
28+
}
1529
const result = parser(prTitle, parserOpts);
1630

1731
function printAvailableTypes() {

0 commit comments

Comments
 (0)