This repository was archived by the owner on Feb 10, 2025. It is now read-only.
forked from pulp/pulp_python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidate_commit_message.py
More file actions
74 lines (59 loc) · 2.5 KB
/
Copy pathvalidate_commit_message.py
File metadata and controls
74 lines (59 loc) · 2.5 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
# WARNING: DO NOT EDIT!
#
# This file was generated by plugin_template, and is managed by it. Please use
# './plugin-template --github pulp_python' to update this file.
#
# For more info visit https://github.com/pulp/plugin_template
import re
import requests
import subprocess
import sys
from pathlib import Path
KEYWORDS = ["fixes", "closes", "re", "ref"]
NO_ISSUE = "[noissue]"
STATUSES = ["NEW", "ASSIGNED", "POST", "MODIFIED"]
REDMINE_URL = "https://pulp.plan.io"
CHANGELOG_EXTS = [".feature", ".bugfix", ".doc", ".removal", ".misc", ".deprecation"]
sha = sys.argv[1]
project = ""
message = subprocess.check_output(["git", "log", "--format=%B", "-n 1", sha]).decode("utf-8")
def __check_status(issue):
response = requests.get(f"{REDMINE_URL}/issues/{issue}.json")
response.raise_for_status()
bug_json = response.json()
status = bug_json["issue"]["status"]["name"]
if status not in STATUSES:
sys.exit(
"Error: issue #{issue} has invalid status of {status}. Status must be one of "
"{statuses}.".format(issue=issue, status=status, statuses=", ".join(STATUSES))
)
if project:
project_id = bug_json["issue"]["project"]["id"]
project_json = requests.get(f"{REDMINE_URL}/projects/{project_id}.json").json()
if project_json["project"]["identifier"] != project:
sys.exit(f"Error: issue {issue} is not in the {project} project.")
def __check_changelog(issue):
matches = list(Path("CHANGES").rglob(f"{issue}.*"))
if len(matches) < 1:
sys.exit(f"Could not find changelog entry in CHANGES/ for {issue}.")
for match in matches:
if match.suffix not in CHANGELOG_EXTS:
sys.exit(f"Invalid extension for changelog entry '{match}'.")
print("Checking commit message for {sha}.".format(sha=sha[0:7]))
# validate the issue attached to the commit
regex = r"(?:{keywords})[\s:]+#(\d+)".format(keywords=("|").join(KEYWORDS))
pattern = re.compile(regex, re.IGNORECASE)
issues = pattern.findall(message)
if issues:
for issue in pattern.findall(message):
__check_status(issue)
__check_changelog(issue)
else:
if NO_ISSUE in message:
print("Commit {sha} has no issues but is tagged {tag}.".format(sha=sha[0:7], tag=NO_ISSUE))
else:
sys.exit(
"Error: no attached issues found for {sha}. If this was intentional, add "
" '{tag}' to the commit message.".format(sha=sha[0:7], tag=NO_ISSUE)
)
print("Commit message for {sha} passed.".format(sha=sha[0:7]))