-
Notifications
You must be signed in to change notification settings - Fork 1.5k
{CI} Add Verify PR title check #9750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| name: Verify PR Title | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, edited, synchronize] | ||
| branches: | ||
| - main | ||
|
|
||
| permissions: {} | ||
|
|
||
| jobs: | ||
| verify-pr-title: | ||
| runs-on: ubuntu-latest | ||
| name: Verify PR title format | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.11' | ||
|
|
||
| - name: Verify PR title | ||
| env: | ||
| PR_TITLE: ${{ github.event.pull_request.title }} | ||
| run: python -m verify_pr_title "$PR_TITLE" | ||
| working-directory: scripts/ci |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from .extractors import AfterPrefixExtractor, ComponentPrefixExtractor, Extractor, FullTitleExtractor | ||
| from .rule import Rule | ||
| from .rules import RULES | ||
| from .runner import run | ||
| from .verifiers import NegativeRegexVerifier, NonEmptyVerifier, RegexVerifier, Verifier | ||
|
|
||
| __all__ = [ | ||
| "Rule", | ||
| "Extractor", | ||
| "Verifier", | ||
| "FullTitleExtractor", | ||
| "ComponentPrefixExtractor", | ||
| "AfterPrefixExtractor", | ||
| "RegexVerifier", | ||
| "NegativeRegexVerifier", | ||
| "NonEmptyVerifier", | ||
| "RULES", | ||
| "run", | ||
| ] |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||||
| from __future__ import annotations | ||||||||
|
|
||||||||
| import sys | ||||||||
|
|
||||||||
| from .runner import run | ||||||||
|
|
||||||||
| _EXPECTED_FORMAT = ( | ||||||||
| "[Component] (BREAKING CHANGE: | Hotfix[:] | Fix #<N>[:])? <description>\n" | ||||||||
| "{Component} (BREAKING CHANGE: | Hotfix[:] | Fix #<N>[:])? <description>" | ||||||||
| ) | ||||||||
|
|
||||||||
|
|
||||||||
| def main() -> None: | ||||||||
| if len(sys.argv) < 2: | ||||||||
| print('Usage: python -m verify_pr_title "<PR title>"', file=sys.stderr) | ||||||||
| sys.exit(2) | ||||||||
|
|
||||||||
| title = sys.argv[1] | ||||||||
| failures = run(title) | ||||||||
|
|
||||||||
| if not failures: | ||||||||
| print(f"PR title validation passed: '{title}'") | ||||||||
| sys.exit(0) | ||||||||
|
|
||||||||
| print(f"PR title validation failed for: '{title}'\n") | ||||||||
| for name, error in failures: | ||||||||
| print(f" Rule: {name}") | ||||||||
| for line in error.splitlines(): | ||||||||
| print(f" {line}") | ||||||||
| print() | ||||||||
| print(f"Expected format:\n {_EXPECTED_FORMAT}") | ||||||||
|
||||||||
| print(f"Expected format:\n {_EXPECTED_FORMAT}") | |
| expected_format_indented = "\n".join(f" {line}" for line in _EXPECTED_FORMAT.splitlines()) | |
| print(f"Expected format:\n{expected_format_indented}") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from abc import ABC, abstractmethod | ||
|
|
||
|
|
||
| class Extractor(ABC): | ||
| @abstractmethod | ||
| def extract(self, title: str) -> str: ... | ||
|
|
||
|
|
||
| class FullTitleExtractor(Extractor): | ||
| def extract(self, title: str) -> str: | ||
| return title | ||
|
|
||
|
|
||
| class ComponentPrefixExtractor(Extractor): | ||
| _PATTERN = re.compile(r"^(\[.+?\]|\{.+?\})") | ||
|
|
||
| def extract(self, title: str) -> str: | ||
| m = self._PATTERN.match(title) | ||
| return m.group(1) if m else "" | ||
|
|
||
|
|
||
| class AfterPrefixExtractor(Extractor): | ||
| _PATTERN = re.compile(r"^(?:\[.+?\]|\{.+?\})\s*(.*)", re.DOTALL) | ||
|
|
||
|
Comment on lines
+18
to
+27
|
||
| def extract(self, title: str) -> str: | ||
| m = self._PATTERN.match(title) | ||
| return m.group(1) if m else title | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
| from .extractors import Extractor | ||
| from .verifiers import Verifier | ||
|
|
||
|
|
||
| @dataclass | ||
| class Rule: | ||
| name: str | ||
| extractor: Extractor | ||
| verifier: Verifier | ||
|
|
||
| def check(self, title: str) -> tuple[bool, str]: | ||
| value = self.extractor.extract(title) | ||
| return self.verifier.verify(value) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,44 @@ | ||||||
| from __future__ import annotations | ||||||
|
|
||||||
| from .extractors import AfterPrefixExtractor, FullTitleExtractor | ||||||
| from .rule import Rule | ||||||
| from .verifiers import RegexVerifier | ||||||
|
|
||||||
| RULES: list[Rule] = [ | ||||||
| Rule( | ||||||
| name="Component prefix present", | ||||||
| extractor=FullTitleExtractor(), | ||||||
| verifier=RegexVerifier( | ||||||
| pattern=r"^(\[.+?\]|\{.+?\})", | ||||||
|
||||||
| pattern=r"^(\[.+?\]|\{.+?\})", | |
| pattern=r"^(\[(?!\s*\])[^]]+\]|\{(?!\s*\})[^}]+\})", |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,13 @@ | ||||||||||
| from __future__ import annotations | ||||||||||
|
|
||||||||||
| from .rule import Rule | ||||||||||
| from .rules import RULES | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def run(title: str, rules: list[Rule] = RULES) -> list[tuple[str, str]]: | ||||||||||
|
||||||||||
| def run(title: str, rules: list[Rule] = RULES) -> list[tuple[str, str]]: | |
| def run(title: str, rules: list[Rule] | None = None) -> list[tuple[str, str]]: | |
| if rules is None: | |
| rules = RULES |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from abc import ABC, abstractmethod | ||
|
|
||
|
|
||
| class Verifier(ABC): | ||
| @abstractmethod | ||
| def verify(self, value: str) -> tuple[bool, str]: ... | ||
|
|
||
|
|
||
| class RegexVerifier(Verifier): | ||
| def __init__(self, pattern: str, error_message: str, *, fullmatch: bool = False) -> None: | ||
| self._regex = re.compile(pattern) | ||
| self._error_message = error_message | ||
| self._fullmatch = fullmatch | ||
|
|
||
| def verify(self, value: str) -> tuple[bool, str]: | ||
| fn = self._regex.fullmatch if self._fullmatch else self._regex.search | ||
| if fn(value): | ||
| return True, "" | ||
| return False, self._error_message | ||
|
|
||
|
|
||
| class NegativeRegexVerifier(Verifier): | ||
| def __init__(self, pattern: str, error_message: str) -> None: | ||
| self._regex = re.compile(pattern) | ||
| self._error_message = error_message | ||
|
|
||
| def verify(self, value: str) -> tuple[bool, str]: | ||
| if not self._regex.search(value): | ||
| return True, "" | ||
| return False, self._error_message | ||
|
|
||
|
|
||
| class NonEmptyVerifier(Verifier): | ||
| def __init__(self, error_message: str) -> None: | ||
| self._error_message = error_message | ||
|
|
||
| def verify(self, value: str) -> tuple[bool, str]: | ||
| if value.strip(): | ||
| return True, "" | ||
| return False, self._error_message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The printed "Expected format" string implies a required space after the prefix, but the current extraction/validation logic allows titles like "[Core]Fix #123: ..." (no space). Either enforce the space in the rules or adjust
_EXPECTED_FORMATso the CLI output matches what is actually accepted.