Skip to content

Commit

Permalink
Add create commit to empty-upload command (#654)
Browse files Browse the repository at this point in the history
* Add create commit to empty-upload command

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.

* Force new version for test-results-parser

This is because build is failing for 3.13

* Fix spelling of commands
  • Loading branch information
michelletran-codecov authored Mar 11, 2025
1 parent 079f01b commit 30cba3f
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 14 deletions.
2 changes: 1 addition & 1 deletion codecov_cli/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"--pr",
"--pull-request-number",
"pull_request_number",
help="Specify the pull request number mannually. Used to override pre-existing CI environment variables",
help="Specify the pull request number manually. Used to override pre-existing CI environment variables",
cls=CodecovOption,
fallback_field=FallbackFieldEnum.pull_request_number,
)
Expand Down
40 changes: 39 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 manually. 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,29 @@ 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)

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
2 changes: 1 addition & 1 deletion codecov_cli/commands/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"--pr",
"--pull-request-number",
"pull_request_number",
help="Specify the pull request number mannually. Used to override pre-existing CI environment variables",
help="Specify the pull request number manually. Used to override pre-existing CI environment variables",
cls=CodecovOption,
fallback_field=FallbackFieldEnum.pull_request_number,
)
Expand Down
2 changes: 1 addition & 1 deletion codecov_cli/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _turn_env_vars_into_dict(ctx, params, value):
"--pr",
"--pull-request-number",
"pull_request_number",
help="Specify the pull request number mannually. Used to override pre-existing CI environment variables",
help="Specify the pull request number manually. Used to override pre-existing CI environment variables",
cls=CodecovOption,
fallback_field=FallbackFieldEnum.pull_request_number,
),
Expand Down
17 changes: 12 additions & 5 deletions codecovcli_commands
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Options:
--parent-sha TEXT SHA (with 40 chars) of what should be the
parent of this commit
-P, --pr, --pull-request-number TEXT
Specify the pull request number mannually.
Specify the pull request number manually.
Used to override pre-existing CI environment
variables
-B, --branch TEXT Branch to which this commit belongs to
Expand All @@ -49,7 +49,7 @@ Options:
--code TEXT The code of the report. If unsure, leave
default
-P, --pr, --pull-request-number TEXT
Specify the pull request number mannually.
Specify the pull request number manually.
Used to override pre-existing CI environment
variables
-C, --sha, --commit-sha TEXT Commit SHA (with 40 chars) [required]
Expand Down Expand Up @@ -109,7 +109,7 @@ Options:
in Codecov UI
-B, --branch TEXT Branch to which this commit belongs to
-P, --pr, --pull-request-number TEXT
Specify the pull request number mannually.
Specify the pull request number manually.
Used to override pre-existing CI environment
variables
-e, --env, --env-var TEXT Specify environment variables to be included
Expand Down Expand Up @@ -151,6 +151,13 @@ Usage: codecovcli empty-upload [OPTIONS]

Options:
--force
--parent-sha TEXT SHA (with 40 chars) of what should be the
parent of this commit
-P, --pr, --pull-request-number TEXT
Specify the pull request number manually.
Used to override pre-existing CI environment
variables
-B, --branch TEXT Branch to which this commit belongs to
-C, --sha, --commit-sha TEXT Commit SHA (with 40 chars) [required]
-Z, --fail-on-error Exit with non-zero code in case of error
--git-service [github|gitlab|bitbucket|github_enterprise|gitlab_enterprise|bitbucket_server]
Expand Down Expand Up @@ -293,7 +300,7 @@ Options:
in Codecov UI
-B, --branch TEXT Branch to which this commit belongs to
-P, --pr, --pull-request-number TEXT
Specify the pull request number mannually.
Specify the pull request number manually.
Used to override pre-existing CI environment
variables
-e, --env, --env-var TEXT Specify environment variables to be included
Expand Down Expand Up @@ -369,7 +376,7 @@ Options:
in Codecov UI
-B, --branch TEXT Branch to which this commit belongs to
-P, --pr, --pull-request-number TEXT
Specify the pull request number mannually.
Specify the pull request number manually.
Used to override pre-existing CI environment
variables
-e, --env, --env-var TEXT Specify environment variables to be included
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
"regex",
"responses==0.21.*",
"sentry-sdk>=2.20.0",
"test-results-parser==0.5.*",
"test-results-parser==0.5.4",
"tree-sitter==0.20.*",
"wrapt>=1.17.2",
]
Expand Down
25 changes: 25 additions & 0 deletions tests/commands/test_invoke_empty_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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()

4 changes: 2 additions & 2 deletions tests/commands/test_invoke_upload_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def test_upload_coverage_options(mocker):
" Codecov UI",
" -B, --branch TEXT Branch to which this commit belongs to",
" -P, --pr, --pull-request-number TEXT",
" Specify the pull request number mannually.",
" Used to override pre-existing CI environment",
" Specify the pull request number manually. Used",
" to override pre-existing CI environment",
" variables",
" -e, --env, --env-var TEXT Specify environment variables to be included",
" with this build.",
Expand Down
4 changes: 2 additions & 2 deletions tests/commands/test_invoke_upload_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def test_upload_process_options(mocker):
" Codecov UI",
" -B, --branch TEXT Branch to which this commit belongs to",
" -P, --pr, --pull-request-number TEXT",
" Specify the pull request number mannually.",
" Used to override pre-existing CI environment",
" Specify the pull request number manually. Used",
" to override pre-existing CI environment",
" variables",
" -e, --env, --env-var TEXT Specify environment variables to be included",
" with this build.",
Expand Down

0 comments on commit 30cba3f

Please sign in to comment.