1
- from github import Github
2
- import os
3
- import sys
4
1
import json
2
+ from pathlib import Path
5
3
import re
6
4
5
+ # Function to print GitHub action notice
6
+ def print_github_action_notice (file , url ):
7
+ print (f"::warning file={ file } ,line=1::Annotation: { url } " )
7
8
8
- def print_github_action_notice (file_name , message ):
9
- print (f"::notice file={ file_name } ::{ message } " )
9
+ # Read specification URLs from JSON file
10
+ with open ("specification_urls.json" , "r" ) as f :
11
+ urls = json .load (f )
10
12
11
- def main ():
12
-
13
- # Get GITHUB_TOKEN from environment variables automatically
14
- g = Github (os .environ .get ('GITHUB_TOKEN' ))
15
-
16
- # Get repository and pull request number from environment variables
17
- repo_name = os .environ .get ('GITHUB_REPOSITORY' )
18
-
19
- # Extract pull request number from GITHUB_REF if it's a pull request event
20
- event_name = os .environ .get ('GITHUB_EVENT_NAME' )
21
- if event_name == 'pull_request' :
22
- pull_request_number = os .environ .get ('GITHUB_REF' ).split ('/' )[- 2 ]
23
- else :
24
- print ("Not a pull request event." )
25
- sys .exit (1 )
26
-
27
- if not repo_name or not pull_request_number :
28
- print ("Repository name or pull request number not found in environment variables." )
29
- sys .exit (1 )
30
-
31
- # Get repository object
32
- repo = g .get_repo (repo_name )
33
-
34
- # Get the pull request object
35
- pr = repo .get_pull (int (pull_request_number ))
36
-
37
- # Get the list of changed files in the pull request
38
- changed_files = [file .filename for file in pr .get_files ()]
39
-
40
- print (changed_files )
41
- # Traverse each file in the 'tests' folder and print JSON content
42
- for file in changed_files :
43
- if file .startswith ('tests/' ):
13
+ # Iterate through files in tests folder
14
+ for root , dirs , files in Path ("tests" ).walk ():
15
+ for file in files :
16
+ if file .endswith ('.json' ): # Check if file is JSON
17
+ file_path = root / file
44
18
# Read the file content
45
- draft = file .split ('/' )[1 ]
46
-
47
- urls = json .loads (repo .get_contents ("specification_urls.json" ).decoded_content .decode ('utf-8' ))
48
-
49
- branch_name = pr .head .ref
50
- changed_file_content = repo .get_contents (file , ref = branch_name ).decoded_content .decode ('utf-8' )
19
+ with open (file_path , "r" ) as f :
20
+ changed_file_content = f .read ()
51
21
52
22
# Parse JSON content
53
23
try :
@@ -56,13 +26,16 @@ def main():
56
26
if "specification" in test :
57
27
for specification_object in test ["specification" ]:
58
28
for spec , section in specification_object .items ():
59
- if spec in ["core" , "validation" , "hyper-schema" ]: print_github_action_notice (file , urls [draft ][spec ] + section )
60
- elif spec in ["quote" ]: continue
61
- elif spec in ["ecma262" , "perl5" ]: print_github_action_notice (file , urls [spec ] + section )
62
- elif re .match ("^rfc\\ d+$" , spec ): print_github_action_notice (file , urls ["rfc" ] + spec + ".txt#" + section )
63
- else : print_github_action_notice (file , urls ["iso" ])
64
- except json .JSONDecodeError as e :
65
- print (f"Error parsing JSON in file '{ file } ': { e } " )
66
-
67
- if __name__ == "__main__" :
68
- main ()
29
+ draft = root .split ('/' )[- 1 ]
30
+ if spec in ["core" , "validation" , "hyper-schema" ]:
31
+ print_github_action_notice (file_path , urls [draft ][spec ] + section )
32
+ elif spec in ["quote" ]:
33
+ continue
34
+ elif spec in ["ecma262" , "perl5" ]:
35
+ print_github_action_notice (file_path , urls [spec ] + section )
36
+ elif re .match ("^rfc\\ d+$" , spec ):
37
+ print_github_action_notice (file_path , urls ["rfc" ] + spec + ".txt#" + section )
38
+ else :
39
+ print_github_action_notice (file_path , urls ["iso" ])
40
+ except json .JSONDecodeError :
41
+ print (f"Failed to parse JSON content for file: { file_path } " )
0 commit comments