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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Jomshir98 marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Jomshir98 marked this conversation as resolved.
Show resolved Hide resolved
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."
Jomshir98 marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
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