From 0fa24322db8c13dfeb14da457ee219b377b79534 Mon Sep 17 00:00:00 2001 From: jomshir98 Date: Wed, 20 Apr 2022 20:19:42 +0200 Subject: [PATCH 1/5] feat: add options to pass custom regex to conventional-commits-parser --- src/index.js | 10 ++++++++-- src/parseConfig.js | 14 ++++++++++++++ src/validatePrTitle.js | 16 +++++++++++++++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index b53f621a9..763bce938 100644 --- a/src/index.js +++ b/src/index.js @@ -12,6 +12,8 @@ module.exports = async function run() { wip, subjectPattern, subjectPatternError, + headerPattern, + headerPatternCorrespondence, validateSingleCommit, validateSingleCommitMatchesPrTitle, githubBaseUrl, @@ -66,7 +68,9 @@ module.exports = async function run() { scopes, requireScope, subjectPattern, - subjectPatternError + subjectPatternError, + headerPattern, + headerPatternCorrespondence }); if (validateSingleCommit) { @@ -105,7 +109,9 @@ module.exports = async function run() { scopes, requireScope, subjectPattern, - subjectPatternError + subjectPatternError, + headerPattern, + headerPatternCorrespondence }); } catch (error) { throw new Error( diff --git a/src/parseConfig.js b/src/parseConfig.js index 773b0d989..a3f8f93f0 100644 --- a/src/parseConfig.js +++ b/src/parseConfig.js @@ -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); @@ -64,6 +76,8 @@ module.exports = function parseConfig() { wip, subjectPattern, subjectPatternError, + headerPattern, + headerPatternCorrespondence, validateSingleCommit, validateSingleCommitMatchesPrTitle, githubBaseUrl, diff --git a/src/validatePrTitle.js b/src/validatePrTitle.js index 5416ba101..b87f28dfd 100644 --- a/src/validatePrTitle.js +++ b/src/validatePrTitle.js @@ -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() { From 4fb344574d97fc3b3330f07025e88cd2fa2d0a0a Mon Sep 17 00:00:00 2001 From: jomshir98 Date: Wed, 20 Apr 2022 20:33:11 +0200 Subject: [PATCH 2/5] Add new options to action.yml file --- action.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/action.yml b/action.yml index c4699ab92..1c4c95e2d 100644 --- a/action.yml +++ b/action.yml @@ -23,6 +23,12 @@ 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 + headerPattern: + description: "Configure custom regex that will be passed to conventional-commits-parser. 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 property to also configure which RegEx groups match what part of the semantic commit." + 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 From 2242e1823261a41de2b2999040eeafe6caf55b2d Mon Sep 17 00:00:00 2001 From: jomshir98 Date: Wed, 20 Apr 2022 21:01:59 +0200 Subject: [PATCH 3/5] Add info to readme --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 605f93b10..41d8e6522 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,13 @@ 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. + # Configure custom regex that will be passed to conventional-commits-parser. + # This can be used to specify different required format of the title. + # The correspondence is used to define what capturing group of headerPattern captures what header part. + # The example values are the default. + # For more details 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 From 45a1a7351f7c0f2f63ebd1a7bc2634f15b8af9f9 Mon Sep 17 00:00:00 2001 From: Jomshir98 Date: Fri, 22 Apr 2022 17:46:48 +0200 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: Jan Amann --- README.md | 10 +++++----- action.yml | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 41d8e6522..fe98d7559 100644 --- a/README.md +++ b/README.md @@ -65,11 +65,11 @@ 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. - # Configure custom regex that will be passed to conventional-commits-parser. - # This can be used to specify different required format of the title. - # The correspondence is used to define what capturing group of headerPattern captures what header part. - # The example values are the default. - # For more details see: https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser#headerpattern + # 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 diff --git a/action.yml b/action.yml index 1c4c95e2d..b6f5f2e29 100644 --- a/action.yml +++ b/action.yml @@ -24,10 +24,10 @@ inputs: 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 headerPattern: - description: "Configure custom regex that will be passed to conventional-commits-parser. For more details see: https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser#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 property to also configure which RegEx groups match what part of the semantic commit." + 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." From 05a5a3591e2fab87b27dc08bf0d11d4893899f05 Mon Sep 17 00:00:00 2001 From: jomshir98 Date: Fri, 22 Apr 2022 17:49:34 +0200 Subject: [PATCH 5/5] Move headerPattern and wip options in docs --- README.md | 28 ++++++++++++++-------------- action.yml | 18 +++++++++--------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index fe98d7559..320df8739 100644 --- a/README.md +++ b/README.md @@ -65,20 +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. - # 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 # 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 @@ -96,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 diff --git a/action.yml b/action.yml index b6f5f2e29..8f9d8a130 100644 --- a/action.yml +++ b/action.yml @@ -23,15 +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 - 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 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 @@ -44,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