From f86c19e6499deba313ec5dd54f10d681ff901778 Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Wed, 11 Oct 2023 18:31:36 +0200 Subject: [PATCH 1/3] Don't crash on pushing when pushing on something else than a branch --- coverage_comment/main.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/coverage_comment/main.py b/coverage_comment/main.py index 39d4c848..60dfae1a 100644 --- a/coverage_comment/main.py +++ b/coverage_comment/main.py @@ -112,6 +112,14 @@ def process_pr( ) -> int: log.info("Generating comment for PR") + if not config.GITHUB_PR_NUMBER and not config.GITHUB_BRANCH_NAME: + log.info( + "This worflow is not triggered on a pull_request event, " + "nor on a push event on a branch. Consequently, there's nothing to do. " + "Exiting." + ) + return 0 + _, coverage = coverage_module.get_coverage_info( merge=config.MERGE_COVERAGE_FILES, coverage_path=config.COVERAGE_PATH, @@ -177,7 +185,6 @@ def process_pr( if pr_number is None: # If we don't have a PR number, we're launched from a push event, # so we need to find the PR number from the branch name - assert config.GITHUB_BRANCH_NAME try: pr_number = github.find_pr_for_branch( github=gh, From 926f6ceb2b2b285bcae69a1625da44f4961c0992 Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Fri, 13 Oct 2023 13:36:47 +0200 Subject: [PATCH 2/3] Add test --- tests/integration/test_main.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index 20811c83..a653d9da 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -400,6 +400,25 @@ def checker(payload): assert output_file.read_text() == expected_output +def test_action__push__no_branch( + push_config, session, in_integration_env, git, get_logs +): + session.register("GET", "/repos/py-cov-action/foobar")( + json={"default_branch": "main", "visibility": "public"} + ) + + result = main.action( + config=push_config( + GITHUB_REF="refs/tags/v1.0.0", + ), + github_session=session, + http_session=session, + git=git, + ) + assert result == 0 + assert get_logs("INFO", "This worflow is not triggered on a pull_request event") + + def test_action__push__non_default_branch__no_pr( push_config, session, in_integration_env, output_file, summary_file, git ): From b45ecb97030fcb44aab693b65a2b188fdd064f6e Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Fri, 13 Oct 2023 13:37:01 +0200 Subject: [PATCH 3/3] Add an unrelated test that was missing for coverage --- tests/unit/test_coverage.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/unit/test_coverage.py b/tests/unit/test_coverage.py index 05ab09ed..e0760f00 100644 --- a/tests/unit/test_coverage.py +++ b/tests/unit/test_coverage.py @@ -287,3 +287,16 @@ def test_parse_diff_output(git): pathlib.Path("README.md"): [1, 3, 4, 5, 6], pathlib.Path("foo.txt"): [1], } + + +def test_parse_diff_output__error(git): + diff = """ +@@ -0,0 +1,1 @@ ++name: Python Coverage Comment +diff --git a/README.md b/README.md +index 1f1d9a4..e69de29 100644 +""" + git.register("git fetch origin main --depth=1000")() + git.register("git diff --unified=0 FETCH_HEAD -- .")(stdout=diff) + with pytest.raises(ValueError): + coverage.parse_diff_output(diff=diff)