-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalidate_commit_message.py
More file actions
75 lines (58 loc) · 2.06 KB
/
validate_commit_message.py
File metadata and controls
75 lines (58 loc) · 2.06 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
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "pygithub>=2.8.1,<3.0.0",
# ]
# ///
import os
import re
import subprocess
import sys
from pathlib import Path
import tomllib
from github import Github
with open("pyproject.toml", "rb") as fp:
PYPROJECT_TOML = tomllib.load(fp)
KEYWORDS = ["fixes", "closes"]
BLOCKING_REGEX = [
"DRAFT",
"WIP",
"NOMERGE",
r"DO\s*NOT\s*MERGE",
"EXPERIMENT",
]
CHANGELOG_EXTS = [f".{item['directory']}" for item in PYPROJECT_TOML["tool"]["towncrier"]["type"]]
NOISSUE_MARKER = "[noissue]"
sha = sys.argv[1]
message = subprocess.check_output(["git", "log", "--format=%B", "-n 1", sha]).decode("utf-8")
if NOISSUE_MARKER in message:
sys.exit("Do not add '[noissue]' in the commit message.")
if any(re.match(pattern, message) for pattern in BLOCKING_REGEX):
sys.exit("This PR is not ready for consumption.")
g = Github(os.environ.get("GITHUB_TOKEN"))
repo = g.get_repo("pulp/pulp-cli-maven")
def check_status(issue: str) -> None:
gi = repo.get_issue(int(issue))
if gi.pull_request:
sys.exit(f"Error: issue #{issue} is a pull request.")
if gi.closed_at:
sys.exit(f"Error: issue #{issue} is closed.")
def check_changelog(issue: str) -> None:
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(f"Checking commit message for {sha[0:7]}.")
# validate the issue attached to the commit
issue_regex = r"(?:{keywords})[\s:]+#(\d+)".format(keywords=("|").join(KEYWORDS))
issues = re.findall(issue_regex, message, re.IGNORECASE)
cherry_pick_regex = r"^\s*\(cherry picked from commit [0-9a-f]*\)\s*$"
cherry_pick = re.search(cherry_pick_regex, message, re.MULTILINE)
if issues:
for issue in issues:
if not cherry_pick:
check_status(issue)
check_changelog(issue)
print(f"Commit message for {sha[0:7]} passed.")