Skip to content

Commit

Permalink
feat: Add options to pass custom regex to conventional-commits-parser (
Browse files Browse the repository at this point in the history
  • Loading branch information
Jomshir98 authored Apr 22, 2022
1 parent 6872cf1 commit 956659a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ The action works without configuration, however you can provide options for cust
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
# 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.
wip: true
# 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
Expand All @@ -89,6 +82,20 @@ The action works without configuration, however you can provide options for cust
ignoreLabels: |
bot
ignore-semantic-pull-request
# If you're using a format for the PR title that differs from the traditional Conventional
# Commits spec, you can use these options 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`.
# See: https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser#headerpattern
headerPattern: '^(\w*)(?:\(([\w$.\-*/ ]*)\))?: (.*)$'
headerPatternCorrespondence: type, scope, subject
# 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.
wip: true
```
## Event triggers
Expand Down
12 changes: 9 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ inputs:
subjectPatternError:
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."
required: false
wip:
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."
required: false
validateSingleCommit:
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."
required: false
Expand All @@ -38,3 +35,12 @@ inputs:
ignoreLabels:
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."
required: false
headerPattern:
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"
required: false
headerPatternCorrespondence:
description: "If `headerPattern` is configured, you can use this to define which capturing groups correspond to the type, scope and subject."
required: false
wip:
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."
required: false
10 changes: 8 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module.exports = async function run() {
wip,
subjectPattern,
subjectPatternError,
headerPattern,
headerPatternCorrespondence,
validateSingleCommit,
validateSingleCommitMatchesPrTitle,
githubBaseUrl,
Expand Down Expand Up @@ -66,7 +68,9 @@ module.exports = async function run() {
scopes,
requireScope,
subjectPattern,
subjectPatternError
subjectPatternError,
headerPattern,
headerPatternCorrespondence
});

if (validateSingleCommit) {
Expand Down Expand Up @@ -105,7 +109,9 @@ module.exports = async function run() {
scopes,
requireScope,
subjectPattern,
subjectPatternError
subjectPatternError,
headerPattern,
headerPatternCorrespondence
});
} catch (error) {
throw new Error(
Expand Down
14 changes: 14 additions & 0 deletions src/parseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ module.exports = function parseConfig() {
);
}

let headerPattern;
if (process.env.INPUT_HEADERPATTERN) {
headerPattern = ConfigParser.parseString(process.env.INPUT_HEADERPATTERN);
}

let headerPatternCorrespondence;
if (process.env.INPUT_HEADERPATTERNCORRESPONDENCE) {
headerPatternCorrespondence = ConfigParser.parseString(
process.env.INPUT_HEADERPATTERNCORRESPONDENCE
);
}

let wip;
if (process.env.INPUT_WIP) {
wip = ConfigParser.parseBoolean(process.env.INPUT_WIP);
Expand Down Expand Up @@ -64,6 +76,8 @@ module.exports = function parseConfig() {
wip,
subjectPattern,
subjectPatternError,
headerPattern,
headerPatternCorrespondence,
validateSingleCommit,
validateSingleCommitMatchesPrTitle,
githubBaseUrl,
Expand Down
16 changes: 15 additions & 1 deletion src/validatePrTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@ const defaultTypes = Object.keys(conventionalCommitTypes.types);

module.exports = async function validatePrTitle(
prTitle,
{types, scopes, requireScope, subjectPattern, subjectPatternError} = {}
{
types,
scopes,
requireScope,
subjectPattern,
subjectPatternError,
headerPattern,
headerPatternCorrespondence
} = {}
) {
if (!types) types = defaultTypes;

const {parserOpts} = await conventionalCommitsConfig();
if (headerPattern) {
parserOpts.headerPattern = headerPattern;
}
if (headerPatternCorrespondence) {
parserOpts.headerCorrespondence = headerPatternCorrespondence;
}
const result = parser(prTitle, parserOpts);

function printAvailableTypes() {
Expand Down

0 comments on commit 956659a

Please sign in to comment.