Repository navigation
Expand file tree
/
Copy pathcommit-message.py
More file actions
executable file
·53 lines (46 loc) · 2.07 KB
/
Copy pathcommit-message.py
File metadata and controls
executable file
·53 lines (46 loc) · 2.07 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
#!/usr/bin/env python
import configparser
import os
import sys
import re
helper = __import__("commit-helper")
COMMIT_BASE_PATTERN = "[A-Z][\w\d'\" -,.]+[\w'\"\)\]\}`]"
COMMIT_ISSUE_SUBJECT_PATTERN = "^{}$".format(COMMIT_BASE_PATTERN)
COMMIT_MASTER_SUBJECT_PATTERN = "^#([0-9]+): {}$".format(COMMIT_BASE_PATTERN)
commit_msg_file = sys.argv[1]
# parse config
try:
config = configparser.ConfigParser()
config.read(".git/hooks/gitlab.conf")
GITLAB_PROJECT_ID = config.get("gitlab", "projectid")
GITLAB_API_URL = config.get("gitlab", "url")
GITLAB_API_TOKEN = config.get("gitlab", "token")
except configparser.NoSectionError:
helper.die("Error while reading/parsing .git/hooks/gitlab.conf")
git = helper.Git(GITLAB_API_URL, GITLAB_API_TOKEN, GITLAB_PROJECT_ID)
branch = git.get_branch(os.getcwd())
with open(commit_msg_file, "r") as f:
commit_subject = f.readline()
if branch == "master":
git.check_commit_subject(COMMIT_MASTER_SUBJECT_PATTERN, commit_subject,
diemessage="Commit subject is not of proper form."
" An example: \"#ID: Subject\".\nExact regex to "
"match: "
"\"{}\"".format(COMMIT_MASTER_SUBJECT_PATTERN))
issue_id = re.match(COMMIT_MASTER_SUBJECT_PATTERN,
commit_subject).group(1)
git.check_commit_issue(issue_id)
else:
capture_issue_id = re.search("^issue/(\d+)$", branch)
if capture_issue_id:
issue_id = capture_issue_id.group(1)
git.check_commit_subject(COMMIT_ISSUE_SUBJECT_PATTERN, commit_subject,
"Commit subject is not of proper form. "
"An example: \"Subject\" (so without an "
"\"#ID:\"-prefix)\n"
"Exact regex to match: \""
"{}\"".format(COMMIT_ISSUE_SUBJECT_PATTERN))
git.check_commit_issue(issue_id)
else:
helper.die("Unsupported branch, either use master or an issue"
" branch.")