Skip to content

Commit f276661

Browse files
committed
template library, url loads changes
1 parent 3a50900 commit f276661

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

bin/annotation_workflow.py

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import json, re
22
from pathlib import Path
3+
import uritemplate
34

4-
def github_action_notice(file, url, line):
5+
def github_action_notice(path, url, line):
56
"""
6-
Print GitHub action notice with file path, URL, and line number.
7+
Return a GitHub action notice with file path, URL, and line number.
78
89
Parameters:
9-
file (str): File path.
10+
path (str): File path.
1011
url (str): URL.
1112
line (int): Line number.
1213
"""
13-
return f"::warning file={file},line={line}::Annotation: {url}"
14+
return f"::warning file={path},line={line}::Annotation: {url}"
1415

1516
def find_test_line_number(test_content, test_name):
1617
"""
@@ -23,54 +24,52 @@ def find_test_line_number(test_content, test_name):
2324
Returns:
2425
int: Line number of the test.
2526
"""
26-
lines = test_content.split("\n") # Split content into lines
27-
for i, line in enumerate(lines, start=1): # Iterate over lines
28-
if test_name in line: # Check if test name is found in the line
29-
return i # Return the line number if found
30-
return 1 # Return None if test name is not found
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
3132

3233
def clear_previous_annotations():
3334
"""
3435
Clear previous GitHub action annotations.
3536
"""
3637
print("::remove-matcher owner=me::")
3738

38-
# Specify the path to the JSON file using pathlib.Path
3939
json_file_path = Path("bin/specification_urls.json")
4040

41-
# Read specification URLs from JSON file using pathlib.Path
42-
with json_file_path.open("r", encoding="utf-8") as f:
43-
urls = json.load(f)
41+
BIN_DIR = Path(__file__).parent
42+
urls = json.loads(BIN_DIR.joinpath("specification_urls.json").read_text())
43+
44+
clear_previous_annotations()
4445

45-
# Iterate through JSON files in tests folder and subdirectories
4646
for file_path in Path("tests").rglob("*.json"):
47-
# Read the file content using pathlib.Path
47+
4848
with file_path.open("r", encoding="utf-8") as f:
4949
changed_file_content = f.read()
50-
51-
# Parse JSON content
50+
5251
try:
5352
json_content = json.loads(changed_file_content)
54-
for test in json_content:
55-
if "specification" in test:
56-
line_number = find_test_line_number(changed_file_content, test.get("description") )
53+
except json.JSONDecodeError:
54+
print(f"::error file={file_path}::Failed to parse JSON content")
5755

58-
for specification_object in test["specification"]:
59-
for spec, section in specification_object.items():
60-
draft = file_path.parent.name
61-
if spec in ["quote"]:
62-
continue
63-
elif spec in ["core", "validation", "hyper-schema"]:
64-
url = urls[draft][spec].format(spec=spec, section=section)
65-
elif re.match("^rfc\\d+$", spec):
66-
url = urls["rfc"].format(spec=spec, section=section)
67-
elif re.match("^iso\\d+$", spec):
68-
url = urls["iso"].format(spec=spec, section=section)
69-
else:
70-
url = urls[spec].format(spec=spec, section=section)
56+
for test in json_content:
57+
if "specification" in test:
58+
line_number = find_test_line_number(changed_file_content, test.get("description") )
7159

72-
clear_previous_annotations()
73-
print(github_action_notice(file_path, url, line_number))
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)
7474

75-
except json.JSONDecodeError:
76-
print(f"::error file={file_path}::Failed to parse JSON content")
75+
print(github_action_notice(file_path, url, line_number))

0 commit comments

Comments
 (0)