-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathvalidate_commit_message.py
More file actions
97 lines (77 loc) · 3.18 KB
/
validate_commit_message.py
File metadata and controls
97 lines (77 loc) · 3.18 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# This file is managed by the plugin template.
# Do not edit.
import os
import re
import subprocess
import sys
import tomllib
import yaml
from pathlib import Path
from github import Github
def check_status(issue, repo, cherry_pick):
gi = repo.get_issue(int(issue))
if gi.pull_request:
sys.exit(f"Error: issue #{issue} is a pull request.")
if gi.closed_at and not cherry_pick:
print("Make sure to use 'git cherry-pick -x' when backporting a change.")
print(
"If a backport of a change requires a significant amount of rewriting, "
"consider creating a new issue."
)
sys.exit(f"Error: issue #{issue} is closed.")
def check_changelog(issue, CHANGELOG_EXTS):
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}'.")
def main() -> None:
TEMPLATE_CONFIG = yaml.safe_load(Path("template_config.yml").read_text())
GITHUB_ORG = TEMPLATE_CONFIG["github_org"]
PLUGIN_NAME = TEMPLATE_CONFIG["plugin_name"]
with Path("pyproject.toml").open("rb") as _fp:
PYPROJECT_TOML = tomllib.load(_fp)
KEYWORDS = ["fixes", "closes"]
BLOCKING_REGEX = [
r"^DRAFT",
r"^WIP",
r"^NOMERGE",
r"^DO\s*NOT\s*MERGE",
r"^EXPERIMENT",
r"^FIXUP",
r"^fixup!", # This is created by 'git commit --fixup'
r"Apply suggestions from code review", # This usually comes from GitHub
]
try:
CHANGELOG_EXTS = [
f".{item['directory']}" for item in PYPROJECT_TOML["tool"]["towncrier"]["type"]
]
except KeyError:
CHANGELOG_EXTS = [".feature", ".bugfix", ".doc", ".removal", ".misc"]
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(f"Do not add '{NOISSUE_MARKER}' in the commit message.")
blocking_matches = [m for m in (re.match(pattern, message) for pattern in BLOCKING_REGEX) if m]
if blocking_matches:
print("Found these phrases in the commit message:")
for m in blocking_matches:
print(" - " + m.group(0))
sys.exit("This PR is not ready for consumption.")
g = Github(os.environ.get("GITHUB_TOKEN"))
repo = g.get_repo(f"{GITHUB_ORG}/{PLUGIN_NAME}")
print("Checking commit message for {sha}.".format(sha=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:
check_status(issue, repo, cherry_pick)
check_changelog(issue, CHANGELOG_EXTS)
print("Commit message for {sha} passed.".format(sha=sha[0:7]))
if __name__ == "__main__":
main()