Skip to content

Commit

Permalink
Add JobSummary report for GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis-Averin committed May 12, 2024
1 parent 126dab1 commit 0deb593
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
35 changes: 17 additions & 18 deletions scripts/check-urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import collections
import fileinput
import os
import re
Expand All @@ -9,6 +8,8 @@
from queue import SimpleQueue
from typing import Optional

from github_job_summary import JobSummary

"""
Read file names from stdin
Extract all URL-like strings
Expand Down Expand Up @@ -77,9 +78,8 @@ class Curl:
# print(URL_RE_PATTERN)
URL_REGEX = re.compile(URL_RE_PATTERN, re.MULTILINE)

BROKEN_URLS = collections.defaultdict(list)

EXTRACTED_URLS_WITH_FILES = {k: [] for k in URLS_TO_IGNORE}
# URL : [Files]
EXTRACTED_URLS_WITH_FILES: dict[str, [str]] = {k: [] for k in URLS_TO_IGNORE}


def url_extractor(text, filename):
Expand Down Expand Up @@ -183,7 +183,7 @@ def process_finished_task(task) -> None:
% (expected_ret_code, task.ret_code, task.url, task.stderr),
file=sys.stderr,
)
BROKEN_URLS[task.url] = EXTRACTED_URLS_WITH_FILES[task.url]
JOB_SUMMARY.add_error(f"Broken URL '{task.url}': {task.stderr}Files: {EXTRACTED_URLS_WITH_FILES[task.url]}")


WORKER_QUEUE = SimpleQueue()
Expand Down Expand Up @@ -219,7 +219,11 @@ def url_checker(num_workers=8):
print("Worker finished")


def main(files):
JOB_SUMMARY = JobSummary(os.environ.get('GITHUB_STEP_SUMMARY', "step_summary.md"))
JOB_SUMMARY.add_header("Test all URLs")


def main(files: [str]) -> int:
checker = threading.Thread(target=url_checker)
checker.start()

Expand All @@ -230,18 +234,13 @@ def main(files):
WORKER_QUEUE.put_nowait(None)
checker.join()

if BROKEN_URLS:
print("Errors:", file=sys.stdout, flush=True)
for url, files in BROKEN_URLS.items():
print(
"BROKEN URL: '%s' in files: %s"
% (url, ", ".join("'%s'" % f for f in files)),
file=sys.stderr,
flush=True,
)
if BROKEN_URLS:
exit(1)
if JOB_SUMMARY.has_errors:
print(JOB_SUMMARY, file=sys.stderr, flush=True)
return 1
else:
print(JOB_SUMMARY, file=sys.stdout, flush=True)
return 0


if __name__ == "__main__":
main([filename.strip() for filename in fileinput.input()])
exit(main([filename.strip() for filename in fileinput.input()]))
54 changes: 54 additions & 0 deletions scripts/github_job_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import contextlib
from typing import TextIO
from threading import Lock


class JobSummary:

def __init__(self, filename: str):
"""
:param filename: Use $GITHUB_STEP_SUMMARY inside GitHub
"""
self.__file: TextIO = open(filename, mode='wt')
self._errors = []
self._lock = Lock()

def close(self):
assert not self.__file.closed
self.__file.close()

def __str__(self) -> str:
if not self.has_errors:
return "OK"
lines = ["Errors:"] + self._errors
return '\n\n'.join(lines)

def _write_line(self, line):
with self._lock:
self.__file.write(line)

@property
def has_errors(self) -> bool:
return bool(self._errors)

def add_header(self, text: str, level: int = 3):
self._write_line(f"{'#' * level} {text}\n\n")

def add_error(self, text: str):
if not self._errors:
self._write_line("Errors:\n")
self._errors.append(text)
self._write_line(f"\n1. :x: {text}\n")


def test():
summary: JobSummary
with contextlib.closing(JobSummary("test.md")) as summary:
summary.add_header("Test results")
summary.add_error("Text for 1 error message")
summary.add_error("Text for 2 error message")


if __name__ == '__main__':
test()

0 comments on commit 0deb593

Please sign in to comment.