|
| 1 | +import os, re, sys |
| 2 | +from typing import List, Optional |
| 3 | + |
| 4 | + |
| 5 | +def is_checkbox(line: str) -> bool: |
| 6 | + return bool(re.match(r"^\s*-\s*\[[ xX]\]\s*.+", line)) |
| 7 | + |
| 8 | + |
| 9 | +def is_checked(line: str) -> bool: |
| 10 | + return bool(re.match(r"^\s*-\s*\[[xX]\]\s*.+", line)) |
| 11 | + |
| 12 | + |
| 13 | +def is_comment(line: str) -> bool: |
| 14 | + return bool(re.match(r"^\s*<!--.*-->\s*$", line)) |
| 15 | + |
| 16 | + |
| 17 | +def text_clean(lines: List[str]) -> str: |
| 18 | + text = [line for line in lines if not is_comment(line)] |
| 19 | + return "".join("".join(text).strip().split()) |
| 20 | + |
| 21 | + |
| 22 | +def validate_section(section_name: str, lines: List[str]) -> Optional[str]: |
| 23 | + has_checkboxes = any(is_checkbox(line) for line in lines) |
| 24 | + if has_checkboxes: |
| 25 | + if not any(is_checked(line) for line in lines): |
| 26 | + return f"Section {section_name} is a checklist without selections" |
| 27 | + return None |
| 28 | + if not text_clean(lines): |
| 29 | + return f"Section {section_name} is empty text section" |
| 30 | + return None |
| 31 | + |
| 32 | + |
| 33 | +def check_description(description: str) -> List[str]: |
| 34 | + if not description: |
| 35 | + # pull_request_template is not merged yet, so treat as valid for now |
| 36 | + return [] |
| 37 | + # return ["PR description is empty"] |
| 38 | + |
| 39 | + sections = [] |
| 40 | + current_section = None |
| 41 | + current_lines = [] |
| 42 | + errors = [] |
| 43 | + |
| 44 | + for line in description.splitlines(): |
| 45 | + header_match = re.match(r"^\s*##\s*(.+?)\s*$", line) |
| 46 | + if header_match: |
| 47 | + if current_section: |
| 48 | + sections.append((current_section, current_lines)) |
| 49 | + current_section = header_match.group(1) |
| 50 | + current_lines = [] |
| 51 | + elif current_section: |
| 52 | + current_lines.append(line) |
| 53 | + |
| 54 | + if current_section: |
| 55 | + sections.append((current_section, current_lines)) |
| 56 | + |
| 57 | + if not sections: |
| 58 | + return ["No sections available, template is empty"] |
| 59 | + |
| 60 | + for section_name, section_lines in sections: |
| 61 | + error = validate_section(section_name, section_lines) |
| 62 | + if error: |
| 63 | + errors.append(error) |
| 64 | + |
| 65 | + return errors |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + pr_description = os.getenv("PR_DESCRIPTION", "") |
| 70 | + |
| 71 | + errors = check_description(pr_description) |
| 72 | + if not errors: |
| 73 | + print("All good") |
| 74 | + exit(0) |
| 75 | + print("\n".join(errors)) |
| 76 | + exit(1) |
0 commit comments