From a721ec67b76d1e6dfcb8de73d58c77fa50cc16e7 Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Fri, 20 Sep 2024 15:44:38 -0500 Subject: [PATCH] fix: first pass with tests --- codecov_cli/commands/base_picking.py | 12 ++--- codecov_cli/commands/commit.py | 14 ++--- codecov_cli/commands/create_report_result.py | 13 ++--- codecov_cli/commands/empty_upload.py | 13 ++--- codecov_cli/commands/get_report_results.py | 10 +--- codecov_cli/commands/labelanalysis.py | 12 ++--- codecov_cli/commands/process_test_results.py | 24 +++++++-- codecov_cli/commands/report.py | 12 ++--- codecov_cli/commands/send_notifications.py | 11 ++-- codecov_cli/commands/staticanalysis.py | 14 ++--- codecov_cli/commands/upload.py | 31 ++--------- codecov_cli/commands/upload_process.py | 29 ++--------- codecov_cli/helpers/args.py | 31 +++++++++++ codecov_cli/main.py | 2 + codecov_cli/services/commit/__init__.py | 21 ++++++-- codecov_cli/services/commit/base_picking.py | 3 +- codecov_cli/services/empty_upload/__init__.py | 16 +++++- codecov_cli/services/report/__init__.py | 30 +++++++++-- .../services/staticanalysis/__init__.py | 1 + codecov_cli/services/upload/__init__.py | 2 + .../services/upload/legacy_upload_sender.py | 7 ++- codecov_cli/services/upload/upload_sender.py | 8 +-- .../services/upload_completion/__init__.py | 13 ++++- tests/commands/test_process_test_results.py | 5 +- tests/helpers/test_args.py | 51 +++++++++++++++++++ tests/helpers/test_upload_sender.py | 6 ++- tests/services/commit/test_commit_service.py | 9 +++- .../empty_upload/test_empty_upload.py | 10 ++-- tests/services/report/test_report_results.py | 6 ++- tests/services/report/test_report_service.py | 7 +-- .../test_static_analysis_service.py | 6 +++ tests/services/upload/test_upload_service.py | 5 ++ 32 files changed, 268 insertions(+), 166 deletions(-) create mode 100644 codecov_cli/helpers/args.py create mode 100644 tests/helpers/test_args.py diff --git a/codecov_cli/commands/base_picking.py b/codecov_cli/commands/base_picking.py index 372bc888..3c536751 100644 --- a/codecov_cli/commands/base_picking.py +++ b/codecov_cli/commands/base_picking.py @@ -4,6 +4,7 @@ import click from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum +from codecov_cli.helpers.args import get_cli_args from codecov_cli.helpers.encoder import slug_without_subgroups_is_invalid from codecov_cli.services.commit.base_picking import base_picking_logic from codecov_cli.types import CommandContext @@ -54,16 +55,11 @@ def pr_base_picking( service: typing.Optional[str], ): enterprise_url = ctx.obj.get("enterprise_url") + args = get_cli_args(ctx) logger.debug( "Starting base picking process", extra=dict( - extra_log_attributes=dict( - pr=pr, - slug=slug, - token=token, - service=service, - enterprise_url=enterprise_url, - ) + extra_log_attributes=args, ), ) @@ -73,4 +69,4 @@ def pr_base_picking( ) return - base_picking_logic(base_sha, pr, slug, token, service, enterprise_url) + base_picking_logic(base_sha, pr, slug, token, service, enterprise_url, args) diff --git a/codecov_cli/commands/commit.py b/codecov_cli/commands/commit.py index 316fbdff..b2b14a7e 100644 --- a/codecov_cli/commands/commit.py +++ b/codecov_cli/commands/commit.py @@ -4,6 +4,7 @@ import click 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 @@ -47,19 +48,11 @@ def create_commit( fail_on_error: bool, ): enterprise_url = ctx.obj.get("enterprise_url") + args = get_cli_args(ctx) logger.debug( "Starting create commit process", extra=dict( - extra_log_attributes=dict( - commit_sha=commit_sha, - parent_sha=parent_sha, - pr=pull_request_number, - branch=branch, - slug=slug, - token=token, - service=git_service, - enterprise_url=enterprise_url, - ) + extra_log_attributes=args, ), ) create_commit_logic( @@ -72,4 +65,5 @@ def create_commit( git_service, enterprise_url, fail_on_error, + args, ) diff --git a/codecov_cli/commands/create_report_result.py b/codecov_cli/commands/create_report_result.py index 27dae8da..28648f23 100644 --- a/codecov_cli/commands/create_report_result.py +++ b/codecov_cli/commands/create_report_result.py @@ -2,6 +2,7 @@ import click +from codecov_cli.helpers.args import get_cli_args from codecov_cli.helpers.options import global_options from codecov_cli.services.report import create_report_results_logic from codecov_cli.types import CommandContext @@ -25,19 +26,13 @@ def create_report_results( fail_on_error: bool, ): enterprise_url = ctx.obj.get("enterprise_url") + args = get_cli_args(ctx) logger.debug( "Creating report results", extra=dict( - extra_log_attributes=dict( - commit_sha=commit_sha, - code=code, - slug=slug, - service=git_service, - enterprise_url=enterprise_url, - token=token, - ) + extra_log_attributes=args, ), ) create_report_results_logic( - commit_sha, code, slug, git_service, token, enterprise_url, fail_on_error + commit_sha, code, slug, git_service, token, enterprise_url, fail_on_error, args ) diff --git a/codecov_cli/commands/empty_upload.py b/codecov_cli/commands/empty_upload.py index 7af5a246..d68e0224 100644 --- a/codecov_cli/commands/empty_upload.py +++ b/codecov_cli/commands/empty_upload.py @@ -4,6 +4,7 @@ import click 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.empty_upload import empty_upload_logic @@ -26,19 +27,13 @@ def empty_upload( fail_on_error: typing.Optional[bool], ): enterprise_url = ctx.obj.get("enterprise_url") + args = get_cli_args(ctx) logger.debug( "Starting empty upload process", extra=dict( - extra_log_attributes=dict( - commit_sha=commit_sha, - slug=slug, - token=token, - service=git_service, - enterprise_url=enterprise_url, - fail_on_error=fail_on_error, - ) + extra_log_attributes=args, ), ) return empty_upload_logic( - commit_sha, slug, token, git_service, enterprise_url, fail_on_error, force + commit_sha, slug, token, git_service, enterprise_url, fail_on_error, force, args ) diff --git a/codecov_cli/commands/get_report_results.py b/codecov_cli/commands/get_report_results.py index 6487dd0c..017025d1 100644 --- a/codecov_cli/commands/get_report_results.py +++ b/codecov_cli/commands/get_report_results.py @@ -28,17 +28,11 @@ def get_report_results( fail_on_error: bool, ): enterprise_url = ctx.obj.get("enterprise_url") + args = get_cli_args(ctx) logger.debug( "Getting report results", extra=dict( - extra_log_attributes=dict( - commit_sha=commit_sha, - code=code, - slug=slug, - service=git_service, - enterprise_url=enterprise_url, - token=token, - ) + extra_log_attributes=args, ), ) encoded_slug = encode_slug(slug) diff --git a/codecov_cli/commands/labelanalysis.py b/codecov_cli/commands/labelanalysis.py index ec1cfb24..cb664994 100644 --- a/codecov_cli/commands/labelanalysis.py +++ b/codecov_cli/commands/labelanalysis.py @@ -9,6 +9,7 @@ from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum from codecov_cli.helpers import request +from codecov_cli.helpers.args import get_cli_args from codecov_cli.helpers.config import CODECOV_API_URL from codecov_cli.helpers.validators import validate_commit_sha from codecov_cli.runners import get_runner @@ -89,18 +90,11 @@ def label_analysis( runner_params: List[str], ): enterprise_url = ctx.obj.get("enterprise_url") + args = get_cli_args(ctx) logger.debug( "Starting label analysis", extra=dict( - extra_log_attributes=dict( - head_commit_sha=head_commit_sha, - base_commit_sha=base_commit_sha, - token=token, - runner_name=runner_name, - enterprise_url=enterprise_url, - max_wait_time=max_wait_time, - dry_run=dry_run, - ) + extra_log_attributes=args, ), ) if head_commit_sha == base_commit_sha: diff --git a/codecov_cli/commands/process_test_results.py b/codecov_cli/commands/process_test_results.py index 1bf4558a..f887c5b9 100644 --- a/codecov_cli/commands/process_test_results.py +++ b/codecov_cli/commands/process_test_results.py @@ -13,11 +13,13 @@ parse_junit_xml, ) +from codecov_cli.helpers.args import get_cli_args from codecov_cli.helpers.request import ( log_warnings_and_errors_if_any, send_post_request, ) from codecov_cli.services.upload.file_finder import select_file_finder +from codecov_cli.types import CommandContext logger = logging.getLogger("codecovcli") @@ -83,8 +85,14 @@ class TestResultsNotificationPayload: @click.command() @process_test_results_options +@click.pass_context def process_test_results( - dir=None, files=None, exclude_folders=None, disable_search=None, provider_token=None + ctx: CommandContext, + dir=None, + files=None, + exclude_folders=None, + disable_search=None, + provider_token=None, ): if provider_token is None: raise click.ClickException( @@ -130,10 +138,11 @@ def process_test_results( # GITHUB_REF is documented here: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables pr_number = ref.split("/")[2] - create_github_comment(provider_token, slug, pr_number, message) + args = get_cli_args(ctx) + create_github_comment(provider_token, slug, pr_number, message, args) -def create_github_comment(token, repo_slug, pr_number, message): +def create_github_comment(token, repo_slug, pr_number, message, args): url = f"https://api.github.com/repos/{repo_slug}/issues/{pr_number}/comments" headers = { @@ -144,7 +153,14 @@ def create_github_comment(token, repo_slug, pr_number, message): logger.info("Posting github comment") log_warnings_and_errors_if_any( - send_post_request(url=url, data={"body": message}, headers=headers), + send_post_request( + url=url, + data={ + "body": message, + "cli_args": args, + }, + headers=headers, + ), "Posting test results comment", ) diff --git a/codecov_cli/commands/report.py b/codecov_cli/commands/report.py index 25d63194..7169ba4f 100644 --- a/codecov_cli/commands/report.py +++ b/codecov_cli/commands/report.py @@ -3,6 +3,7 @@ import click from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum +from codecov_cli.helpers.args import get_cli_args from codecov_cli.helpers.options import global_options from codecov_cli.services.report import create_report_logic from codecov_cli.types import CommandContext @@ -36,17 +37,11 @@ def create_report( pull_request_number: int, ): enterprise_url = ctx.obj.get("enterprise_url") + args = get_cli_args(ctx) logger.debug( "Starting create report process", extra=dict( - extra_log_attributes=dict( - commit_sha=commit_sha, - code=code, - slug=slug, - service=git_service, - enterprise_url=enterprise_url, - token=token, - ) + extra_log_attributes=args, ), ) res = create_report_logic( @@ -58,6 +53,7 @@ def create_report( enterprise_url, pull_request_number, fail_on_error, + args, ) if not res.error: logger.info( diff --git a/codecov_cli/commands/send_notifications.py b/codecov_cli/commands/send_notifications.py index 7798a6e3..0b7b79d1 100644 --- a/codecov_cli/commands/send_notifications.py +++ b/codecov_cli/commands/send_notifications.py @@ -4,6 +4,7 @@ import click 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.upload_completion import upload_completion_logic @@ -24,16 +25,11 @@ def send_notifications( fail_on_error: bool, ): enterprise_url = ctx.obj.get("enterprise_url") + args = get_cli_args(ctx) logger.debug( "Sending notifications process has started", extra=dict( - extra_log_attributes=dict( - commit_sha=commit_sha, - slug=slug, - token=token, - service=git_service, - enterprise_url=enterprise_url, - ) + extra_log_attributes=args, ), ) return upload_completion_logic( @@ -43,4 +39,5 @@ def send_notifications( git_service, enterprise_url, fail_on_error, + args, ) diff --git a/codecov_cli/commands/staticanalysis.py b/codecov_cli/commands/staticanalysis.py index 265efcc9..a876f90b 100644 --- a/codecov_cli/commands/staticanalysis.py +++ b/codecov_cli/commands/staticanalysis.py @@ -6,6 +6,7 @@ import click from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum +from codecov_cli.helpers.args import get_cli_args from codecov_cli.helpers.validators import validate_commit_sha from codecov_cli.services.staticanalysis import run_analysis_entrypoint from codecov_cli.types import CommandContext @@ -59,19 +60,11 @@ def static_analysis( folders_to_exclude: typing.List[pathlib.Path], ): enterprise_url = ctx.obj.get("enterprise_url") + args = get_cli_args(ctx) logger.debug( "Starting Static Analysis processing", extra=dict( - extra_log_attributes=dict( - foldertosearch=foldertosearch, - numberprocesses=numberprocesses, - pattern=pattern, - commit_sha=commit, - token=token, - force=force, - folders_to_exclude=folders_to_exclude, - enterprise_url=enterprise_url, - ) + extra_log_attributes=args, ), ) return asyncio.run( @@ -85,5 +78,6 @@ def static_analysis( force, list(folders_to_exclude), enterprise_url, + args, ) ) diff --git a/codecov_cli/commands/upload.py b/codecov_cli/commands/upload.py index c14f5614..dfbfe431 100644 --- a/codecov_cli/commands/upload.py +++ b/codecov_cli/commands/upload.py @@ -6,6 +6,7 @@ import click from codecov_cli.fallbacks import CodecovOption, FallbackFieldEnum +from codecov_cli.helpers.args import get_cli_args from codecov_cli.helpers.options import global_options from codecov_cli.services.upload import do_upload_logic from codecov_cli.types import CommandContext @@ -225,36 +226,11 @@ def do_upload( cli_config = codecov_yaml.get("cli", {}) ci_adapter = ctx.obj.get("ci_adapter") enterprise_url = ctx.obj.get("enterprise_url") + args = get_cli_args(ctx) logger.debug( "Starting upload processing", extra=dict( - extra_log_attributes=dict( - branch=branch, - build_code=build_code, - build_url=build_url, - commit_sha=commit_sha, - disable_file_fixes=disable_file_fixes, - disable_search=disable_search, - enterprise_url=enterprise_url, - env_vars=env_vars, - files_search_exclude_folders=files_search_exclude_folders, - files_search_explicitly_listed_files=files_search_explicitly_listed_files, - files_search_root_folder=files_search_root_folder, - flags=flags, - git_service=git_service, - handle_no_reports_found=handle_no_reports_found, - job_code=job_code, - name=name, - network_filter=network_filter, - network_prefix=network_prefix, - network_root_folder=network_root_folder, - plugin_names=plugin_names, - pull_request_number=pull_request_number, - report_code=report_code, - slug=slug, - token=token, - upload_file_type=report_type, - ) + extra_log_attributes=args, ), ) do_upload_logic( @@ -289,4 +265,5 @@ def do_upload( token=token, upload_file_type=report_type, use_legacy_uploader=use_legacy_uploader, + args=args, ) diff --git a/codecov_cli/commands/upload_process.py b/codecov_cli/commands/upload_process.py index 2d12a587..e6efecc2 100644 --- a/codecov_cli/commands/upload_process.py +++ b/codecov_cli/commands/upload_process.py @@ -7,6 +7,7 @@ from codecov_cli.commands.commit import create_commit from codecov_cli.commands.report import create_report from codecov_cli.commands.upload import do_upload, global_upload_options +from codecov_cli.helpers.args import get_cli_args from codecov_cli.helpers.options import global_options from codecov_cli.types import CommandContext @@ -53,35 +54,11 @@ def upload_process( handle_no_reports_found: bool, report_type: str, ): + args = get_cli_args(ctx) logger.debug( "Starting upload process", extra=dict( - extra_log_attributes=dict( - commit_sha=commit_sha, - report_code=report_code, - build_code=build_code, - build_url=build_url, - job_code=job_code, - env_vars=env_vars, - flags=flags, - name=name, - network_filter=network_filter, - network_prefix=network_prefix, - network_root_folder=network_root_folder, - files_search_root_folder=files_search_root_folder, - files_search_exclude_folders=files_search_exclude_folders, - files_search_explicitly_listed_files=files_search_explicitly_listed_files, - plugin_names=plugin_names, - token=token, - branch=branch, - slug=slug, - pull_request_number=pull_request_number, - git_service=git_service, - disable_search=disable_search, - disable_file_fixes=disable_file_fixes, - fail_on_error=fail_on_error, - handle_no_reports_found=handle_no_reports_found, - ) + extra_log_attributes=args, ), ) diff --git a/codecov_cli/helpers/args.py b/codecov_cli/helpers/args.py new file mode 100644 index 00000000..74c82f7b --- /dev/null +++ b/codecov_cli/helpers/args.py @@ -0,0 +1,31 @@ +import json +import logging +from pathlib import PosixPath + +import click + +from codecov_cli import __version__ + +logger = logging.getLogger("codecovcli") + + +def get_cli_args(ctx: click.Context): + args = ctx.obj["cli_args"] + args["command"] = str(ctx.command.name) + args["version"] = f"cli-{__version__}" + args.update(ctx.params) + if "token" in args: + del args["token"] + + filtered_args = {} + for k in args.keys(): + try: + if type(args[k]) == PosixPath: + filtered_args[k] = str(args[k]) + else: + json.dumps(args[k]) + filtered_args[k] = args[k] + except Exception as e: + continue + + return filtered_args diff --git a/codecov_cli/main.py b/codecov_cli/main.py index e1a50728..9505aaa6 100644 --- a/codecov_cli/main.py +++ b/codecov_cli/main.py @@ -51,6 +51,8 @@ def cli( enterprise_url: str, verbose: bool = False, ): + ctx.obj["cli_args"] = ctx.params + ctx.obj["cli_args"]["version"] = f"cli-{__version__}" configure_logger(logger, log_level=(logging.DEBUG if verbose else logging.INFO)) ctx.help_option_names = ["-h", "--help"] ctx.obj["ci_adapter"] = get_ci_adapter(auto_load_params_from) diff --git a/codecov_cli/services/commit/__init__.py b/codecov_cli/services/commit/__init__.py index a465c0df..85c4d256 100644 --- a/codecov_cli/services/commit/__init__.py +++ b/codecov_cli/services/commit/__init__.py @@ -23,6 +23,7 @@ def create_commit_logic( service: typing.Optional[str], enterprise_url: typing.Optional[str] = None, fail_on_error: bool = False, + args: dict = None, ): encoded_slug = encode_slug(slug) sending_result = send_commit_data( @@ -34,6 +35,7 @@ def create_commit_logic( token=token, service=service, enterprise_url=enterprise_url, + args=args, ) log_warnings_and_errors_if_any(sending_result, "Commit creating", fail_on_error) @@ -41,7 +43,15 @@ def create_commit_logic( def send_commit_data( - commit_sha, parent_sha, pr, branch, slug, token, service, enterprise_url + commit_sha, + parent_sha, + pr, + branch, + slug, + token, + service, + enterprise_url, + args, ): # this is how the CLI receives the username of the user to whom the fork belongs # to and the branch name from the action @@ -54,12 +64,17 @@ def send_commit_data( headers = get_token_header_or_fail(token) data = { + "branch": branch, + "cli_args": args, "commitid": commit_sha, "parent_commit_id": parent_sha, "pullid": pr, - "branch": branch, } upload_url = enterprise_url or CODECOV_API_URL url = f"{upload_url}/upload/{service}/{slug}/commits" - return send_post_request(url=url, data=data, headers=headers) + return send_post_request( + url=url, + data=data, + headers=headers, + ) diff --git a/codecov_cli/services/commit/base_picking.py b/codecov_cli/services/commit/base_picking.py index 811b2d7b..20767132 100644 --- a/codecov_cli/services/commit/base_picking.py +++ b/codecov_cli/services/commit/base_picking.py @@ -10,8 +10,9 @@ logger = logging.getLogger("codecovcli") -def base_picking_logic(base_sha, pr, slug, token, service, enterprise_url): +def base_picking_logic(base_sha, pr, slug, token, service, enterprise_url, args): data = { + "cli_args": args, "user_provided_base_sha": base_sha, } headers = get_token_header_or_fail(token) diff --git a/codecov_cli/services/empty_upload/__init__.py b/codecov_cli/services/empty_upload/__init__.py index 57bb32b9..7c8b0682 100644 --- a/codecov_cli/services/empty_upload/__init__.py +++ b/codecov_cli/services/empty_upload/__init__.py @@ -13,14 +13,26 @@ def empty_upload_logic( - commit_sha, slug, token, git_service, enterprise_url, fail_on_error, should_force + commit_sha, + slug, + token, + git_service, + enterprise_url, + fail_on_error, + should_force, + args, ): encoded_slug = encode_slug(slug) headers = get_token_header_or_fail(token) upload_url = enterprise_url or CODECOV_API_URL url = f"{upload_url}/upload/{git_service}/{encoded_slug}/commits/{commit_sha}/empty-upload" sending_result = send_post_request( - url=url, headers=headers, data={"should_force": should_force} + url=url, + headers=headers, + data={ + "cli_args": args, + "should_force": should_force, + }, ) log_warnings_and_errors_if_any(sending_result, "Empty Upload", fail_on_error) if sending_result.status_code == 200: diff --git a/codecov_cli/services/report/__init__.py b/codecov_cli/services/report/__init__.py index b8e7c445..a3734ad1 100644 --- a/codecov_cli/services/report/__init__.py +++ b/codecov_cli/services/report/__init__.py @@ -28,6 +28,7 @@ def create_report_logic( enterprise_url: str, pull_request_number: int, fail_on_error: bool = False, + args: dict = None, ): encoded_slug = encode_slug(slug) sending_result = send_create_report_request( @@ -38,15 +39,26 @@ def create_report_logic( encoded_slug, enterprise_url, pull_request_number, + args, ) log_warnings_and_errors_if_any(sending_result, "Report creating", fail_on_error) return sending_result def send_create_report_request( - commit_sha, code, service, token, encoded_slug, enterprise_url, pull_request_number + commit_sha, + code, + service, + token, + encoded_slug, + enterprise_url, + pull_request_number, + args, ): - data = {"code": code} + data = { + "cli_args": args, + "code": code, + } headers = get_token_header(token) upload_url = enterprise_url or CODECOV_API_URL url = f"{upload_url}/upload/{service}/{encoded_slug}/commits/{commit_sha}/reports" @@ -61,6 +73,7 @@ def create_report_results_logic( token: str, enterprise_url: str, fail_on_error: bool = False, + args: dict = None, ): encoded_slug = encode_slug(slug) sending_result = send_reports_result_request( @@ -79,12 +92,21 @@ def create_report_results_logic( def send_reports_result_request( - commit_sha, report_code, encoded_slug, service, token, enterprise_url + commit_sha, + report_code, + encoded_slug, + service, + token, + enterprise_url, + args, ): + data = { + "cli_args": args, + } headers = get_token_header_or_fail(token) upload_url = enterprise_url or CODECOV_API_URL url = f"{upload_url}/upload/{service}/{encoded_slug}/commits/{commit_sha}/reports/{report_code}/results" - return send_post_request(url=url, headers=headers) + return send_post_request(url=url, data=data, headers=headers) def send_reports_result_get_request( diff --git a/codecov_cli/services/staticanalysis/__init__.py b/codecov_cli/services/staticanalysis/__init__.py index f82457df..aedd82c1 100644 --- a/codecov_cli/services/staticanalysis/__init__.py +++ b/codecov_cli/services/staticanalysis/__init__.py @@ -33,6 +33,7 @@ async def run_analysis_entrypoint( should_force: bool, folders_to_exclude: typing.List[Path], enterprise_url: typing.Optional[str], + args: dict, ): ff = select_file_finder(config) files = list(ff.find_files(folder, pattern, folders_to_exclude)) diff --git a/codecov_cli/services/upload/__init__.py b/codecov_cli/services/upload/__init__.py index a8fc64d7..411c820c 100644 --- a/codecov_cli/services/upload/__init__.py +++ b/codecov_cli/services/upload/__init__.py @@ -25,6 +25,7 @@ def do_upload_logic( versioning_system: VersioningSystemInterface, ci_adapter: CIAdapterBase, *, + args: dict = None, branch: typing.Optional[str], build_code: typing.Optional[str], build_url: typing.Optional[str], @@ -127,6 +128,7 @@ def do_upload_logic( ci_service, git_service, enterprise_url, + args, ) else: logger.info("dry-run option activated. NOT sending data to Codecov.") diff --git a/codecov_cli/services/upload/legacy_upload_sender.py b/codecov_cli/services/upload/legacy_upload_sender.py index 283b204d..67711ef3 100644 --- a/codecov_cli/services/upload/legacy_upload_sender.py +++ b/codecov_cli/services/upload/legacy_upload_sender.py @@ -51,6 +51,7 @@ def send_upload_data( ci_service: typing.Optional[str] = None, git_service: typing.Optional[str] = None, enterprise_url: typing.Optional[str] = None, + args: dict = None, ) -> UploadSendingResult: params = { "package": f"codecov-cli/{codecov_cli_version}", @@ -72,9 +73,13 @@ def send_upload_data( logger.warning("Token is empty.") headers = {"X-Upload-Token": ""} + data = { + "cli_args": args, + } + upload_url = enterprise_url or LEGACY_CODECOV_API_URL resp = send_post_request( - f"{upload_url}/upload/v4", headers=headers, params=params + f"{upload_url}/upload/v4", data=data, headers=headers, params=params ) if resp.status_code >= 400: return resp diff --git a/codecov_cli/services/upload/upload_sender.py b/codecov_cli/services/upload/upload_sender.py index 4b9817d2..22f8924a 100644 --- a/codecov_cli/services/upload/upload_sender.py +++ b/codecov_cli/services/upload/upload_sender.py @@ -42,15 +42,17 @@ def send_upload_data( ci_service: typing.Optional[str] = None, git_service: typing.Optional[str] = None, enterprise_url: typing.Optional[str] = None, + args: dict = None, ) -> RequestResult: data = { + "ci_service": ci_service, "ci_url": build_url, - "flags": flags, + "cli_args": args, "env": env_vars, - "name": name, + "flags": flags, "job_code": job_code, + "name": name, "version": codecov_cli_version, - "ci_service": ci_service, } headers = get_token_header(token) encoded_slug = encode_slug(slug) diff --git a/codecov_cli/services/upload_completion/__init__.py b/codecov_cli/services/upload_completion/__init__.py index 20f68872..b595ba7f 100644 --- a/codecov_cli/services/upload_completion/__init__.py +++ b/codecov_cli/services/upload_completion/__init__.py @@ -13,13 +13,22 @@ def upload_completion_logic( - commit_sha, slug, token, git_service, enterprise_url, fail_on_error=False + commit_sha, + slug, + token, + git_service, + enterprise_url, + fail_on_error=False, + args=None, ): encoded_slug = encode_slug(slug) headers = get_token_header(token) upload_url = enterprise_url or CODECOV_API_URL url = f"{upload_url}/upload/{git_service}/{encoded_slug}/commits/{commit_sha}/upload-complete" - sending_result = send_post_request(url=url, headers=headers) + data = { + "cli_args": args, + } + sending_result = send_post_request(url=url, data=data, headers=headers) log_warnings_and_errors_if_any( sending_result, "Upload Completion", fail_on_error=fail_on_error ) diff --git a/tests/commands/test_process_test_results.py b/tests/commands/test_process_test_results.py index d0c62eb7..ca073037 100644 --- a/tests/commands/test_process_test_results.py +++ b/tests/commands/test_process_test_results.py @@ -48,7 +48,8 @@ def test_process_test_results( mocked_post.assert_called_with( url="https://api.github.com/repos/fake/repo/issues/pull/comments", data={ - "body": "### :x: Failed Test Results: \nCompleted 4 tests with **`1 failed`**, 3 passed and 0 skipped.\n
View the full list of failed tests\n\n| **Test Description** | **Failure message** |\n| :-- | :-- |\n|
Testsuite:
api.temp.calculator.test_calculator::test_divide

Test name:
pytest
|
def
test_divide():
> assert Calculator.divide(1, 2) == 0.5
E assert 1.0 == 0.5
E + where 1.0 = <function Calculator.divide at 0x104c9eb90>(1, 2)
E + where <function Calculator.divide at 0x104c9eb90> = Calculator.divide
.../temp/calculator/test_calculator.py:30: AssertionError
|" + "body": "### :x: Failed Test Results: \nCompleted 4 tests with **`1 failed`**, 3 passed and 0 skipped.\n
View the full list of failed tests\n\n| **Test Description** | **Failure message** |\n| :-- | :-- |\n|
Testsuite:
api.temp.calculator.test_calculator::test_divide

Test name:
pytest
|
def
test_divide():
> assert Calculator.divide(1, 2) == 0.5
E assert 1.0 == 0.5
E + where 1.0 = <function Calculator.divide at 0x104c9eb90>(1, 2)
E + where <function Calculator.divide at 0x104c9eb90> = Calculator.divide
.../temp/calculator/test_calculator.py:30: AssertionError
|", + "cli_args": {'auto_load_params_from': None, 'codecov_yml_path': None, 'enterprise_url': None, 'verbose': False, 'version': 'cli-0.7.4', 'command': 'process-test-results', 'provider_token': 'whatever', 'disable_search': True, 'dir': os.getcwd(), 'exclude_folders': ()}, }, headers={ "Accept": "application/vnd.github+json", @@ -221,4 +222,4 @@ def test_process_test_results_missing_step_summary(mocker, tmpdir): "Error: Error getting step summary file path from environment. Can't find GITHUB_STEP_SUMMARY environment variable.", ] for log in expected_logs: - assert log in result.output \ No newline at end of file + assert log in result.output diff --git a/tests/helpers/test_args.py b/tests/helpers/test_args.py new file mode 100644 index 00000000..503b877e --- /dev/null +++ b/tests/helpers/test_args.py @@ -0,0 +1,51 @@ +import os +from pathlib import PosixPath + +import click + +from codecov_cli import __version__ +from codecov_cli.helpers.args import get_cli_args + + +def test_get_cli_args(): + ctx = click.Context(click.Command("do-upload")) + ctx.obj = {} + ctx.obj["cli_args"] = { + "verbose": True, + } + ctx.params = { + "branch": "fake_branch", + "token": "fakeTOKEN", + } + + expected = { + "branch": "fake_branch", + "command": "do-upload", + "verbose": True, + "version": f"cli-{__version__}", + } + + assert get_cli_args(ctx) == expected + + +def test_get_cli_args_with_posix(): + ctx = click.Context(click.Command("do-upload")) + ctx.obj = {} + ctx.obj["cli_args"] = { + "verbose": True, + } + ctx.params = { + "branch": "fake_branch", + "path": PosixPath(os.getcwd()), + "token": "fakeTOKEN", + } + + expected = { + "branch": "fake_branch", + "command": "do-upload", + "path": str(PosixPath(os.getcwd())), + "verbose": True, + "version": f"cli-{__version__}", + } + + assert get_cli_args(ctx) == expected diff --git a/tests/helpers/test_upload_sender.py b/tests/helpers/test_upload_sender.py index 14195a43..9033082c 100644 --- a/tests/helpers/test_upload_sender.py +++ b/tests/helpers/test_upload_sender.py @@ -16,6 +16,7 @@ random_token = "f359afb9-8a2a-42ab-a448-c3d267ff495b" random_sha = "845548c6b95223f12e8317a1820705f64beaf69e" named_upload_data = { + "args": None, "upload_file_type": "coverage", "report_code": "report_code", "env_vars": {}, @@ -46,13 +47,14 @@ "git_service": "github", } request_data = { + "ci_service": "ci_service", "ci_url": "build_url", + "cli_args": None, "env": {}, "flags": "flags", "job_code": "job_code", "name": "name", "version": codecov_cli_version, - "ci_service": "ci_service", } @@ -232,7 +234,7 @@ def test_upload_sender_post_called_with_right_parameters_tokenless( mocker, ): headers = {} - + mocked_legacy_upload_endpoint.match = [ matchers.json_params_matcher(request_data), matchers.header_matcher(headers), diff --git a/tests/services/commit/test_commit_service.py b/tests/services/commit/test_commit_service.py index 240040c2..3f4ae1fd 100644 --- a/tests/services/commit/test_commit_service.py +++ b/tests/services/commit/test_commit_service.py @@ -48,6 +48,7 @@ def test_commit_command_with_warnings(mocker): token="token", service="service", enterprise_url=None, + args=None, ) @@ -76,6 +77,7 @@ def test_commit_command_with_error(mocker): token="token", service="service", enterprise_url=None, + args={}, ) out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) @@ -96,6 +98,7 @@ def test_commit_command_with_error(mocker): token="token", service="service", enterprise_url=None, + args={}, ) @@ -114,6 +117,7 @@ def test_commit_sender_200(mocker): token, "service", None, + None, ) assert res.error is None assert res.warnings == [] @@ -135,6 +139,7 @@ def test_commit_sender_403(mocker): token, "service", None, + None, ) assert res.error == RequestError( code="HTTP Error 403", @@ -160,14 +165,16 @@ def test_commit_sender_with_forked_repo(mocker): None, "github", None, + None, ) mocked_response.assert_called_with( url="https://api.codecov.io/upload/github/codecov::::codecov-cli/commits", data={ + "branch": "user_forked_repo/codecov-cli:branch", + "cli_args": None, "commitid": "commit_sha", "parent_commit_id": "parent_sha", "pullid": "1", - "branch": "user_forked_repo/codecov-cli:branch", }, headers=None, ) diff --git a/tests/services/empty_upload/test_empty_upload.py b/tests/services/empty_upload/test_empty_upload.py index cc714470..16f9f946 100644 --- a/tests/services/empty_upload/test_empty_upload.py +++ b/tests/services/empty_upload/test_empty_upload.py @@ -21,7 +21,7 @@ def test_empty_upload_with_warnings(mocker): runner = CliRunner() with runner.isolation() as outstreams: res = empty_upload_logic( - "commit_sha", "owner/repo", uuid.uuid4(), "service", None, False, False + "commit_sha", "owner/repo", uuid.uuid4(), "service", None, False, False, None ) out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) assert out_bytes == [ @@ -50,7 +50,7 @@ def test_empty_upload_with_error(mocker): runner = CliRunner() with runner.isolation() as outstreams: res = empty_upload_logic( - "commit_sha", "owner/repo", uuid.uuid4(), "service", None, False, False + "commit_sha", "owner/repo", uuid.uuid4(), "service", None, False, False, None ) out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) @@ -77,7 +77,7 @@ def test_empty_upload_200(mocker): runner = CliRunner() with runner.isolation() as outstreams: res = empty_upload_logic( - "commit_sha", "owner/repo", token, "service", None, False, False + "commit_sha", "owner/repo", token, "service", None, False, False, None ) out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) assert out_bytes == [ @@ -97,7 +97,7 @@ def test_empty_upload_403(mocker): ) token = uuid.uuid4() res = empty_upload_logic( - "commit_sha", "owner/repo", token, "service", None, False, False + "commit_sha", "owner/repo", token, "service", None, False, False, None ) assert res.error == RequestError( code="HTTP Error 403", @@ -122,7 +122,7 @@ def test_empty_upload_force(mocker): runner = CliRunner() with runner.isolation() as outstreams: res = empty_upload_logic( - "commit_sha", "owner/repo", token, "service", None, False, True + "commit_sha", "owner/repo", token, "service", None, False, True, None ) out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) assert out_bytes == [ diff --git a/tests/services/report/test_report_results.py b/tests/services/report/test_report_results.py index 3b22cc0d..27808d23 100644 --- a/tests/services/report/test_report_results.py +++ b/tests/services/report/test_report_results.py @@ -31,6 +31,7 @@ def test_report_results_command_with_warnings(mocker): slug="owner/repo", token="token", enterprise_url=None, + args=None, ) out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) @@ -74,6 +75,7 @@ def test_report_results_command_with_error(mocker): slug="owner/repo", token="token", enterprise_url=None, + args=None, ) out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) @@ -99,7 +101,7 @@ def test_report_results_request_200(mocker): ) token = uuid.uuid4() res = send_reports_result_request( - "commit_sha", "report_code", "encoded_slug", "service", token, None + "commit_sha", "report_code", "encoded_slug", "service", token, None, None ) assert res.error is None assert res.warnings == [] @@ -113,7 +115,7 @@ def test_report_results_403(mocker): ) token = uuid.uuid4() res = send_reports_result_request( - "commit_sha", "report_code", "encoded_slug", "service", token, None + "commit_sha", "report_code", "encoded_slug", "service", token, None, None ) assert res.error == RequestError( code="HTTP Error 403", diff --git a/tests/services/report/test_report_service.py b/tests/services/report/test_report_service.py index 8d31a112..b3a0f04a 100644 --- a/tests/services/report/test_report_service.py +++ b/tests/services/report/test_report_service.py @@ -20,6 +20,7 @@ def test_send_create_report_request_200(mocker): "owner::::repo", "enterprise_url", 1, + None, ) assert res.error is None assert res.warnings == [] @@ -32,7 +33,7 @@ def test_send_create_report_request_403(mocker): return_value=mocker.MagicMock(status_code=403, text="Permission denied"), ) res = send_create_report_request( - "commit_sha", "code", "github", uuid.uuid4(), "owner::::repo", None, 1 + "commit_sha", "code", "github", uuid.uuid4(), "owner::::repo", None, 1, None ) assert res.error == RequestError( code="HTTP Error 403", @@ -77,7 +78,7 @@ def test_create_report_command_with_warnings(mocker): text="", ) mocked_send_request.assert_called_with( - "commit_sha", "code", "github", "token", "owner::::repo", None, 1 + "commit_sha", "code", "github", "token", "owner::::repo", None, 1, None ) @@ -123,5 +124,5 @@ def test_create_report_command_with_error(mocker): warnings=[], ) mock_send_report_data.assert_called_with( - "commit_sha", "code", "github", "token", "owner::::repo", "enterprise_url", 1 + "commit_sha", "code", "github", "token", "owner::::repo", "enterprise_url", 1, None ) diff --git a/tests/services/static_analysis/test_static_analysis_service.py b/tests/services/static_analysis/test_static_analysis_service.py index c8721d98..262a3130 100644 --- a/tests/services/static_analysis/test_static_analysis_service.py +++ b/tests/services/static_analysis/test_static_analysis_service.py @@ -136,6 +136,7 @@ async def side_effect(*args, **kwargs): should_force=False, folders_to_exclude=[], enterprise_url=None, + args=None ) mock_file_finder.assert_called_with({}) mock_file_finder.return_value.find_files.assert_called() @@ -211,6 +212,7 @@ async def side_effect(client, all_data, el): should_force=False, folders_to_exclude=[], enterprise_url=None, + args=None, ) assert "Unknown error cancelled the upload tasks." in str(exp.value) mock_file_finder.assert_called_with({}) @@ -384,6 +386,7 @@ async def side_effect(*args, **kwargs): should_force=False, folders_to_exclude=[], enterprise_url=None, + args=None, ) mock_file_finder.assert_called_with({}) mock_file_finder.return_value.find_files.assert_called() @@ -458,6 +461,7 @@ async def side_effect(*args, **kwargs): should_force=False, folders_to_exclude=[], enterprise_url=None, + args=None, ) mock_file_finder.assert_called_with({}) mock_file_finder.return_value.find_files.assert_called() @@ -534,6 +538,7 @@ async def side_effect(*args, **kwargs): should_force=True, folders_to_exclude=[], enterprise_url=None, + args=None, ) mock_file_finder.assert_called_with({}) mock_file_finder.return_value.find_files.assert_called() @@ -605,6 +610,7 @@ async def side_effect(*args, **kwargs): should_force=False, folders_to_exclude=[], enterprise_url=None, + args=None, ) mock_file_finder.assert_called_with({}) mock_file_finder.return_value.find_files.assert_called() diff --git a/tests/services/upload/test_upload_service.py b/tests/services/upload/test_upload_service.py index 8f7ad3e4..4bba13e9 100644 --- a/tests/services/upload/test_upload_service.py +++ b/tests/services/upload/test_upload_service.py @@ -70,6 +70,7 @@ def test_do_upload_logic_happy_path_legacy_uploader(mocker): pull_request_number="pr", git_service="git_service", enterprise_url=None, + args=None, ) out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) assert out_bytes == [ @@ -103,6 +104,7 @@ def test_do_upload_logic_happy_path_legacy_uploader(mocker): "service", "git_service", None, + None, ) @@ -192,6 +194,7 @@ def test_do_upload_logic_happy_path(mocker): "service", "git_service", None, + None, ) @@ -534,6 +537,7 @@ def test_do_upload_logic_happy_path_test_results(mocker): pull_request_number="pr", git_service="git_service", enterprise_url=None, + args={"args": "fake_args"} ) out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) assert out_bytes == [ @@ -565,4 +569,5 @@ def test_do_upload_logic_happy_path_test_results(mocker): "service", "git_service", None, + {"args": "fake_args"} )