|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +Minimal PR title validator for CI gate. |
| 6 | +
|
| 7 | +Rules: |
| 8 | +- Accept either a strict task title with required prefix '[TASK]' |
| 9 | + Pattern: [TASK] <Task>-<Variant>. <Last> <First> <Middle>. <Group>. <Task name> |
| 10 | +- Or a development title with prefix '[DEV]' followed by any non-empty text |
| 11 | + Pattern: [DEV] <any text> |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import argparse |
| 17 | +import json |
| 18 | +import os |
| 19 | +import re |
| 20 | +import sys |
| 21 | +from typing import List, Optional |
| 22 | + |
| 23 | + |
| 24 | +TITLE_TASK_REGEX = r""" |
| 25 | +^\[TASK\]\s+ |
| 26 | +(?P<task>\d+)-(?P<variant>\d+)\.\s+ |
| 27 | +(?P<lastname>[А-ЯA-ZЁ][а-яa-zё]+)\s+ |
| 28 | +(?P<firstname>[А-ЯA-ZЁ][а-яa-zё]+)\s+ |
| 29 | +(?P<middlename>[А-ЯA-ZЁ][а-яa-zё]+)\.\s+ |
| 30 | +(?P<group>.+?)\.\s+ |
| 31 | +(?P<taskname>\S.*) |
| 32 | +$ |
| 33 | +""" |
| 34 | + |
| 35 | +TITLE_DEV_REGEX = r"^\[DEV\]\s+\S.*$" |
| 36 | + |
| 37 | + |
| 38 | +def _trim(s: Optional[str]) -> str: |
| 39 | + return (s or "").strip() |
| 40 | + |
| 41 | + |
| 42 | +def validate_title(title: str) -> List[str]: |
| 43 | + """Validate PR title. Returns a list of error messages (empty if valid).""" |
| 44 | + title = (title or "").strip() |
| 45 | + if not title: |
| 46 | + return [ |
| 47 | + "Empty PR title. Use '[TASK] …' for tasks or '[DEV] …' for development.", |
| 48 | + ] |
| 49 | + |
| 50 | + # Accept development titles with a simple rule |
| 51 | + if re.match(TITLE_DEV_REGEX, title, flags=re.UNICODE): |
| 52 | + return [] |
| 53 | + |
| 54 | + # Accept strict course task titles |
| 55 | + if re.match(TITLE_TASK_REGEX, title, flags=re.UNICODE | re.VERBOSE): |
| 56 | + return [] |
| 57 | + |
| 58 | + example_task_ru = ( |
| 59 | + "[TASK] 2-12. Иванов Иван Иванович. 2341-а234. Вычисление суммы элементов вектора." |
| 60 | + ) |
| 61 | + example_task_en = ( |
| 62 | + "[TASK] 2-12. Ivanov Ivan Ivanovich. 2341-a234. Vector elements sum calculation." |
| 63 | + ) |
| 64 | + example_dev = "[DEV] Update docs for lab 2" |
| 65 | + return [ |
| 66 | + "Invalid PR title.", |
| 67 | + "Allowed formats:", |
| 68 | + f"- Task: {example_task_ru}", |
| 69 | + f"- Task: {example_task_en}", |
| 70 | + f"- Dev: {example_dev}", |
| 71 | + ] |
| 72 | + |
| 73 | + |
| 74 | +def _load_event_payload(path: Optional[str]) -> Optional[dict]: |
| 75 | + if not path or not os.path.exists(path): |
| 76 | + return None |
| 77 | + with open(path, "r", encoding="utf-8") as f: |
| 78 | + try: |
| 79 | + return json.load(f) |
| 80 | + except Exception: |
| 81 | + return None |
| 82 | + |
| 83 | + |
| 84 | +def main() -> int: |
| 85 | + try: |
| 86 | + payload = _load_event_payload(os.environ.get("GITHUB_EVENT_PATH")) |
| 87 | + |
| 88 | + pr_title = None |
| 89 | + if payload and payload.get("pull_request"): |
| 90 | + pr = payload["pull_request"] |
| 91 | + pr_title = pr.get("title") |
| 92 | + |
| 93 | + if pr_title is None: |
| 94 | + # Not a PR context – do not fail the gate |
| 95 | + print("No PR title in event payload; skipping title check (non-PR event).") |
| 96 | + return 0 |
| 97 | + |
| 98 | + errs = validate_title(pr_title) |
| 99 | + if errs: |
| 100 | + for e in errs: |
| 101 | + print(f"✗ {e}") |
| 102 | + return 1 |
| 103 | + |
| 104 | + print("OK: PR title is valid.") |
| 105 | + return 0 |
| 106 | + |
| 107 | + except SystemExit: |
| 108 | + raise |
| 109 | + except Exception as e: |
| 110 | + print(f"Internal error occurred: {e}", file=sys.stderr) |
| 111 | + return 2 |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + sys.exit(main()) |
0 commit comments