Skip to content

Commit

Permalink
Add create commit to empty-upload command
Browse files Browse the repository at this point in the history
This is so that we can also create the commit before uploading.
The create-commit is optional (i.e. if users don't pass in values, then their workflow shouldn't break).
It will only create a commit if the values are passed.
  • Loading branch information
michelletran-codecov committed Mar 10, 2025
1 parent 079f01b commit f9c8a47
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
41 changes: 40 additions & 1 deletion codecov_cli/commands/empty_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum
from codecov_cli.helpers.args import get_cli_args
from codecov_cli.helpers.git import GitService
from codecov_cli.helpers.options import global_options
from codecov_cli.services.commit import create_commit_logic
from codecov_cli.services.empty_upload import empty_upload_logic
from codecov_cli.types import CommandContext

Expand All @@ -16,6 +16,26 @@

@click.command()
@click.option("--force", is_flag=True, default=False)
@click.option(
"--parent-sha",
help="SHA (with 40 chars) of what should be the parent of this commit",
)
@click.option(
"-P",
"--pr",
"--pull-request-number",
"pull_request_number",
help="Specify the pull request number mannually. Used to override pre-existing CI environment variables",
cls=CodecovOption,
fallback_field=FallbackFieldEnum.pull_request_number,
)
@click.option(
"-B",
"--branch",
help="Branch to which this commit belongs to",
cls=CodecovOption,
fallback_field=FallbackFieldEnum.branch,
)
@global_options
@click.pass_context
def empty_upload(
Expand All @@ -26,11 +46,30 @@ def empty_upload(
token: typing.Optional[str],
git_service: typing.Optional[str],
fail_on_error: typing.Optional[bool],
parent_sha: typing.Optional[str],
pull_request_number: typing.Optional[int],
branch: typing.Optional[str],
):
with sentry_sdk.start_transaction(op="task", name="Empty Upload"):
with sentry_sdk.start_span(name="empty_upload"):
enterprise_url = ctx.obj.get("enterprise_url")
args = get_cli_args(ctx)

if parent_sha and pull_request_number and branch:
logger.debug("Attempting to Create Commit before doing an empty upload.")
create_commit_logic(
commit_sha,
parent_sha,
pull_request_number,
branch,
slug,
token,
git_service,
enterprise_url,
fail_on_error,
args,
)

logger.debug(
"Starting empty upload process",
extra=dict(
Expand Down
41 changes: 41 additions & 0 deletions tests/commands/test_invoke_empty_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from click.testing import CliRunner

from codecov_cli.fallbacks import FallbackFieldEnum
from codecov_cli.main import cli
from tests.factory import FakeProvider, FakeVersioningSystem

def test_invoke_empty_upload_with_create_commit(mocker):
create_commit_mock = mocker.patch("codecov_cli.commands.empty_upload.create_commit_logic")
empty_upload_mock = mocker.patch("codecov_cli.commands.empty_upload.empty_upload_logic")

fake_ci_provider = FakeProvider({FallbackFieldEnum.commit_sha: None})
mocker.patch("codecov_cli.main.get_ci_adapter", return_value=fake_ci_provider)

runner = CliRunner()
result = runner.invoke(cli, ["empty-upload",
"-C", "command-sha",
"--slug", "owner/repo",
"--parent-sha", "asdf",
"--branch", "main",
"--pr", 1234], obj={})
assert result.exit_code == 0

create_commit_mock.assert_called_once()
empty_upload_mock.assert_called_once()

def test_invoke_empty_upload_no_create_commit(mocker):
create_commit_mock = mocker.patch("codecov_cli.commands.empty_upload.create_commit_logic")
empty_upload_mock = mocker.patch("codecov_cli.commands.empty_upload.empty_upload_logic")

fake_ci_provider = FakeProvider({FallbackFieldEnum.commit_sha: None})
mocker.patch("codecov_cli.main.get_ci_adapter", return_value=fake_ci_provider)

runner = CliRunner()
result = runner.invoke(cli, ["empty-upload",
"-C", "command-sha",
"--slug", "owner/repo"], obj={})
assert result.exit_code == 0

create_commit_mock.assert_not_called()
empty_upload_mock.assert_called_once()

0 comments on commit f9c8a47

Please sign in to comment.