|
| 1 | +import json, re |
| 2 | +from pathlib import Path |
| 3 | +import uritemplate |
| 4 | + |
| 5 | +def github_action_notice(path, url, line): |
| 6 | + """ |
| 7 | + Return a GitHub action notice with file path, URL, and line number. |
| 8 | +
|
| 9 | + Parameters: |
| 10 | + path (str): File path. |
| 11 | + url (str): URL. |
| 12 | + line (int): Line number. |
| 13 | + """ |
| 14 | + return f"::warning file={path},line={line}::Annotation: {url}" |
| 15 | + |
| 16 | +def find_test_line_number(test_content, test_name): |
| 17 | + """ |
| 18 | + Find the line number of a test in the JSON content. |
| 19 | +
|
| 20 | + Parameters: |
| 21 | + test_content (str): JSON content. |
| 22 | + test_name (str): Test name. |
| 23 | +
|
| 24 | + Returns: |
| 25 | + int: Line number of the test. |
| 26 | + """ |
| 27 | + lines = test_content.split("\n") |
| 28 | + for i, line in enumerate(lines, start=1): |
| 29 | + if test_name in line: |
| 30 | + return i |
| 31 | + return 1 |
| 32 | + |
| 33 | +def clear_previous_annotations(): |
| 34 | + """ |
| 35 | + Clear previous GitHub action annotations. |
| 36 | + """ |
| 37 | + print("::remove-matcher owner=me::") |
| 38 | + |
| 39 | +json_file_path = Path("bin/specification_urls.json") |
| 40 | + |
| 41 | +BIN_DIR = Path(__file__).parent |
| 42 | +urls = json.loads(BIN_DIR.joinpath("specification_urls.json").read_text()) |
| 43 | + |
| 44 | +clear_previous_annotations() |
| 45 | + |
| 46 | +for file_path in Path("tests").rglob("*.json"): |
| 47 | + |
| 48 | + with file_path.open("r", encoding="utf-8") as f: |
| 49 | + changed_file_content = f.read() |
| 50 | + |
| 51 | + try: |
| 52 | + json_content = json.loads(changed_file_content) |
| 53 | + except json.JSONDecodeError: |
| 54 | + print(f"::error file={file_path}::Failed to parse JSON content") |
| 55 | + |
| 56 | + for test in json_content: |
| 57 | + if "specification" in test: |
| 58 | + line_number = find_test_line_number(changed_file_content, test.get("description") ) |
| 59 | + |
| 60 | + for specification_object in test["specification"]: |
| 61 | + for spec, section in specification_object.items(): |
| 62 | + draft = file_path.parent.name |
| 63 | + if spec in ["quote"]: |
| 64 | + continue |
| 65 | + elif spec in ["core", "validation", "hyper-schema"]: |
| 66 | + template = uritemplate.URITemplate(urls[draft][spec]) |
| 67 | + elif re.match("^rfc\\d+$", spec): |
| 68 | + template = uritemplate.URITemplate(urls["rfc"]) |
| 69 | + elif re.match("^iso\\d+$", spec): |
| 70 | + template = uritemplate.URITemplate(urls["iso"]) |
| 71 | + else: |
| 72 | + template = uritemplate.URITemplate(urls[spec]) |
| 73 | + url = template.expand(spec=spec, section=section) |
| 74 | + |
| 75 | + print(github_action_notice(file_path, url, line_number)) |
0 commit comments