Skip to content

Commit 2b1ffb7

Browse files
committed
Best practices followed with optimized code
1 parent e88a2da commit 2b1ffb7

File tree

3 files changed

+82
-61
lines changed

3 files changed

+82
-61
lines changed

.github/workflows/annotation_workflow.yml renamed to .github/workflows/show_specification_annotations.yml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
name: Specifications Annotation Workflow
1+
name: Show Specification Annotations
22

33
on:
44
pull_request:
55
paths:
66
- 'tests/**'
77

88
jobs:
9-
run_tests:
10-
name: logging annotations
9+
annotate:
1110
runs-on: ubuntu-latest
1211

1312
steps:
14-
- name: Checkout Repository
15-
uses: actions/checkout@v3
13+
uses: actions/checkout@v4
1614

17-
- name: Set up Python
18-
uses: actions/setup-python@v4
15+
- name: Generate Annotations
16+
uses: actions/setup-python@v5
1917
with:
2018
python-version: '3.x'
2119

bin/annotation_workflow.py

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,65 @@
11
import json
2-
import os
3-
import re
42
from pathlib import Path
53

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}")
4+
def github_action_notice(file, url, line):
5+
"""
6+
Print GitHub action notice with file path, URL, and line number.
97
10-
# Read specification URLs from JSON file
11-
with open("bin/specification_urls.json", "r") as f:
8+
Parameters:
9+
file (str): File path.
10+
url (str): URL.
11+
line (int): Line number.
12+
"""
13+
return f"::warning file={file},line={line}::Annotation: {url}"
14+
15+
def find_test_line_number(test_content, test_name):
16+
"""
17+
Find the line number of a test in the JSON content.
18+
19+
Parameters:
20+
test_content (str): JSON content.
21+
test_name (str): Test name.
22+
23+
Returns:
24+
int: Line number of the test.
25+
"""
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
31+
32+
# Specify the path to the JSON file using pathlib.Path
33+
json_file_path = Path("bin/specification_urls.json")
34+
35+
# Read specification URLs from JSON file using pathlib.Path
36+
with json_file_path.open("r", encoding="utf-8") as f:
1237
urls = json.load(f)
1338

1439
# 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)
19-
# Read the file content
20-
with open(file_path, 'r', encoding='utf-8') as f:
21-
changed_file_content = f.read()
22-
23-
# Parse JSON content
24-
try:
25-
json_content = json.loads(changed_file_content)
26-
for test in json_content:
27-
if "specification" in test:
28-
for specification_object in test["specification"]:
29-
for spec, section in specification_object.items():
30-
draft = Path(file_path).parent.name
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}")
40+
for file_path in Path("tests").rglob("*.json"):
41+
# Read the file content using pathlib.Path
42+
with file_path.open('r', encoding='utf-8') as f:
43+
changed_file_content = f.read()
44+
45+
# Parse JSON content
46+
try:
47+
json_content = json.loads(changed_file_content)
48+
for test in json_content:
49+
if "specification" in test:
50+
line_number = find_test_line_number(changed_file_content, test.get("description") )
51+
52+
for specification_object in test["specification"]:
53+
for spec, section in specification_object.items():
54+
draft = file_path.parent.name
55+
if spec in ["quote"]:
56+
continue
57+
elif spec in ["core", "validation", "hyper-schema"]:
58+
url = urls[draft][spec].format(spec=spec, section=section)
59+
else:
60+
url = urls[spec].format(spec=spec, section=section)
61+
annotation = github_action_notice(file_path, url, line_number)
62+
print(annotation)
63+
64+
except json.JSONDecodeError:
65+
print(f"::error file={file_path}::Failed to parse JSON content")

