lib/checks/check-pr-meta.py
174 lines · 5412 bytes
1"""Checks PR metadata: title format, draft status, template completeness, and checkboxes."""
2
3import json
4import os
5import re
6import subprocess
7import sys
8
9FINDINGS_PATH = "/tmp/findings-compliance.json"
10
11BAD_TITLES = {"update readme.md", "update awesome-privacy.yml"}
12
13TITLE_MSG = (
14 "The pull request title does not follow the format defined in our guidelines."
15 " Please rename it to `[Add/Remove/Update] [software name] in [software section]`"
16)
17DRAFT_MSG = (
18 "Please avoid opening WIP pull requests."
19 " Your PR should be 100% ready and complete before submitting"
20)
21TEMPLATE_MSG = (
22 "Please fill in pull request template in full."
23 " You can find a copy of this"
24 " [here](https://github.com/Lissy93/awesome-privacy/blob/main/.github/PULL_REQUEST_TEMPLATE.md)"
25)
26CHECKBOX_MSG = (
27 "Ensure you have completed the checklist (put a tick the checkboxes with `[x]`),"
28 " to confirm that you've read the contributing guidelines, checked your submission,"
29 " indicated your affiliation and agree to follow our CoC"
30)
31README_MSG = (
32 "Do not edit the README directly. This file is auto-generated from the"
33 " content in `awesome-privacy.yml`, and so your changes will be overridden!"
34 " Instead, only modify the YAML file, and be sure to follow our Contributing Guidelines."
35)
36BOT_MSG = (
37 "Submissions are only accepted from humans."
38 " This PR appears to have been authored by a bot or AI assistant."
39)
40
41_BOT_AUTHOR_RE = re.compile(
42 r"(?:noreply@anthropic\.com|devin-ai-integration|copilot-swe-agent|noreply@cursor\.com)",
43 re.IGNORECASE,
44)
45
46
47def extract_section(body, header):
48 """Extract content between a ### header and the next delimiter."""
49 pattern = rf"###\s*{re.escape(header)}\s*\n(.*?)(?=\n---|\n###|\Z)"
50 match = re.search(pattern, body, re.DOTALL)
51 return match.group(1) if match else None
52
53
54def strip_html_comments(text):
55 """Remove HTML comments from text."""
56 return re.sub(r"<!--.*?-->", "", text, flags=re.DOTALL).strip()
57
58
59def check_title(title):
60 """Return a finding if the PR title matches a known-bad pattern."""
61 if title and title.strip().lower() in BAD_TITLES:
62 return TITLE_MSG
63 return None
64
65
66def check_draft(draft_str):
67 """Return a finding if the PR is in draft state."""
68 if str(draft_str).lower() == "true":
69 return DRAFT_MSG
70 return None
71
72
73def check_template(body):
74 """Return a finding if required template sections are missing or empty."""
75 for header in ("Type", "Changes", "Checklist"):
76 content = extract_section(body, header)
77 if content is None or not strip_html_comments(content):
78 return TEMPLATE_MSG
79 return None
80
81
82def check_checkboxes(body):
83 """Return a finding if any checklist checkboxes are unchecked."""
84 section = extract_section(body, "Checklist")
85 if section is None:
86 return None
87 checked = re.findall(r"- \[x\]", section, re.IGNORECASE)
88 unchecked = re.findall(r"- \[ \]", section)
89 if not checked and not unchecked:
90 return None
91 if unchecked:
92 return CHECKBOX_MSG
93 return None
94
95
96def check_bot_coauthors(base_ref):
97 """Return a finding if any commit in the PR has a bot author or co-author."""
98 if not base_ref:
99 return None
100 try:
101 result = subprocess.run(
102 ["git", "log", f"{base_ref}..HEAD", "--format=%aN <%aE>%n%B"],
103 capture_output=True, text=True, timeout=30,
104 )
105 if result.returncode != 0:
106 return None
107 if _BOT_AUTHOR_RE.search(result.stdout):
108 return BOT_MSG
109 except Exception:
110 pass
111 return None
112
113
114def check_readme(readme_failed):
115 """Return a finding if the README check reported a failure."""
116 if readme_failed == "true":
117 return README_MSG
118 return None
119
120
121def write_findings(findings):
122 """Write the findings list to the output JSON file."""
123 with open(FINDINGS_PATH, "w") as f:
124 json.dump(findings, f)
125
126
127def main():
128 findings = []
129 critical = False
130 try:
131 title = os.environ.get("PR_TITLE", "")
132 body = os.environ.get("PR_BODY", "")
133 draft = os.environ.get("PR_DRAFT", "false")
134 readme_failed = os.environ.get("README_FAILED", "false")
135 base_ref = os.environ.get("BASE_REF", "")
136
137 finding = check_bot_coauthors(base_ref)
138 if finding:
139 findings.append(finding)
140
141 finding = check_title(title)
142 if finding:
143 findings.append({"msg": finding, "level": "error"})
144 critical = True
145
146 finding = check_draft(draft)
147 if finding:
148 findings.append(finding)
149
150 if not body or not body.strip():
151 findings.append({"msg": TEMPLATE_MSG, "level": "error"})
152 critical = True
153 else:
154 finding = check_template(body)
155 if finding:
156 findings.append({"msg": finding, "level": "error"})
157 critical = True
158 finding = check_checkboxes(body)
159 if finding:
160 findings.append({"msg": finding, "level": "error"})
161 critical = True
162
163 finding = check_readme(readme_failed)
164 if finding:
165 findings.append({"msg": finding, "level": "error"})
166 except Exception:
167 pass
168
169 write_findings(findings)
170 sys.exit(1 if critical else 0)
171
172
173if __name__ == "__main__":
174 main()