From f9c8a4751343e7437aaf7becd44d0f51f5c58a5f Mon Sep 17 00:00:00 2001 From: Michelle Tran Date: Mon, 10 Mar 2025 10:06:40 -0400 Subject: [PATCH 1/3] 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. --- codecov_cli/commands/empty_upload.py | 41 +++++++++++++++++++++- tests/commands/test_invoke_empty_upload.py | 41 ++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/commands/test_invoke_empty_upload.py diff --git a/codecov_cli/commands/empty_upload.py b/codecov_cli/commands/empty_upload.py index bfb0f39a5..48cddf41c 100644 --- a/codecov_cli/commands/empty_upload.py +++ b/codecov_cli/commands/empty_upload.py @@ -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 @@ -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( @@ -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( diff --git a/tests/commands/test_invoke_empty_upload.py b/tests/commands/test_invoke_empty_upload.py new file mode 100644 index 000000000..59a87dc21 --- /dev/null +++ b/tests/commands/test_invoke_empty_upload.py @@ -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() + From b2674e07744932e0d01f8901dabe1e48f4924dca Mon Sep 17 00:00:00 2001 From: Michelle Tran Date: Mon, 10 Mar 2025 11:00:07 -0400 Subject: [PATCH 2/3] Force new version for test-results-parser This is because build is failing for 3.13 --- codecov_cli/commands/empty_upload.py | 29 +++++++++++----------- pyproject.toml | 2 +- tests/commands/test_invoke_empty_upload.py | 16 ------------ 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/codecov_cli/commands/empty_upload.py b/codecov_cli/commands/empty_upload.py index 48cddf41c..463fd81c5 100644 --- a/codecov_cli/commands/empty_upload.py +++ b/codecov_cli/commands/empty_upload.py @@ -25,7 +25,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, ) @@ -55,20 +55,19 @@ def 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("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", diff --git a/pyproject.toml b/pyproject.toml index 2f7ff34e2..eecab18ba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", ] diff --git a/tests/commands/test_invoke_empty_upload.py b/tests/commands/test_invoke_empty_upload.py index 59a87dc21..d38a1a794 100644 --- a/tests/commands/test_invoke_empty_upload.py +++ b/tests/commands/test_invoke_empty_upload.py @@ -23,19 +23,3 @@ def test_invoke_empty_upload_with_create_commit(mocker): 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() - From 2e3d4c631e6ad1ceb19f7b9a22c18b665fc77f07 Mon Sep 17 00:00:00 2001 From: Michelle Tran Date: Mon, 10 Mar 2025 20:49:48 -0400 Subject: [PATCH 3/3] Fix spelling of commands --- codecov_cli/commands/commit.py | 2 +- codecov_cli/commands/report.py | 2 +- codecov_cli/commands/upload.py | 2 +- codecovcli_commands | 17 ++++++++++++----- tests/commands/test_invoke_upload_coverage.py | 4 ++-- tests/commands/test_invoke_upload_process.py | 4 ++-- 6 files changed, 19 insertions(+), 12 deletions(-) diff --git a/codecov_cli/commands/commit.py b/codecov_cli/commands/commit.py index 5571e3e5a..97dbff1ce 100644 --- a/codecov_cli/commands/commit.py +++ b/codecov_cli/commands/commit.py @@ -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, ) diff --git a/codecov_cli/commands/report.py b/codecov_cli/commands/report.py index 3a0d05ef5..280e8492c 100644 --- a/codecov_cli/commands/report.py +++ b/codecov_cli/commands/report.py @@ -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, ) diff --git a/codecov_cli/commands/upload.py b/codecov_cli/commands/upload.py index 5bf5c7dd6..2b2830027 100644 --- a/codecov_cli/commands/upload.py +++ b/codecov_cli/commands/upload.py @@ -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, ), diff --git a/codecovcli_commands b/codecovcli_commands index 95724a66e..22391fc05 100644 --- a/codecovcli_commands +++ b/codecovcli_commands @@ -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 @@ -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] @@ -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 @@ -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] @@ -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 @@ -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 diff --git a/tests/commands/test_invoke_upload_coverage.py b/tests/commands/test_invoke_upload_coverage.py index da1876e9b..58e6507f3 100644 --- a/tests/commands/test_invoke_upload_coverage.py +++ b/tests/commands/test_invoke_upload_coverage.py @@ -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.", diff --git a/tests/commands/test_invoke_upload_process.py b/tests/commands/test_invoke_upload_process.py index 998bc4301..967e0dfeb 100644 --- a/tests/commands/test_invoke_upload_process.py +++ b/tests/commands/test_invoke_upload_process.py @@ -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.",