bin/specification_urls.json

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
"core": "https://json-schema.org/draft-03/draft-zyp-json-schema-03.pdf"
44
},
55
"draft4": {
6-
"core": "https://json-schema.org/draft-04/draft-zyp-json-schema-04#rfc.section.",
7-
"validation": "https://json-schema.org/draft-04/draft-fge-json-schema-validation-00#rfc.section.",
8-
"hyper-schema": "https://json-schema.org/draft-04/draft-luff-json-hyper-schema-00#rfc.section."
6+
"core": "https://json-schema.org/draft-04/draft-zyp-json-schema-04#rfc.section.{section}",
7+
"validation": "https://json-schema.org/draft-04/draft-fge-json-schema-validation-00#rfc.section.{section}",
8+
"hyper-schema": "https://json-schema.org/draft-04/draft-luff-json-hyper-schema-00#rfc.section.{section}"
99
},
1010
"draft6": {
11-
"core": "https://json-schema.org/draft-06/draft-wright-json-schema-01#rfc.section.",
12-
"validation": "https://json-schema.org/draft-06/draft-wright-json-schema-validation-01#rfc.section.",
13-
"hyper-schema": "https://json-schema.org/draft-06/draft-wright-json-schema-hyperschema-01#rfc.section."
11+
"core": "https://json-schema.org/draft-06/draft-wright-json-schema-01#rfc.section.{section}",
12+
"validation": "https://json-schema.org/draft-06/draft-wright-json-schema-validation-01#rfc.section.{section}",
13+
"hyper-schema": "https://json-schema.org/draft-06/draft-wright-json-schema-hyperschema-01#rfc.section.{section}"
1414
},
1515
"draft7": {
16-
"core": "https://json-schema.org/draft-07/draft-handrews-json-schema-01#rfc.section.",
17-
"validation": "https://json-schema.org/draft-07/draft-handrews-json-schema-validation-01#rfc.section.",
18-
"hyper-schema": "https://json-schema.org/draft-07/draft-handrews-json-schema-hyperschema-01#rfc.section."
16+
"core": "https://json-schema.org/draft-07/draft-handrews-json-schema-01#rfc.section.{section}",
17+
"validation": "https://json-schema.org/draft-07/draft-handrews-json-schema-validation-01#rfc.section.{section}",
18+
"hyper-schema": "https://json-schema.org/draft-07/draft-handrews-json-schema-hyperschema-01#rfc.section.{section}"
1919
},
2020
"draft2019-09": {
21-
"core": "https://json-schema.org/draft/2019-09/draft-handrews-json-schema-02#rfc.section.",
22-
"validation": "https://json-schema.org/draft/2019-09/draft-handrews-json-schema-validation-02#rfc.section.",
23-
"hyper-schema": "https://json-schema.org/draft/2019-09/draft-handrews-json-schema-hyperschema-02#rfc.section."
21+
"core": "https://json-schema.org/draft/2019-09/draft-handrews-json-schema-02#rfc.section.{section}",
22+
"validation": "https://json-schema.org/draft/2019-09/draft-handrews-json-schema-validation-02#rfc.section.{section}",
23+
"hyper-schema": "https://json-schema.org/draft/2019-09/draft-handrews-json-schema-hyperschema-02#rfc.section.{section}"
2424
},
2525
"draft2020-12": {
26-
"core": "https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01#section-",
27-
"validation": "https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-",
28-
"hyper-schema": "https://json-schema.org/draft/2019-09/draft-handrews-json-schema-hyperschema-02#rfc.section."
26+
"core": "https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01#section-{section}",
27+
"validation": "https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01#section-{section}",
28+
"hyper-schema": "https://json-schema.org/draft/2019-09/draft-handrews-json-schema-hyperschema-02#rfc.section.{section}"
2929
},
30-
"ecma262": "https://262.ecma-international.org/",
31-
"perl5": "https://perldoc.perl.org/perlre#",
32-
"rfc": "https://www.rfc-editor.org/rfc/",
30+
"ecma262": "https://262.ecma-international.org/{section}",
31+
"perl5": "https://perldoc.perl.org/perlre#{section}",
32+
"rfc": "https://www.rfc-editor.org/rfc/{spec}.txt#{section}",
3333
"iso": "https://www.iso.org/obp/ui"
34-
}
34+
}

0 commit comments

Comments
 (0)