Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add options to pass custom regex to conventional-commits-parser #177

Merged
merged 5 commits into from Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions README.md
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
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
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
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
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