|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Validate pull request title and body. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + python3 validate_pr.py --title "scope: description" --body-file body.md |
| 7 | + python3 validate_pr.py --title "scope: description" --body "## Description ..." |
| 8 | +
|
| 9 | +Exits with code 1 and prints a human-readable error summary when |
| 10 | +validation fails. |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import re |
| 15 | +import sys |
| 16 | + |
| 17 | +TITLE_PATTERN = re.compile( |
| 18 | + r"^([a-z]+|[A-Z][a-zA-Z0-9]*(\{[^}]+\})?): .+" |
| 19 | +) |
| 20 | +FORBIDDEN_INDICATORS = re.compile( |
| 21 | + r"\b(wip|fixup|squash|tmp|todo|hack|xxx|do not merge|dnm|draft)\b", |
| 22 | + re.IGNORECASE, |
| 23 | +) |
| 24 | +HTML_COMMENT = re.compile(r"<!--[\s\S]*?-->") |
| 25 | +SECTION_RE = re.compile(r"## {}\s*\n([\s\S]*?)(?=\n## |$)") |
| 26 | +GPU_HEADER = re.compile(r"###\s+.+/.+/.+") |
| 27 | + |
| 28 | +REQUIRED_TEST_FIELDS = [ |
| 29 | + "Total Tests", |
| 30 | + "Passed", |
| 31 | + "Failed", |
| 32 | + "Success Rate", |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +def extract_section(body, name): |
| 37 | + """Return the content of a markdown ## section, stripped of HTML comments.""" |
| 38 | + match = re.search(SECTION_RE.pattern.format(re.escape(name)), body) |
| 39 | + if not match: |
| 40 | + return "" |
| 41 | + return HTML_COMMENT.sub("", match.group(1)).strip() |
| 42 | + |
| 43 | + |
| 44 | +def validate_title(title): |
| 45 | + """Validate the PR title and return a list of error strings.""" |
| 46 | + errors = [] |
| 47 | + |
| 48 | + if not TITLE_PATTERN.match(title): |
| 49 | + errors.append( |
| 50 | + "**Title** — must match pattern `scope: description`\n" |
| 51 | + " - scope: lowercase (cmake, ci, docs) OR " |
| 52 | + "PascalCase (VkVideoDecoder, FindShaderc)\n" |
| 53 | + " - Example: `cmake: fix build issue` or " |
| 54 | + "`VkVideoDecoder: add H265 support`\n" |
| 55 | + f" - Got: `{title}`" |
| 56 | + ) |
| 57 | + |
| 58 | + match = FORBIDDEN_INDICATORS.search(title) |
| 59 | + if match: |
| 60 | + errors.append( |
| 61 | + f"**Title** — must not contain draft indicators like `{match.group(0)}`\n" |
| 62 | + " - Forbidden: WIP, fixup, squash, tmp, todo, hack, " |
| 63 | + "xxx, do not merge, dnm, draft" |
| 64 | + ) |
| 65 | + |
| 66 | + if len(title) > 100: |
| 67 | + errors.append( |
| 68 | + f"**Title** — must not exceed 100 characters (currently {len(title)})." |
| 69 | + ) |
| 70 | + |
| 71 | + if title.endswith("."): |
| 72 | + errors.append("**Title** — must not end with a period.") |
| 73 | + |
| 74 | + return errors |
| 75 | + |
| 76 | + |
| 77 | +def validate_body(body): |
| 78 | + """Validate the PR body sections and return a list of error strings.""" |
| 79 | + errors = [] |
| 80 | + |
| 81 | + # --- Description (required) --- |
| 82 | + if not extract_section(body, "Description"): |
| 83 | + errors.append("**Description** — please describe your changes.") |
| 84 | + |
| 85 | + # --- Type of change (required) --- |
| 86 | + if not extract_section(body, "Type of change"): |
| 87 | + errors.append( |
| 88 | + "**Type of change** — please specify: " |
| 89 | + "bug fix / feature / refactor / docs / cleanup." |
| 90 | + ) |
| 91 | + |
| 92 | + # --- Tests (required) --- |
| 93 | + tests_content = extract_section(body, "Tests") |
| 94 | + if not tests_content: |
| 95 | + errors.append("**Tests** — please provide your test results.") |
| 96 | + else: |
| 97 | + missing = [ |
| 98 | + field |
| 99 | + for field in REQUIRED_TEST_FIELDS |
| 100 | + if not re.search(rf"{field}\s*:\s*\S+", tests_content, re.IGNORECASE) |
| 101 | + ] |
| 102 | + if missing: |
| 103 | + errors.append( |
| 104 | + f"**Tests** — missing or empty fields: {', '.join(missing)}.\n" |
| 105 | + " Expected format:\n" |
| 106 | + " ```\n" |
| 107 | + " Total Tests: 70\n" |
| 108 | + " Passed: 48\n" |
| 109 | + " Failed: 0\n" |
| 110 | + " Success Rate: 100.0%\n" |
| 111 | + " ```" |
| 112 | + ) |
| 113 | + |
| 114 | + if not GPU_HEADER.search(tests_content): |
| 115 | + errors.append( |
| 116 | + "**Tests** — please include a header with GPU / Driver / OS.\n" |
| 117 | + " Example: `### NVIDIA GeForce RTX 3050 Ti Laptop GPU " |
| 118 | + "/ NVIDIA 570.123.19 / Ubuntu 24.04.3 LTS`" |
| 119 | + ) |
| 120 | + |
| 121 | + return errors |
| 122 | + |
| 123 | + |
| 124 | +def main(): |
| 125 | + parser = argparse.ArgumentParser(description="Validate PR title and body") |
| 126 | + parser.add_argument("--title", required=True, help="Pull request title") |
| 127 | + body_group = parser.add_mutually_exclusive_group(required=True) |
| 128 | + body_group.add_argument("--body", help="Pull request body as a string") |
| 129 | + body_group.add_argument("--body-file", help="Path to a file containing the PR body") |
| 130 | + args = parser.parse_args() |
| 131 | + |
| 132 | + title = args.title |
| 133 | + if args.body_file: |
| 134 | + with open(args.body_file, encoding="utf-8") as f: |
| 135 | + body = f.read() |
| 136 | + else: |
| 137 | + body = args.body |
| 138 | + |
| 139 | + errors = validate_title(title) + validate_body(body) |
| 140 | + |
| 141 | + if errors: |
| 142 | + print("❌ PR validation failed. Please fix the following:\n") |
| 143 | + for error in errors: |
| 144 | + print(f"- {error}") |
| 145 | + sys.exit(1) |
| 146 | + |
| 147 | + print("✅ PR title and body validation passed.") |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + main() |
0 commit comments