|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +''' |
| 6 | +Check PR branch commit conventions and markdown linting. |
| 7 | +
|
| 8 | +Validates: |
| 9 | + - no merge commits |
| 10 | + - no fixup! commits |
| 11 | + - commit messages have a well-formed subsystem prefix before ':' |
| 12 | + - commit subject lines are <= 160 characters |
| 13 | + - changed markdown files pass markdownlint-cli2 |
| 14 | +
|
| 15 | +AP_FLAKE8_CLEAN |
| 16 | +''' |
| 17 | + |
| 18 | +import argparse |
| 19 | +import os |
| 20 | +import re |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | + |
| 24 | +import build_script_base |
| 25 | + |
| 26 | +DOCS_URL = "https://ardupilot.org/dev/docs/submitting-patches-back-to-master.html" |
| 27 | +MAX_SUBJECT_LEN = 160 |
| 28 | +# spaces and quotes allowed to support Revert commits e.g. 'Revert "AP_Periph: ...' |
| 29 | +PREFIX_RE = re.compile(r'^[-A-Za-z0-9._/" ]+$') |
| 30 | + |
| 31 | +# Enable colour when attached to a terminal or running under GitHub Actions |
| 32 | +_colour = sys.stdout.isatty() or os.environ.get('GITHUB_ACTIONS') == 'true' |
| 33 | +_GREEN = '\033[32m' if _colour else '' |
| 34 | +_RED = '\033[31m' if _colour else '' |
| 35 | +_YELLOW = '\033[33m' if _colour else '' |
| 36 | +_RESET = '\033[0m' if _colour else '' |
| 37 | + |
| 38 | +PASS = f"{_GREEN}✓{_RESET}" |
| 39 | +FAIL = f"{_RED}✗{_RESET}" |
| 40 | +SKIP = f"{_YELLOW}~{_RESET}" |
| 41 | + |
| 42 | + |
| 43 | +class CheckBranchConventions(build_script_base.BuildScriptBase): |
| 44 | + |
| 45 | + DEFAULT_UPSTREAM = "origin/master" |
| 46 | + |
| 47 | + def __init__(self, base_branch: str | None = None) -> None: |
| 48 | + super().__init__() |
| 49 | + self.base_branch = base_branch |
| 50 | + |
| 51 | + def progress_prefix(self) -> str: |
| 52 | + return "CBC" |
| 53 | + |
| 54 | + def run_git(self, args, show_output=True, source_dir=None): |
| 55 | + cmd_list = ["git"] + list(args) |
| 56 | + return self.run_program( |
| 57 | + "SCB-GIT", cmd_list, |
| 58 | + show_output=show_output, show_command=False, cwd=source_dir, |
| 59 | + ) |
| 60 | + |
| 61 | + def check_merge_commits(self) -> bool: |
| 62 | + merge_commits = self.run_git( |
| 63 | + ["log", f"{self.base_branch}..HEAD", "--merges", "--oneline"], |
| 64 | + show_output=False, |
| 65 | + ).strip() |
| 66 | + if merge_commits: |
| 67 | + print(f"{FAIL} Merge commits are not allowed:") |
| 68 | + for line in merge_commits.splitlines(): |
| 69 | + print(f" {line}") |
| 70 | + print(f" See: {DOCS_URL}") |
| 71 | + return False |
| 72 | + print(f"{PASS} No merge commits.") |
| 73 | + return True |
| 74 | + |
| 75 | + def check_fixup_commits(self, commits: str) -> bool: |
| 76 | + bad = [line for line in commits.splitlines() if "fixup!" in line] |
| 77 | + if bad: |
| 78 | + print(f"{FAIL} fixup! commits are not allowed:") |
| 79 | + for line in bad: |
| 80 | + print(f" {line}") |
| 81 | + print(f" See: {DOCS_URL}") |
| 82 | + return False |
| 83 | + print(f"{PASS} No fixup! commits.") |
| 84 | + return True |
| 85 | + |
| 86 | + def check_commit_messages(self, commits: str) -> bool: |
| 87 | + ok = True |
| 88 | + for line in commits.splitlines(): |
| 89 | + if not line.strip(): |
| 90 | + continue |
| 91 | + # strip leading hash from --oneline format |
| 92 | + subject = line.split(" ", 1)[1] if " " in line else line |
| 93 | + if ":" not in subject: |
| 94 | + print(f"{FAIL} Missing subsystem prefix: {line}") |
| 95 | + print(f" Reword to e.g. 'AP_Compass: {subject}'") |
| 96 | + print(f" See: {DOCS_URL}") |
| 97 | + ok = False |
| 98 | + continue |
| 99 | + prefix = subject.split(":")[0] |
| 100 | + if not PREFIX_RE.match(prefix): |
| 101 | + print(f"{FAIL} Malformed subsystem prefix '{prefix}': {line}") |
| 102 | + print(" Prefix must contain only letters, digits, '.', '_', '/', '-', spaces, quotes.") |
| 103 | + print(f" See: {DOCS_URL}") |
| 104 | + ok = False |
| 105 | + if ok: |
| 106 | + print(f"{PASS} All commit messages have well-formed subsystem tags.") |
| 107 | + return ok |
| 108 | + |
| 109 | + def check_commit_lengths(self, commits: str) -> bool: |
| 110 | + ok = True |
| 111 | + for line in commits.splitlines(): |
| 112 | + if not line.strip(): |
| 113 | + continue |
| 114 | + if len(line) > MAX_SUBJECT_LEN: |
| 115 | + print(f"{FAIL} Subject too long ({len(line)} chars, limit {MAX_SUBJECT_LEN}): {line}") |
| 116 | + ok = False |
| 117 | + if ok: |
| 118 | + print(f"{PASS} All commit subject lines within {MAX_SUBJECT_LEN} characters.") |
| 119 | + return ok |
| 120 | + |
| 121 | + def check_markdown(self) -> bool: |
| 122 | + changed_md = self.run_git( |
| 123 | + ["diff", "--name-only", "--diff-filter=AM", |
| 124 | + f"{self.base_branch}...HEAD", "--", "*.md"], |
| 125 | + show_output=False, |
| 126 | + ).strip() |
| 127 | + if not changed_md: |
| 128 | + print(f"{PASS} No markdown files changed.") |
| 129 | + return True |
| 130 | + |
| 131 | + try: |
| 132 | + result = subprocess.run(["markdownlint-cli2"] + changed_md.splitlines()) |
| 133 | + except FileNotFoundError: |
| 134 | + print(f"{SKIP} markdownlint-cli2 not installed.") |
| 135 | + return True |
| 136 | + if result.returncode != 0: |
| 137 | + print(f"{FAIL} Markdown linting errors found (see above).") |
| 138 | + return False |
| 139 | + |
| 140 | + print(f"{PASS} Markdown files pass linting.") |
| 141 | + return True |
| 142 | + |
| 143 | + def run(self) -> None: |
| 144 | + if self.base_branch is None: |
| 145 | + current = self.find_current_git_branch_or_sha1() |
| 146 | + self.base_branch = self.find_git_branch_merge_base(current, self.DEFAULT_UPSTREAM) |
| 147 | + self.progress(f"Using merge base with {self.DEFAULT_UPSTREAM}: {self.base_branch}") |
| 148 | + |
| 149 | + commits = self.run_git( |
| 150 | + ["log", f"{self.base_branch}..HEAD", "--oneline"], |
| 151 | + show_output=False, |
| 152 | + ).strip() |
| 153 | + |
| 154 | + n = len(commits.splitlines()) if commits else 0 |
| 155 | + print(f"\nChecking {n} commit(s) since {self.base_branch}...\n") |
| 156 | + |
| 157 | + results = [ |
| 158 | + self.check_merge_commits(), |
| 159 | + self.check_fixup_commits(commits), |
| 160 | + self.check_commit_messages(commits), |
| 161 | + self.check_commit_lengths(commits), |
| 162 | + self.check_markdown(), |
| 163 | + ] |
| 164 | + |
| 165 | + failures = results.count(False) |
| 166 | + print(f"\n{'All checks passed.' if not failures else f'{failures} check(s) failed.'}") |
| 167 | + sys.exit(0 if all(results) else 1) |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + parser = argparse.ArgumentParser( |
| 172 | + description="Check PR branch commit conventions and markdown linting", |
| 173 | + ) |
| 174 | + parser.add_argument( |
| 175 | + "--base-branch", |
| 176 | + default=None, |
| 177 | + help="Upstream base branch or commit to compare against " |
| 178 | + "(default: merge base of HEAD with origin/master)", |
| 179 | + ) |
| 180 | + args = parser.parse_args() |
| 181 | + CheckBranchConventions(args.base_branch).run() |
0 commit comments