Skip to content

Commit

Permalink
feat: Allow enum options to be newline delimited only
Browse files Browse the repository at this point in the history
Fixes: amannn#204

Signed-off-by: Jesse Szwedko <[email protected]>
  • Loading branch information
jszwedko committed Oct 4, 2022
1 parent 505e44b commit 4105a13
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ inputs:
headerPatternCorrespondence:
description: "If `headerPattern` is configured, you can use this to define which capturing groups correspond to the type, scope and subject."
required: false
requireNewlines:
description: "If `requireNewlines` is `true` then enum configuration (`scopes`, `types`, `disallowScopes`, `ignoreLabels`) will parse the input as newline delimited rather than any whitespace or commas to delimit options."
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
7 changes: 5 additions & 2 deletions src/ConfigParser.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const ENUM_SPLIT_REGEX = /[,\s]\s*/;
const ENUM_SPLIT_NEWLINES_REGEX = /\n/;

module.exports = {
parseEnum(input) {
parseEnum(input, onlyNewlines) {
let pattern = onlyNewlines ? ENUM_SPLIT_NEWLINES_REGEX : ENUM_SPLIT_REGEX;

return input
.split(ENUM_SPLIT_REGEX)
.split(pattern)
.map((part) => part.trim())
.filter((part) => part.length > 0);
},
Expand Down
12 changes: 10 additions & 2 deletions src/ConfigParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const ConfigParser = require('./ConfigParser');

describe('parseEnum', () => {
it('parses commas', () => {
expect(ConfigParser.parseEnum('one, two,three, \nfour ')).toEqual([
expect(ConfigParser.parseEnum('one, two,three, \nfour ', false)).toEqual([
'one',
'two',
'three',
Expand All @@ -11,11 +11,19 @@ describe('parseEnum', () => {
});

it('parses white space', () => {
expect(ConfigParser.parseEnum('one two\nthree \n\rfour')).toEqual([
expect(ConfigParser.parseEnum('one two\nthree \n\rfour', false)).toEqual([
'one',
'two',
'three',
'four'
]);
});

it('allows only newlines', () => {
expect(ConfigParser.parseEnum('one two\nthree \n\rfour', true)).toEqual([
'one two',
'three',
'four'
]);
});
});
13 changes: 9 additions & 4 deletions src/parseConfig.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
const ConfigParser = require('./ConfigParser');

module.exports = function parseConfig() {
let requireNewlines;
if (process.env.INPUT_REQUIRENEWLINES) {
newlineScopes = ConfigParser.parseBoolean(process.env.INPUT_REQUIRENEWLINES);
}

let types;
if (process.env.INPUT_TYPES) {
types = ConfigParser.parseEnum(process.env.INPUT_TYPES);
types = ConfigParser.parseEnum(process.env.INPUT_TYPES, requireNewlines);
}

let scopes;
if (process.env.INPUT_SCOPES) {
scopes = ConfigParser.parseEnum(process.env.INPUT_SCOPES);
scopes = ConfigParser.parseEnum(process.env.INPUT_SCOPES, requireNewlines);
}

let requireScope;
Expand All @@ -18,7 +23,7 @@ module.exports = function parseConfig() {

let disallowScopes;
if (process.env.INPUT_DISALLOWSCOPES) {
disallowScopes = ConfigParser.parseEnum(process.env.INPUT_DISALLOWSCOPES);
disallowScopes = ConfigParser.parseEnum(process.env.INPUT_DISALLOWSCOPES, requireNewlines);
}

let subjectPattern;
Expand Down Expand Up @@ -71,7 +76,7 @@ module.exports = function parseConfig() {

let ignoreLabels;
if (process.env.INPUT_IGNORELABELS) {
ignoreLabels = ConfigParser.parseEnum(process.env.INPUT_IGNORELABELS);
ignoreLabels = ConfigParser.parseEnum(process.env.INPUT_IGNORELABELS, requireNewlines);
}

return {
Expand Down

0 comments on commit 4105a13

Please sign in to comment.