Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update merge commit command #375

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions codecov_cli/helpers/ci_adapters/github_actions.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't actually have to change this adapter's logic. The command being used accounts for this fact and already "fixes" the merge commit if that's the case.

$ git rev-parse HEAD^@
1b00e154fb8a8a7ad09da4f9c6c95e70c5fa1565
211b95514359270c24c3e4e2058b3eef83db75e4 <-- this would be returned by the old logic
$ git show --no-patch --format=%P | cat
1b00e154fb8a8a7ad09da4f9c6c95e70c5fa1565 211b95514359270c24c3e4e2058b3eef83db75e4

capture_output=True,
).stdout

try:
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]
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 (AttributeError, TypeError): # For the re.match and .decode
logger.info(
f" Commit with SHA {commit} of PR {pr} is not a merge commit"
)

return commit

Expand Down
28 changes: 27 additions & 1 deletion tests/ci_adapters/test_ghactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\n"
assert (
GithubActionsCIAdapter().get_fallback_value(FallbackFieldEnum.commit_sha)
== "20e1219371dff308fd910b206f47fdf250621abf"
Expand All @@ -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",
[
Expand Down
Loading