From 824c68ca762dfa3065d90c4b7480db3e33d20435 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 5 Feb 2024 17:53:58 -0800 Subject: [PATCH 1/7] fix: update merge commit command --- .../helpers/ci_adapters/github_actions.py | 32 ++++++++++++++----- tests/ci_adapters/test_ghactions.py | 2 +- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/codecov_cli/helpers/ci_adapters/github_actions.py b/codecov_cli/helpers/ci_adapters/github_actions.py index f12d2105..afbddaf6 100644 --- a/codecov_cli/helpers/ci_adapters/github_actions.py +++ b/codecov_cli/helpers/ci_adapters/github_actions.py @@ -1,9 +1,12 @@ +import logging import os import re import subprocess from codecov_cli.helpers.ci_adapters.base import CIAdapterBase +logger = logging.getLogger("codecovcli") + class GithubActionsCIAdapter(CIAdapterBase): # https://docs.github.com/en/actions/learn-github-actions/environment-variables @@ -17,14 +20,27 @@ def _get_commit_sha(self): if not pr: return commit - # actions/checkout should be run with fetch-depth > 1 or set to 0 for this to work - completed_subprocess = subprocess.run( - ["git", "rev-parse", "HEAD^@"], capture_output=True - ) - - parents_hash = completed_subprocess.stdout.decode().strip().splitlines() - if len(parents_hash) == 2: - return parents_hash[1] + merge_commit_regex = r"^[a-z0-9]{40} [a-z0-9]{40}$" + merge_commit_message = subprocess.run( + ["git", "show", "--no-patch", "--format=%P"], + capture_output=True, + ).stdout + + try: + merge_commit_message = merge_commit_message.decode('utf-8') + + if re.match(merge_commit_regex, merge_commit_message) is not None: + merge_commit = merge_commit_message.split(" ")[1] + logger.info(f" Fixing merge commit SHA ${commit} -> ${merge_commit}") + commit = merge_commit + elif merge_commit_message == "": + logger.info( + "-> Issue detecting commit SHA. Please run actions/checkout with fetch-depth > 1 or set to 0" + ) + except TypeError: # For the re.match + logger.info( + f" Commit with SHA ${commit} of PR ${pr} is not a merge commit" + ) return commit diff --git a/tests/ci_adapters/test_ghactions.py b/tests/ci_adapters/test_ghactions.py index 5d7efb44..4a5f11fa 100644 --- a/tests/ci_adapters/test_ghactions.py +++ b/tests/ci_adapters/test_ghactions.py @@ -56,7 +56,7 @@ def test_commit_sha_in_merge_commit_and_parents_hash_len_is_2(self, mocker): return_value=fake_subprocess, ) - fake_subprocess.stdout = b"aa74b3ff0411086ee37e7a78f1b62984d7759077\n20e1219371dff308fd910b206f47fdf250621abf" + fake_subprocess.stdout = b"aa74b3ff0411086ee37e7a78f1b62984d7759077 20e1219371dff308fd910b206f47fdf250621abf" assert ( GithubActionsCIAdapter().get_fallback_value(FallbackFieldEnum.commit_sha) == "20e1219371dff308fd910b206f47fdf250621abf" From d60042d0ee82d8f1a490956f18db678348588eae Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 5 Feb 2024 18:39:09 -0800 Subject: [PATCH 2/7] fix: verbose --- .github/workflows/ci.yml | 6 +++--- codecov_cli/helpers/ci_adapters/github_actions.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 17ea81c3..fe4e0836 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,10 +44,10 @@ jobs: pip install codecov-cli - name: Create commit in codecov run: | - codecovcli create-commit -t ${{ secrets.CODECOV_TOKEN }} --git-service github + codecovcli -v create-commit -t ${{ secrets.CODECOV_TOKEN }} --git-service github - name: Create commit report in codecov run: | - codecovcli create-report -t ${{ secrets.CODECOV_TOKEN }} --git-service github + codecovcli -v create-report -t ${{ secrets.CODECOV_TOKEN }} --git-service github build-test-upload: runs-on: ubuntu-latest @@ -80,7 +80,7 @@ jobs: - name: Dogfooding codecov-cli if: ${{ !github.event.pull_request.head.repo.fork && github.repository_owner == 'codecov' }} run: | - codecovcli do-upload --fail-on-error -t ${{ secrets.CODECOV_TOKEN }} --plugin pycoverage --flag python${{matrix.python-version}} + codecovcli -v do-upload --fail-on-error -t ${{ secrets.CODECOV_TOKEN }} --plugin pycoverage --flag python${{matrix.python-version}} static-analysis: runs-on: ubuntu-latest diff --git a/codecov_cli/helpers/ci_adapters/github_actions.py b/codecov_cli/helpers/ci_adapters/github_actions.py index afbddaf6..2ee27368 100644 --- a/codecov_cli/helpers/ci_adapters/github_actions.py +++ b/codecov_cli/helpers/ci_adapters/github_actions.py @@ -39,7 +39,7 @@ def _get_commit_sha(self): ) except TypeError: # For the re.match logger.info( - f" Commit with SHA ${commit} of PR ${pr} is not a merge commit" + f" Commit with SHA {commit} of PR {pr} is not a merge commit" ) return commit From 6b602682d52c5c21ccc70e273fba9d834aeae4cf Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 5 Feb 2024 18:45:24 -0800 Subject: [PATCH 3/7] fix: update to strip out the \n --- codecov_cli/helpers/ci_adapters/github_actions.py | 2 +- tests/ci_adapters/test_ghactions.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/codecov_cli/helpers/ci_adapters/github_actions.py b/codecov_cli/helpers/ci_adapters/github_actions.py index 2ee27368..f009cd78 100644 --- a/codecov_cli/helpers/ci_adapters/github_actions.py +++ b/codecov_cli/helpers/ci_adapters/github_actions.py @@ -27,7 +27,7 @@ def _get_commit_sha(self): ).stdout try: - merge_commit_message = merge_commit_message.decode('utf-8') + merge_commit_message = merge_commit_message.decode("utf-8").strip() if re.match(merge_commit_regex, merge_commit_message) is not None: merge_commit = merge_commit_message.split(" ")[1] diff --git a/tests/ci_adapters/test_ghactions.py b/tests/ci_adapters/test_ghactions.py index 4a5f11fa..ff54d5de 100644 --- a/tests/ci_adapters/test_ghactions.py +++ b/tests/ci_adapters/test_ghactions.py @@ -56,7 +56,7 @@ def test_commit_sha_in_merge_commit_and_parents_hash_len_is_2(self, mocker): return_value=fake_subprocess, ) - fake_subprocess.stdout = b"aa74b3ff0411086ee37e7a78f1b62984d7759077 20e1219371dff308fd910b206f47fdf250621abf" + fake_subprocess.stdout = b"aa74b3ff0411086ee37e7a78f1b62984d7759077 20e1219371dff308fd910b206f47fdf250621abf\n" assert ( GithubActionsCIAdapter().get_fallback_value(FallbackFieldEnum.commit_sha) == "20e1219371dff308fd910b206f47fdf250621abf" From 5348e74f647637211f6d22b9b8a1f0f689f4ce6a Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 5 Feb 2024 18:48:02 -0800 Subject: [PATCH 4/7] fix: add tests --- tests/ci_adapters/test_ghactions.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/ci_adapters/test_ghactions.py b/tests/ci_adapters/test_ghactions.py index ff54d5de..4bb1fd08 100644 --- a/tests/ci_adapters/test_ghactions.py +++ b/tests/ci_adapters/test_ghactions.py @@ -82,6 +82,32 @@ def test_commit_sha_in_merge_commit_and_parents_hash_len_is_not_2(self, mocker): == "1234" ) + def test_commit_sha_in_merge_commit_is_empty(self, mocker): + mocker.patch.dict( + os.environ, {GithubActionsEnvEnum.GITHUB_SHA: "1234"}, clear=True + ) + mocker.patch.object( + GithubActionsCIAdapter, "_get_pull_request_number" + ).return_value = "random_pr_number" + + fake_subprocess = mocker.MagicMock() + mocker.patch( + "codecov_cli.helpers.ci_adapters.github_actions.subprocess.run", + return_value=fake_subprocess, + ) + + fake_subprocess.stdout = b"" + assert ( + GithubActionsCIAdapter().get_fallback_value(FallbackFieldEnum.commit_sha) + == "1234" + ) + + fake_subprocess.stdout = None + assert ( + GithubActionsCIAdapter().get_fallback_value(FallbackFieldEnum.commit_sha) + == "1234" + ) + @pytest.mark.parametrize( "env_dict,slug,build_code,expected", [ From c3e8e374658046ca882a31b7ace4309717dab3bd Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 5 Feb 2024 18:51:37 -0800 Subject: [PATCH 5/7] fix: add the right errors --- codecov_cli/helpers/ci_adapters/github_actions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codecov_cli/helpers/ci_adapters/github_actions.py b/codecov_cli/helpers/ci_adapters/github_actions.py index f009cd78..bf3e8e72 100644 --- a/codecov_cli/helpers/ci_adapters/github_actions.py +++ b/codecov_cli/helpers/ci_adapters/github_actions.py @@ -37,7 +37,7 @@ def _get_commit_sha(self): logger.info( "-> Issue detecting commit SHA. Please run actions/checkout with fetch-depth > 1 or set to 0" ) - except TypeError: # For the re.match + except AttributeError, TypeError: # For the re.match and .decode logger.info( f" Commit with SHA {commit} of PR {pr} is not a merge commit" ) From b610dc9a8f4f8155e728501fd246978c0502f6fb Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 5 Feb 2024 18:52:14 -0800 Subject: [PATCH 6/7] fix: I should probably run pytest first --- codecov_cli/helpers/ci_adapters/github_actions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codecov_cli/helpers/ci_adapters/github_actions.py b/codecov_cli/helpers/ci_adapters/github_actions.py index bf3e8e72..86e3ac98 100644 --- a/codecov_cli/helpers/ci_adapters/github_actions.py +++ b/codecov_cli/helpers/ci_adapters/github_actions.py @@ -37,7 +37,7 @@ def _get_commit_sha(self): logger.info( "-> Issue detecting commit SHA. Please run actions/checkout with fetch-depth > 1 or set to 0" ) - except AttributeError, TypeError: # For the re.match and .decode + except (AttributeError, TypeError): # For the re.match and .decode logger.info( f" Commit with SHA {commit} of PR {pr} is not a merge commit" ) From 4289414fa14484675118a99cc8b7fe4cf5a2e4d5 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Mon, 5 Feb 2024 18:52:40 -0800 Subject: [PATCH 7/7] fix: remove -v --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fe4e0836..17ea81c3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,10 +44,10 @@ jobs: pip install codecov-cli - name: Create commit in codecov run: | - codecovcli -v create-commit -t ${{ secrets.CODECOV_TOKEN }} --git-service github + codecovcli create-commit -t ${{ secrets.CODECOV_TOKEN }} --git-service github - name: Create commit report in codecov run: | - codecovcli -v create-report -t ${{ secrets.CODECOV_TOKEN }} --git-service github + codecovcli create-report -t ${{ secrets.CODECOV_TOKEN }} --git-service github build-test-upload: runs-on: ubuntu-latest @@ -80,7 +80,7 @@ jobs: - name: Dogfooding codecov-cli if: ${{ !github.event.pull_request.head.repo.fork && github.repository_owner == 'codecov' }} run: | - codecovcli -v do-upload --fail-on-error -t ${{ secrets.CODECOV_TOKEN }} --plugin pycoverage --flag python${{matrix.python-version}} + codecovcli do-upload --fail-on-error -t ${{ secrets.CODECOV_TOKEN }} --plugin pycoverage --flag python${{matrix.python-version}} static-analysis: runs-on: ubuntu-latest