diff --git a/format_issue_body.py b/format_issue_body.py index c825853..ab73247 100644 --- a/format_issue_body.py +++ b/format_issue_body.py @@ -149,7 +149,8 @@ def format_summary(report): def format_report(summaries, py_version): - template = textwrap.dedent("""\ + template = textwrap.dedent( + """\
Python {py_version} Test Summary ``` @@ -157,7 +158,8 @@ def format_report(summaries, py_version): ```
- """) + """, + ) # can't use f-strings because that would format *before* the dedenting message = template.format(summaries="\n".join(summaries), py_version=py_version) return message @@ -238,20 +240,7 @@ def format_collection_error(error, **formatter_kwargs): """).format(py_version=py_version, name=error.name, traceback=error.repr_) -if __name__ == "__main__": - parser = argparse.ArgumentParser() - parser.add_argument("log-file", type=pathlib.Path) - parser.add_argument("workflow-url", type=str) - parser.add_argument( - "issue-body", type=pathlib.Path, default="issue-body.md", nargs="?" - ) - args = vars(parser.parse_args()) - - py_version = ".".join(str(_) for _ in sys.version_info[:2]) - - print("Parsing logs ...") - - lines = args["log-file"].read_text().splitlines() +def format_message(lines): parsed_lines = [json.loads(line) for line in lines] reports = [ parse_record(data) @@ -268,8 +257,45 @@ def format_collection_error(error, **formatter_kwargs): preformatted, max_chars=65535, py_version=py_version ) + return message + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("log-file", type=pathlib.Path) + parser.add_argument("workflow-url", type=str) + parser.add_argument( + "issue-body", type=pathlib.Path, default="issue-body.md", nargs="?" + ) + args = vars(parser.parse_args()) + + py_version = ".".join(str(_) for _ in sys.version_info[:2]) + + print("Parsing logs ...") + + path = args["log-file"] + + try: + lines = path.read_text().splitlines() + except FileNotFoundError: + message = textwrap.dedent( + f"""\ + Something went wrong. + + However, the log file at + + ``` + {path} + ``` + + does not exist so there are no additional details. + """, + ) + else: + message = format_message(lines) + workflow_link = f"[Workflow Run URL]({args['workflow-url']})" - body = "\n".join([workflow_link, message]) + body = "\n".join([workflow_link, "", message]) output_file = args["issue-body"] print(f"Writing output file to: {output_file.absolute()}")