forked from barzik/branch-name-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (76 loc) · 2.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const childProcess = require('child_process');
const util = require('util');
class BranchNameLint {
constructor(options) {
const defaultOptions = {
prefixes: ['feature', 'hotfix', 'release'],
suggestions: {features: 'feature', feat: 'feature', fix: 'hotfix', releases: 'release'},
banned: ['wip'],
skip: [],
disallowed: ['master', 'develop', 'staging'],
separator: '/',
msgBranchBanned: 'Branches with the name "%s" are not allowed.',
msgBranchDisallowed: 'Pushing to "%s" is not allowed, use git-flow.',
msgPrefixNotAllowed: 'Branch prefix "%s" is not allowed.',
msgPrefixSuggestion: 'Instead of "%s" try "%s".',
msgseparatorRequired: 'Branch "%s" must contain a separator "%s".',
msgDoesNotMatchRegex: 'Does not match the regex given'
};
this.options = Object.assign(defaultOptions, options);
this.branch = this.getCurrentBranch();
this.ERROR_CODE = 1;
this.SUCCESS_CODE = 0;
}
validateWithRegex() {
if (this.options.regex) {
const REGEX = new RegExp(this.options.regex);
return REGEX.test(this.branch);
}
return true;
}
doValidation() {
const parts = this.branch.split(this.options.separator);
const prefix = parts[0];
let name = null;
if (parts[1]) {
name = parts[1];
}
if (this.options.skip.length > 0 && this.options.skip.includes(this.branch)) {
return this.SUCCESS_CODE;
}
if (this.options.banned.includes(this.branch)) {
return this.error(this.options.msgBranchBanned, this.branch);
}
if (this.options.disallowed.includes(this.branch)) {
return this.error(this.options.msgBranchDisallowed, this.branch);
}
if (this.branch.includes(this.options.separator) === false) {
return this.error(this.options.msgseparatorRequired, this.branch, this.options.separator);
}
if (!this.validateWithRegex()) {
return this.error(this.options.msgBranchDisallowed, this.branch, this.options.regex);
}
if (this.options.prefixes.includes(prefix) === false) {
if (this.options.suggestions[prefix]) {
this.error(
this.options.msgPrefixSuggestion,
[prefix, name].join(this.options.separator),
[this.options.suggestions[prefix], name].join(this.options.separator)
);
} else {
this.error(this.options.msgPrefixNotAllowed, prefix);
}
return this.ERROR_CODE;
}
return this.SUCCESS_CODE;
}
getCurrentBranch() {
const branch = childProcess.execFileSync('git', ['rev-parse', '--abbrev-ref', 'HEAD']).toString();
return branch.trim();
}
error() {
console.error('Branch name lint fail!', Reflect.apply(util.format, null, arguments)); // eslint-disable-line prefer-rest-params
return this.ERROR_CODE;
}
}
module.exports = BranchNameLint;