-
Notifications
You must be signed in to change notification settings - Fork 17
/
check-commit-msg.js
72 lines (61 loc) · 1.5 KB
/
check-commit-msg.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
/**
* This husky hook checks if your commit message is well-formatted
* The goals are to improve readability, consistency and integration with project managment processes
*/
// CONFIGURATION
const keywords = [
'Add',
'Allow',
'Change',
'Clean',
'Connect',
'Disable',
'Disallow',
'Document',
'Enable',
'Factor out',
'Fix',
'Hook in',
'Implement',
'Improve',
'Integrate',
'Make',
'Prepare',
'Re-add',
'Refactor',
'Refine',
'Remove',
'Rename',
'Run',
'Simplify',
'Try',
'Update',
'Upgrade',
'Use',
'Write',
]
// READ ENVIRONMENT
const process = require('process')
const fs = require('fs')
// get the husky params which is pointing to the file with the commit msg
const huskyParams = process.argv[2]
// read the commit message given by the user
const msg = fs.readFileSync(huskyParams, 'utf8')
const GITHUB_TAG = 'GH-[1-9][0-9]+'
// MAKE REGEX AND CHECK BRANCH NAME
// enforce giving github issue context if available and an uppercase descriptive verb at the start
const regex = new RegExp(
`^(${GITHUB_TAG}|no-issue)(;${GITHUB_TAG})*: (${keywords.join(
'|'
)}) [ 0-9a-zA-Z]+`
)
// if we have matches it's valid
const isValid = msg.match(regex) !== null
// GIVE USER FEEDBACK
// if it's valid tell the user
if (isValid) {
console.info('Commit message is 👍 👍 👌')
} else {
// if it's invalid annoy the user
throw `Your commit message should match ${regex.toString()}`
}