-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathvalidate_commit_message.py
executable file
·87 lines (70 loc) · 2.64 KB
/
validate_commit_message.py
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
# WARNING: DO NOT EDIT!
#
# This file was generated by plugin_template, and is managed by it. Please use
# './plugin-template --github pulp_container' to update this file.
#
# For more info visit https://github.com/pulp/plugin_template
import os
import re
import subprocess
import sys
import tomllib
from pathlib import Path
from github import Github
with open("pyproject.toml", "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("pulp/pulp_container")
def check_status(issue):
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):
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
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("Commit message for {sha} passed.".format(sha=sha[0:7]))