lib/checks/check-pr-meta.py

139 lines · 4235 bytes

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