Skip to content

Commit 564e695

Browse files
committed
Walking through all leaf files
1 parent 7b84fb4 commit 564e695

File tree

2 files changed

+27
-62
lines changed

2 files changed

+27
-62
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: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,23 @@
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
4418
# 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()
5121

5222
# Parse JSON content
5323
try:
@@ -56,13 +26,16 @@ def main():
5626
if "specification" in test:
5727
for specification_object in test["specification"]:
5828
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

Comments
 (0)