build(coverage): add comment to PR with diff-cover output

- add script to help format the coverage comment
- manage steps logic

refs #15
This commit is contained in:
Arthur Meyre
2021-07-16 10:05:19 +02:00
parent 7dccfa649c
commit ef78e66241
3 changed files with 76 additions and 3 deletions

View File

@@ -0,0 +1,19 @@
CURR_DIR=`dirname $0`
# Run diff-coverage
poetry run diff-cover coverage.xml --fail-under 100 \
--html-report coverage.html \
--compare-branch origin/"$1" | tee diff-coverage.txt
# Get exit code without closing the script
TEST_EXIT_CODE="$?"
# Format diff-coverage.txt for PR comment
poetry run python script/actions_utils/coverage_report_format.py \
--diff-cover-exit-code "$TEST_EXIT_CODE" \
--diff-cover-output diff-coverage.txt
# Set exit code if test failed
if [[ "$TEST_EXIT_CODE" != "0" ]]; then
exit "$TEST_EXIT_CODE"
fi

View File

@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
"""Helper script for github actions"""
import argparse
import traceback
from pathlib import Path
def main(args):
"""Entry point"""
diff_cover_file_path = Path(args.diff_cover_output).resolve().absolute()
diff_cover_content = None
with open(diff_cover_file_path, "r") as f:
diff_cover_content = f.readlines()
with open(diff_cover_file_path, "w", encoding="utf-8") as f:
if args.diff_cover_exit_code == 0:
f.write("## Coverage passed ✅\n\n")
else:
f.write("## Coverage failed ❌\n\n")
# Open collapsible section
f.write("<details><summary>Coverage details</summary>\n<p>\n\n")
f.write("```\n")
f.writelines(diff_cover_content)
# Close collapsible section
f.write("```\n\n")
f.write("</p>\n</details>\n\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument("--diff-cover-exit-code", type=int, required=True)
parser.add_argument("--diff-cover-output", type=str, required=True)
cli_args = parser.parse_args()
try:
main(cli_args)
except Exception as e:
traceback.print_exc()