1
1
import json , re
2
2
from pathlib import Path
3
+ import uritemplate
3
4
4
- def github_action_notice (file , url , line ):
5
+ def github_action_notice (path , url , line ):
5
6
"""
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.
7
8
8
9
Parameters:
9
- file (str): File path.
10
+ path (str): File path.
10
11
url (str): URL.
11
12
line (int): Line number.
12
13
"""
13
- return f"::warning file={ file } ,line={ line } ::Annotation: { url } "
14
+ return f"::warning file={ path } ,line={ line } ::Annotation: { url } "
14
15
15
16
def find_test_line_number (test_content , test_name ):
16
17
"""
@@ -23,54 +24,52 @@ def find_test_line_number(test_content, test_name):
23
24
Returns:
24
25
int: Line number of the test.
25
26
"""
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
31
32
32
33
def clear_previous_annotations ():
33
34
"""
34
35
Clear previous GitHub action annotations.
35
36
"""
36
37
print ("::remove-matcher owner=me::" )
37
38
38
- # Specify the path to the JSON file using pathlib.Path
39
39
json_file_path = Path ("bin/specification_urls.json" )
40
40
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 ()
44
45
45
- # Iterate through JSON files in tests folder and subdirectories
46
46
for file_path in Path ("tests" ).rglob ("*.json" ):
47
- # Read the file content using pathlib.Path
47
+
48
48
with file_path .open ("r" , encoding = "utf-8" ) as f :
49
49
changed_file_content = f .read ()
50
-
51
- # Parse JSON content
50
+
52
51
try :
53
52
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" )
57
55
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" ) )
71
59
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 )
74
74
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