|
1 |
| -from github import Github |
2 |
| -import os |
3 |
| -import sys |
4 | 1 | import json
|
| 2 | +import os |
5 | 3 | import re
|
| 4 | +from pathlib import Path |
6 | 5 |
|
| 6 | +# Function to print GitHub action notice |
| 7 | +def print_github_action_notice(file, url): |
| 8 | + print(f"::warning file={file},line=1::Annotation: {url}") |
7 | 9 |
|
8 |
| -def print_github_action_notice(file_name, message): |
9 |
| - print(f"::notice file={file_name}::{message}") |
10 |
| - |
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) |
| 10 | +# Read specification URLs from JSON file |
| 11 | +with open("bin/specification_urls.json", "r") as f: |
| 12 | + urls = json.load(f) |
33 | 13 |
|
34 |
| - # Get the pull request object |
35 |
| - pr = repo.get_pull(int(pull_request_number)) |
| 14 | +# Iterate through JSON files in tests folder and subdirectories |
| 15 | +for root, dirs, files in os.walk("tests"): |
| 16 | + for file_name in files: |
| 17 | + if file_name.endswith(".json"): |
| 18 | + file_path = os.path.join(root, file_name) |
36 | 19 |
|
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/'): |
44 | 20 | # Read the file content
|
45 |
| - draft = file.split('/')[1] |
46 |
| - |
47 |
| - urls = json.loads(repo.get_contents("bin/specification_urls.json").decoded_content.decode('utf-8')) |
48 |
| - |
49 |
| - |
50 |
| - branch_name = pr.head.ref |
51 |
| - changed_file_content = repo.get_contents(file, ref=branch_name).decoded_content.decode('utf-8') |
52 |
| - |
| 21 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 22 | + changed_file_content = f.read() |
| 23 | + |
53 | 24 | # Parse JSON content
|
54 | 25 | try:
|
55 | 26 | json_content = json.loads(changed_file_content)
|
56 | 27 | for test in json_content:
|
57 | 28 | if "specification" in test:
|
58 | 29 | for specification_object in test["specification"]:
|
59 | 30 | for spec, section in specification_object.items():
|
60 |
| - if spec in ["core", "validation", "hyper-schema"]: print_github_action_notice(file, urls[draft][spec] + section) |
61 |
| - elif spec in ["quote"]: continue |
62 |
| - elif spec in ["ecma262", "perl5"]: print_github_action_notice(file, urls[spec] + section) |
63 |
| - elif re.match("^rfc\\d+$"): print_github_action_notice(file, urls["rfc"] + spec + ".txt#" + section) |
64 |
| - else: print_github_action_notice(file, urls["iso"]) |
65 |
| - except json.JSONDecodeError as e: |
66 |
| - print(f"Error parsing JSON in file '{file}': {e}") |
67 |
| - |
68 |
| -if __name__ == "__main__": |
69 |
| - main() |
| 31 | + draft = Path(file_path).parent.name |
| 32 | + if spec in ["core", "validation", "hyper-schema"]: |
| 33 | + print_github_action_notice(file_path, urls[draft][spec] + section) |
| 34 | + elif spec in ["quote"]: |
| 35 | + continue |
| 36 | + elif spec in ["ecma262", "perl5"]: |
| 37 | + print_github_action_notice(file_path, urls[spec] + section) |
| 38 | + elif re.match("^rfc\\d+$", spec): |
| 39 | + print_github_action_notice(file_path, urls["rfc"] + spec + ".txt#" + section) |
| 40 | + else: |
| 41 | + print_github_action_notice(file_path, urls["iso"]) |
| 42 | + except json.JSONDecodeError: |
| 43 | + print(f"Failed to parse JSON content for file: {file_path}") |
0 commit comments