Skip to content

Commit e4df56e

Browse files
authored
Merge branch 'main' into feature-branch
2 parents 302abe6 + 564e695 commit e4df56e

File tree

2 files changed

+29
-64
lines changed

2 files changed

+29
-64
lines changed

.github/workflows/annotation_workflow.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,5 @@ jobs:
1919
with:
2020
python-version: '3.x'
2121

22-
- name: Install dependencies
23-
run: pip install PyGithub
24-
2522
- name: Run Python script
2623
run: python bin/annotation_workflow.py
27-
env:
28-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29-
GITHUB_REF: ${{ github.ref }}
30-
GITHUB_REPOSITORY: ${{ github.repository }}
31-

bin/annotation_workflow.py

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,42 @@
1-
from github import Github
2-
import os
3-
import sys
41
import json
2+
from pathlib import Path
53
import re
64

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}")
78

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)
1012

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
18+
4419
# 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-
20+
with open(file_path, "r") as f:
21+
changed_file_content = f.read()
22+
5323
# Parse JSON content
5424
try:
5525
json_content = json.loads(changed_file_content)
5626
for test in json_content:
5727
if "specification" in test:
5828
for specification_object in test["specification"]:
5929
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()
30+
draft = root.split('/')[-1]
31+
if spec in ["core", "validation", "hyper-schema"]:
32+
print_github_action_notice(file_path, urls[draft][spec] + section)
33+
elif spec in ["quote"]:
34+
continue
35+
elif spec in ["ecma262", "perl5"]:
36+
print_github_action_notice(file_path, urls[spec] + section)
37+
elif re.match("^rfc\\d+$", spec):
38+
print_github_action_notice(file_path, urls["rfc"] + spec + ".txt#" + section)
39+
else:
40+
print_github_action_notice(file_path, urls["iso"])
41+
except json.JSONDecodeError:
42+
print(f"Failed to parse JSON content for file: {file_path}")

0 commit comments

Comments
 (0)