Skip to content

Commit 81e0ef4

Browse files
committed
feat: added test branch with regex
1 parent b68f77d commit 81e0ef4

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class BranchNameLint {
1414
msgBranchDisallowed: 'Pushing to "%s" is not allowed, use git-flow.',
1515
msgPrefixNotAllowed: 'Branch prefix "%s" is not allowed.',
1616
msgPrefixSuggestion: 'Instead of "%s" try "%s".',
17-
msgSeperatorRequired: 'Branch "%s" must contain a seperator "%s".'
17+
msgSeperatorRequired: 'Branch "%s" must contain a seperator "%s".',
18+
msgDoesNotMatchRegex: 'Does not match the regex given'
1819
};
1920

2021
this.options = Object.assign(defaultOptions, options);
@@ -23,6 +24,15 @@ class BranchNameLint {
2324
this.SUCCESS_CODE = 0;
2425
}
2526

27+
validateWithRegex() {
28+
if (this.options.regex) {
29+
const REGEX = new RegExp(this.options.regex);
30+
return REGEX.test(this.branch);
31+
}
32+
33+
return true;
34+
}
35+
2636
doValidation() {
2737
const parts = this.branch.split(this.options.seperator);
2838
const prefix = parts[0].toLowerCase();
@@ -47,6 +57,10 @@ class BranchNameLint {
4757
return this.error(this.options.msgSeperatorRequired, this.branch, this.options.seperator);
4858
}
4959

60+
if (!this.validateWithRegex()) {
61+
return this.error(this.options.msgBranchDisallowed, this.branch, this.options.regex);
62+
}
63+
5064
if (this.options.prefixes.includes(prefix) === false) {
5165
if (this.options.suggestions[prefix]) {
5266
this.error(

readme.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,20 @@ Any Valid JSON file with `branchNameLinter` attribute.
6464
}
6565
```
6666

67+
## Usage with regex
68+
69+
In order to check the branch name with a regex you can add a a regex as a string under the branchNameLinter in your config JSON.
70+
71+
```
72+
{
73+
"branchNameLinter": {
74+
"regex": "^([A-Z]+-[0-9]+.{5,70})"
75+
...
76+
"msgDoesNotMatchRegex": "Branch \"%s\" must contain certain characters \"%s\"."
77+
}
78+
}
79+
```
80+
6781
## Husky usage
6882

6983
After installation, just add in any husky hook as node modules call.
@@ -92,7 +106,7 @@ branchNameLint();
92106
#### options
93107

94108
Type: `object`
95-
Default:
109+
Default:
96110
```
97111
{
98112
prefixes: ['feature', 'hotfix', 'release'],

test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ test('error prints error', t => {
2424
t.is(answer, 1);
2525
});
2626

27+
test('validateWithRegex', t => {
28+
const branchNameLint = new BranchNameLint();
29+
branchNameLint.options.regex = '^([A-Z]+-[0-9]+.{5,70})';
30+
const validation = branchNameLint.validateWithRegex();
31+
t.falsy(validation);
32+
});
33+
2734
test('getCurrentBranch is working', t => {
2835
const childProcess = require('child_process');
2936
const branchNameLint = new BranchNameLint();

0 commit comments

Comments
 (0)