Skip to content

Commit

Permalink
template library, url loads changes
Browse files Browse the repository at this point in the history
  • Loading branch information
suprith-hub committed May 8, 2024
1 parent 3a50900 commit f276661
Showing 1 changed file with 36 additions and 37 deletions.
73 changes: 36 additions & 37 deletions bin/annotation_workflow.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import json, re
from pathlib import Path
import uritemplate

def github_action_notice(file, url, line):
def github_action_notice(path, url, line):
"""
Print GitHub action notice with file path, URL, and line number.
Return a GitHub action notice with file path, URL, and line number.
Parameters:
file (str): File path.
path (str): File path.
url (str): URL.
line (int): Line number.
"""
return f"::warning file={file},line={line}::Annotation: {url}"
return f"::warning file={path},line={line}::Annotation: {url}"

def find_test_line_number(test_content, test_name):
"""
Expand All @@ -23,54 +24,52 @@ def find_test_line_number(test_content, test_name):
Returns:
int: Line number of the test.
"""
lines = test_content.split("\n") # Split content into lines
for i, line in enumerate(lines, start=1): # Iterate over lines
if test_name in line: # Check if test name is found in the line
return i # Return the line number if found
return 1 # Return None if test name is not found
lines = test_content.split("\n")
for i, line in enumerate(lines, start=1):
if test_name in line:
return i
return 1

def clear_previous_annotations():
"""
Clear previous GitHub action annotations.
"""
print("::remove-matcher owner=me::")

# Specify the path to the JSON file using pathlib.Path
json_file_path = Path("bin/specification_urls.json")

# Read specification URLs from JSON file using pathlib.Path
with json_file_path.open("r", encoding="utf-8") as f:
urls = json.load(f)
BIN_DIR = Path(__file__).parent
urls = json.loads(BIN_DIR.joinpath("specification_urls.json").read_text())

clear_previous_annotations()

# Iterate through JSON files in tests folder and subdirectories
for file_path in Path("tests").rglob("*.json"):
# Read the file content using pathlib.Path

with file_path.open("r", encoding="utf-8") as f:
changed_file_content = f.read()

# Parse JSON content

try:
json_content = json.loads(changed_file_content)
for test in json_content:
if "specification" in test:
line_number = find_test_line_number(changed_file_content, test.get("description") )
except json.JSONDecodeError:
print(f"::error file={file_path}::Failed to parse JSON content")

for specification_object in test["specification"]:
for spec, section in specification_object.items():
draft = file_path.parent.name
if spec in ["quote"]:
continue
elif spec in ["core", "validation", "hyper-schema"]:
url = urls[draft][spec].format(spec=spec, section=section)
elif re.match("^rfc\\d+$", spec):
url = urls["rfc"].format(spec=spec, section=section)
elif re.match("^iso\\d+$", spec):
url = urls["iso"].format(spec=spec, section=section)
else:
url = urls[spec].format(spec=spec, section=section)
for test in json_content:
if "specification" in test:
line_number = find_test_line_number(changed_file_content, test.get("description") )

clear_previous_annotations()
print(github_action_notice(file_path, url, line_number))
for specification_object in test["specification"]:
for spec, section in specification_object.items():
draft = file_path.parent.name
if spec in ["quote"]:
continue
elif spec in ["core", "validation", "hyper-schema"]:
template = uritemplate.URITemplate(urls[draft][spec])
elif re.match("^rfc\\d+$", spec):
template = uritemplate.URITemplate(urls["rfc"])
elif re.match("^iso\\d+$", spec):
template = uritemplate.URITemplate(urls["iso"])
else:
template = uritemplate.URITemplate(urls[spec])
url = template.expand(spec=spec, section=section)

except json.JSONDecodeError:
print(f"::error file={file_path}::Failed to parse JSON content")
print(github_action_notice(file_path, url, line_number))

0 comments on commit f276661

Please sign in to comment.