File: annotate-specification-links

package info (click to toggle)
python-jsonschema 4.25.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,952 kB
  • sloc: python: 8,469; makefile: 210; sh: 12
file content (140 lines) | stat: -rwxr-xr-x 4,342 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python3
"""
Annotate pull requests to the GitHub repository with links to specifications.
"""

from __future__ import annotations

from pathlib import Path
from typing import Any
import json
import re
import sys

from uritemplate import URITemplate


BIN_DIR = Path(__file__).parent
TESTS = BIN_DIR.parent / "tests"
URLS = json.loads(BIN_DIR.joinpath("specification_urls.json").read_text())


def urls(version: str) -> dict[str, URITemplate]:
    """
    Retrieve the version-specific URLs for specifications.
    """
    for_version = {**URLS["json-schema"][version], **URLS["external"]}
    return {k: URITemplate(v) for k, v in for_version.items()}


def annotation(
    path: Path,
    message: str,
    line: int = 1,
    level: str = "notice",
    **kwargs: Any,
) -> str:
    """
    Format a GitHub annotation.

    See https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
    for full syntax.
    """

    if kwargs:
        additional = "," + ",".join(f"{k}={v}" for k, v in kwargs.items())
    else:
        additional = ""

    relative = path.relative_to(TESTS.parent)
    return f"::{level} file={relative},line={line}{additional}::{message}\n"


def line_number_of(path: Path, case: dict[str, Any]) -> int:
    """
    Crudely find the line number of a test case.
    """
    with path.open() as file:
        description = case["description"]
        return next(
            (i + 1 for i, line in enumerate(file, 1) if description in line),
            1,
        )

def extract_kind_and_spec(key: str) -> (str, str):
    """
    Extracts specification number and kind from the defined key
    """
    can_have_spec = ["rfc", "iso"]
    if not any(key.startswith(el) for el in can_have_spec):
        return key, ""
    number = re.search(r"\d+", key)
    spec = "" if number is None else number.group(0)
    kind = key.removesuffix(spec)
    return kind, spec


def main():
    # Clear annotations which may have been emitted by a previous run.
    sys.stdout.write("::remove-matcher owner=me::\n")

    for version in TESTS.iterdir():
        if version.name in {"draft-next", "latest"}:
            continue

        version_urls = urls(version.name)

        for path in version.rglob("*.json"):
            try:
                contents = json.loads(path.read_text())
            except json.JSONDecodeError as error:
                error = annotation(
                    level="error",
                    path=path,
                    line=error.lineno,
                    col=error.pos + 1,
                    title=str(error),
                    message=f"cannot load {path}"
                )
                sys.stdout.write(error)
                continue

            for test_case in contents:
                specifications = test_case.get("specification")
                if specifications is not None:
                    for each in specifications:
                        quote = each.pop("quote", "")
                        (key, section), = each.items()

                        (kind, spec) = extract_kind_and_spec(key)

                        url_template = version_urls[kind]
                        if url_template is None:
                            error = annotation(
                                level="error",
                                path=path,
                                line=line_number_of(path, test_case),
                                title=f"unsupported template '{kind}'",
                                message=f"cannot find a URL template for '{kind}'"
                            )
                            sys.stdout.write(error)
                            continue

                        url = url_template.expand(
                            spec=spec,
                            section=section,
                        )

                        message = f"{url}\n\n{quote}" if quote else url
                        sys.stdout.write(
                            annotation(
                                path=path,
                                line=line_number_of(path, test_case),
                                title="Specification Link",
                                message=message,
                            ),
                        )


if __name__ == "__main__":
    main()