-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconventional_commits_check.py
More file actions
executable file
·75 lines (60 loc) · 1.75 KB
/
conventional_commits_check.py
File metadata and controls
executable file
·75 lines (60 loc) · 1.75 KB
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
#!/usr/bin/env python3
import argparse
import re
import string
import sys
RED = "\033[31m"
COMMIT_TYPES = {
'feat': r'^feat(\(.+\))?:',
'fix': r'^fix(\(.+\))?:',
'docs': r'^docs(\(.+\))?:',
'style': r'^style(\(.+\))?:',
'refactor': r'^refactor(\(.+\))?:',
'perf': r'^perf(\(.+\))?:',
'test': r'^test(\(.+\))?:',
'build': r'^build(\(.+\))?:',
'ci': r'^ci(\(.+\))?:',
'chore': r'^chore(\(.+\))?:',
'revert': '^revert: ',
}
EMOJIS = {
'feat': '✨',
'fix': '🐛',
'docs': '📚',
'style': '💅',
'refactor': '🧹',
'perf': '🚀',
'test': '🧪',
'build': '🏗️',
'ci': '👷',
'chore': '♻️',
'revert': '⏪',
'merge': '🔀',
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument('commit_message_file')
args = parser.parse_args()
with open(args.commit_message_file) as file:
commit_message = file.read()
commit_message = commit_message.strip(string.whitespace + ''.join(EMOJIS.values()))
commit_type = None
for commit, pattern in COMMIT_TYPES.items():
if re.match(pattern, commit_message):
commit_type = commit
break
if not commit_type:
print(
f'{RED}Commit message does not follow Conventional Commits rules.\n' \
f'It must begin with : {", ".join(COMMIT_TYPES.keys())}'
)
sys.exit(1)
emoji = EMOJIS.get(commit_type)
if emoji:
new_commit_message = f'{emoji} {commit_message}'
with open(args.commit_message_file, 'w') as file:
file.write(new_commit_message)
print('Commit message follows Conventional Commits rules and has been updated with an emoji.')
sys.exit(0)
if __name__ == '__main__':
main()