|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Verify the commit-type allowlist stays in sync across two configs. |
| 3 | +
|
| 4 | +Seven prefixes are allowed on commits and PR titles: feat, fix, docs, |
| 5 | +test, refactor, chore, release. Two places enforce that list today: |
| 6 | +
|
| 7 | +1. ``[tool.commitizen].customize.schema_pattern`` in ``pyproject.toml`` — |
| 8 | + the commitizen regex (commit-msg hook, local). |
| 9 | +2. ``.github/workflows/pr-title.yml`` ``types:`` input to the |
| 10 | + ``amannn/action-semantic-pull-request`` step — the PR-title CI check. |
| 11 | +
|
| 12 | +Both are hand-maintained. Add a type in one, forget the other, and the |
| 13 | +two layers drift: commits fail locally but PR titles pass (or vice |
| 14 | +versa). ``docs/DEVELOPMENT.md`` explicitly warns these must stay in |
| 15 | +sync, but prose warnings drift too. |
| 16 | +
|
| 17 | +This script mirrors the ``check_required_contexts.py`` pattern from #72 |
| 18 | +for this second drift class. Fails CI when the two sets disagree in |
| 19 | +either direction. |
| 20 | +
|
| 21 | +Usage (from repo root): |
| 22 | +
|
| 23 | + uv run python .github/scripts/check_commit_types.py |
| 24 | +""" |
| 25 | + |
| 26 | +from __future__ import annotations |
| 27 | + |
| 28 | +import re |
| 29 | +import sys |
| 30 | +import tomllib |
| 31 | +from pathlib import Path |
| 32 | + |
| 33 | +import yaml |
| 34 | + |
| 35 | +PYPROJECT = Path("pyproject.toml") |
| 36 | +PR_TITLE_YML = Path(".github/workflows/pr-title.yml") |
| 37 | + |
| 38 | +# Matches the first alternation group in the commitizen schema_pattern. |
| 39 | +# Schema example: ^(feat|fix|rc2|hot-fix|...)(\([\w\-]+\))?!?:\s.+ |
| 40 | +# Captures: feat|fix|rc2|hot-fix|... |
| 41 | +# |
| 42 | +# Character class [a-z0-9\-|]+ allows: |
| 43 | +# - lowercase letters (standard types: feat, fix, docs, ...) |
| 44 | +# - digits (release-candidate patterns: rc2, v2, ...) |
| 45 | +# - hyphens (compound types: hot-fix, post-release, ...) |
| 46 | +# - the `|` separator |
| 47 | +# Widened from [a-z|]+ (#91): the tighter class would silently truncate |
| 48 | +# extraction when a future type contained digits or hyphens. |
| 49 | +_SCHEMA_ALTERNATION_RE = re.compile(r"\^\(([a-z0-9\-|]+)\)") |
| 50 | + |
| 51 | + |
| 52 | +def commitizen_types() -> set[str]: |
| 53 | + """Return the set of types allowed by the commitizen schema regex.""" |
| 54 | + data = tomllib.loads(PYPROJECT.read_text(encoding="utf-8")) |
| 55 | + schema: str = ( |
| 56 | + data.get("tool", {}) |
| 57 | + .get("commitizen", {}) |
| 58 | + .get("customize", {}) |
| 59 | + .get("schema_pattern", "") |
| 60 | + ) |
| 61 | + if not schema: |
| 62 | + msg = "[tool.commitizen].customize.schema_pattern not found in pyproject.toml" |
| 63 | + raise ValueError(msg) |
| 64 | + match = _SCHEMA_ALTERNATION_RE.search(schema) |
| 65 | + if not match: |
| 66 | + msg = ( |
| 67 | + "Could not extract the type alternation group from " |
| 68 | + f"schema_pattern: {schema!r}. Expected it to start with " |
| 69 | + "'^(<type>|<type>|...)'." |
| 70 | + ) |
| 71 | + raise ValueError(msg) |
| 72 | + types = {t for t in match.group(1).split("|") if t} |
| 73 | + # Defensive: a malformed pattern like "^(|feat)..." could produce an |
| 74 | + # empty type after split. If nothing survives the filter, raise rather |
| 75 | + # than return a silent-pass empty set that would trivially match an |
| 76 | + # empty set from the other extractor (#92). |
| 77 | + if not types: |
| 78 | + msg = ( |
| 79 | + "Empty type alternation extracted from schema_pattern. " |
| 80 | + f"Check pyproject.toml: {schema!r}" |
| 81 | + ) |
| 82 | + raise ValueError(msg) |
| 83 | + return types |
| 84 | + |
| 85 | + |
| 86 | +def pr_title_types() -> set[str]: |
| 87 | + """Return the set of types declared in the pr-title workflow.""" |
| 88 | + data = yaml.safe_load(PR_TITLE_YML.read_text(encoding="utf-8")) |
| 89 | + for job in data.get("jobs", {}).values(): |
| 90 | + for step in job.get("steps", []): |
| 91 | + uses = step.get("uses", "") |
| 92 | + if "action-semantic-pull-request" in uses: |
| 93 | + types_block: str = step.get("with", {}).get("types", "") |
| 94 | + types = { |
| 95 | + line.strip() for line in types_block.splitlines() if line.strip() |
| 96 | + } |
| 97 | + # An empty or whitespace-only `types:` block would return an |
| 98 | + # empty set and trivially match an empty commitizen set — |
| 99 | + # masking a real config error. Fail loudly instead (#92). |
| 100 | + if not types: |
| 101 | + msg = ( |
| 102 | + f"`types:` block in {PR_TITLE_YML} is empty or " |
| 103 | + "whitespace-only. Expected at least one commit type " |
| 104 | + "per line." |
| 105 | + ) |
| 106 | + raise ValueError(msg) |
| 107 | + return types |
| 108 | + msg = ( |
| 109 | + "Could not find an `amannn/action-semantic-pull-request` step in " |
| 110 | + f"{PR_TITLE_YML}. If the action was renamed or the file moved, " |
| 111 | + "update this script." |
| 112 | + ) |
| 113 | + raise ValueError(msg) |
| 114 | + |
| 115 | + |
| 116 | +def main() -> int: |
| 117 | + cz = commitizen_types() |
| 118 | + pr = pr_title_types() |
| 119 | + |
| 120 | + # Belt-and-braces safety net: both extractors raise on empty, but guard |
| 121 | + # against a future refactor that drops the raise (#92). |
| 122 | + if not cz or not pr: |
| 123 | + print( |
| 124 | + "::error::One or both extractors returned empty; sync check cannot " |
| 125 | + "proceed. commitizen_types() empty: " |
| 126 | + f"{not cz}; pr_title_types() empty: {not pr}." |
| 127 | + ) |
| 128 | + return 1 |
| 129 | + |
| 130 | + if cz == pr: |
| 131 | + print(f"Commit types in sync ({len(cz)} types): {sorted(cz)}") |
| 132 | + return 0 |
| 133 | + |
| 134 | + print( |
| 135 | + "::error::[tool.commitizen].customize.schema_pattern and " |
| 136 | + ".github/workflows/pr-title.yml types are out of sync" |
| 137 | + ) |
| 138 | + for name in sorted(cz - pr): |
| 139 | + print(f"::error:: + in commitizen only: {name!r}") |
| 140 | + for name in sorted(pr - cz): |
| 141 | + print(f"::error:: - in pr-title.yml only: {name!r}") |
| 142 | + print( |
| 143 | + "\nFix: update both the schema_pattern in pyproject.toml AND " |
| 144 | + "the `types` list in .github/workflows/pr-title.yml so they " |
| 145 | + "contain the same type names. See docs/DEVELOPMENT.md#commit-messages " |
| 146 | + "for the current allowed list." |
| 147 | + ) |
| 148 | + return 1 |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + sys.exit(main()) |
0 commit comments