|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Part of the EDG Compiler Project, under the Apache License v2.0 with LLVM |
| 4 | +# Exceptions. |
| 5 | +# See https://edgcpp.org/LICENSE.txt for license information. |
| 6 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | + |
| 8 | +'''Check that the issue forms and the discussion forms match. |
| 9 | +
|
| 10 | +Every form in .github/DISCUSSION_TEMPLATE has a twin of the same name in |
| 11 | +.github/ISSUE_TEMPLATE, so contributors who can file issues use the same form |
| 12 | +as everyone else. GitHub can't share one file between the two (issue forms |
| 13 | +need name/description keys discussion forms don't allow), so the two copies |
| 14 | +are kept by hand and this checks that their body: sections are identical, |
| 15 | +apart from links to the other forms (a discussion category in one, an issue |
| 16 | +form in the other). |
| 17 | +''' |
| 18 | + |
| 19 | +import argparse |
| 20 | +import difflib |
| 21 | +import re |
| 22 | +import sys |
| 23 | + |
| 24 | +from pathlib import Path |
| 25 | +from typing import List, Optional |
| 26 | + |
| 27 | +REPOSITORY_URL = 'https://github.com/edgcpp/compiler' |
| 28 | + |
| 29 | +# Links to another form, which differ between the twins by design. |
| 30 | +FORM_LINK_PATTERNS = [ |
| 31 | + re.compile(re.escape(REPOSITORY_URL) + |
| 32 | + r'/discussions/categories/([A-Za-z0-9_-]+)'), |
| 33 | + re.compile(re.escape(REPOSITORY_URL) + |
| 34 | + r'/issues/new\?template=([A-Za-z0-9_-]+)\.yml'), |
| 35 | +] |
| 36 | + |
| 37 | +# Files in ISSUE_TEMPLATE that aren't forms. |
| 38 | +NON_FORMS = {'config.yml'} |
| 39 | + |
| 40 | +# Top-level keys an issue form requires before its body. |
| 41 | +ISSUE_FORM_KEYS = ['name', 'description'] |
| 42 | + |
| 43 | + |
| 44 | +def form_body(path: Path) -> Optional[List[str]]: |
| 45 | + '''Return the lines from the body: key on, with form links normalized.''' |
| 46 | + lines = path.read_text(encoding = 'utf-8').splitlines(keepends = True) |
| 47 | + for index, line in enumerate(lines): |
| 48 | + if line.startswith('body:'): |
| 49 | + body = lines[index:] |
| 50 | + for pattern in FORM_LINK_PATTERNS: |
| 51 | + body = [pattern.sub(r'<form \1>', text) for text in body] |
| 52 | + return body |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +def missing_issue_form_keys(path: Path) -> List[str]: |
| 57 | + '''Return the required top-level keys path lacks before its body.''' |
| 58 | + header = [] |
| 59 | + for line in path.read_text(encoding = 'utf-8').splitlines(): |
| 60 | + if line.startswith('body:'): |
| 61 | + break |
| 62 | + header.append(line) |
| 63 | + return [ |
| 64 | + key for key in ISSUE_FORM_KEYS |
| 65 | + if not any(line.startswith(key + ':') for line in header) |
| 66 | + ] |
| 67 | + |
| 68 | + |
| 69 | +def main() -> int: |
| 70 | + parser = argparse.ArgumentParser(description = __doc__.splitlines()[0]) |
| 71 | + parser.add_argument( |
| 72 | + '--github-dir', |
| 73 | + type = Path, |
| 74 | + default = Path(__file__).resolve().parents[2] / '.github', |
| 75 | + help = 'the .github directory to check (default: this repository\'s)' |
| 76 | + ) |
| 77 | + args = parser.parse_args() |
| 78 | + |
| 79 | + discussion_dir = args.github_dir / 'DISCUSSION_TEMPLATE' |
| 80 | + issue_dir = args.github_dir / 'ISSUE_TEMPLATE' |
| 81 | + discussion_forms = {path.name for path in discussion_dir.glob('*.yml')} |
| 82 | + issue_forms = { |
| 83 | + path.name for path in issue_dir.glob('*.yml') |
| 84 | + if path.name not in NON_FORMS |
| 85 | + } |
| 86 | + |
| 87 | + errors = 0 |
| 88 | + for name in sorted(discussion_forms - issue_forms): |
| 89 | + print(f"error: {discussion_dir / name} has no twin in {issue_dir}", |
| 90 | + file = sys.stderr) |
| 91 | + errors += 1 |
| 92 | + for name in sorted(issue_forms - discussion_forms): |
| 93 | + print(f"error: {issue_dir / name} has no twin in {discussion_dir}", |
| 94 | + file = sys.stderr) |
| 95 | + errors += 1 |
| 96 | + |
| 97 | + for name in sorted(discussion_forms & issue_forms): |
| 98 | + discussion_form = discussion_dir / name |
| 99 | + issue_form = issue_dir / name |
| 100 | + missing = missing_issue_form_keys(issue_form) |
| 101 | + if len(missing) != 0: |
| 102 | + print(f"error: {issue_form} lacks the top-level key(s) " |
| 103 | + f"{', '.join(missing)}", file = sys.stderr) |
| 104 | + errors += 1 |
| 105 | + discussion_body = form_body(discussion_form) |
| 106 | + issue_body = form_body(issue_form) |
| 107 | + if discussion_body is None or issue_body is None: |
| 108 | + for path, body in ((discussion_form, discussion_body), |
| 109 | + (issue_form, issue_body)): |
| 110 | + if body is None: |
| 111 | + print(f"error: {path} has no body: key", file = sys.stderr) |
| 112 | + errors += 1 |
| 113 | + continue |
| 114 | + if discussion_body != issue_body: |
| 115 | + print(f"error: the body: sections of {discussion_form} and " |
| 116 | + f"{issue_form} differ:", file = sys.stderr) |
| 117 | + sys.stderr.writelines(difflib.unified_diff( |
| 118 | + discussion_body, |
| 119 | + issue_body, |
| 120 | + fromfile = str(discussion_form), |
| 121 | + tofile = str(issue_form) |
| 122 | + )) |
| 123 | + errors += 1 |
| 124 | + |
| 125 | + if errors != 0: |
| 126 | + return 1 |
| 127 | + print(f"{len(discussion_forms & issue_forms)} form(s) match.") |
| 128 | + return 0 |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == '__main__': |
| 132 | + sys.exit(main()) |
0 commit comments