From c6e62dff2c03eb9548df5d1d9c80c2feb14477fa Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Tue, 15 Oct 2024 15:18:52 -0700 Subject: [PATCH 01/13] setup and cleanup --- requirements.txt | 12 +++++++++++- setup.py | 1 + 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 2d33a3d3..0a4e2252 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ # -# This file is autogenerated by pip-compile with Python 3.11 +# This file is autogenerated by pip-compile with Python 3.12 # by the following command: # # pip-compile setup.py @@ -28,6 +28,16 @@ idna==3.4 # rfc3986 ijson==3.2.3 # via codecov-cli (setup.py) +iniconfig==2.0.0 + # via pytest +packaging==24.1 + # via pytest +pluggy==1.5.0 + # via pytest +pytest==8.3.3 + # via pytest-mock +pytest-mock==3.14.0 + # via codecov-cli (setup.py) pyyaml==6.0.1 # via codecov-cli (setup.py) regex==2023.12.25 diff --git a/setup.py b/setup.py index b1c59f32..72eb5bb7 100644 --- a/setup.py +++ b/setup.py @@ -26,6 +26,7 @@ "tree-sitter==0.20.*", "test-results-parser==0.5.*", "regex", + "pytest-mock" ], entry_points={ "console_scripts": [ From 6f67a54620ce25dbd943ffae8d4ba214692f79e2 Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Tue, 15 Oct 2024 15:19:58 -0700 Subject: [PATCH 02/13] remove token enforcement for true tokenless endpoints --- codecov_cli/services/commit/__init__.py | 8 ++-- codecov_cli/services/report/__init__.py | 13 +++---- codecov_cli/services/upload/__init__.py | 2 +- codecov_cli/services/upload/upload_sender.py | 2 +- .../services/upload_completion/__init__.py | 4 +- tests/services/commit/test_commit_service.py | 30 ++++++++++++++ .../empty_upload/test_empty_upload.py | 15 +++++++ tests/services/report/test_report_results.py | 39 ++++++++++++++++--- tests/services/report/test_report_service.py | 24 +++++++++++- .../test_upload_completion.py | 14 +++++++ 10 files changed, 128 insertions(+), 23 deletions(-) diff --git a/codecov_cli/services/commit/__init__.py b/codecov_cli/services/commit/__init__.py index c142e7bf..e23957d7 100644 --- a/codecov_cli/services/commit/__init__.py +++ b/codecov_cli/services/commit/__init__.py @@ -3,9 +3,9 @@ import typing from codecov_cli.helpers.config import CODECOV_INGEST_URL -from codecov_cli.helpers.encoder import decode_slug, encode_slug +from codecov_cli.helpers.encoder import encode_slug from codecov_cli.helpers.request import ( - get_token_header_or_fail, + get_token_header, log_warnings_and_errors_if_any, send_post_request, ) @@ -19,7 +19,7 @@ def create_commit_logic( pr: typing.Optional[str], branch: typing.Optional[str], slug: typing.Optional[str], - token: str, + token: str | None, service: typing.Optional[str], enterprise_url: typing.Optional[str] = None, fail_on_error: bool = False, @@ -61,7 +61,7 @@ def send_commit_data( branch = tokenless # type: ignore logger.info("The PR is happening in a forked repo. Using tokenless upload.") else: - headers = get_token_header_or_fail(token) + headers = get_token_header(token) data = { "branch": branch, diff --git a/codecov_cli/services/report/__init__.py b/codecov_cli/services/report/__init__.py index 0b29c9d6..06b3f563 100644 --- a/codecov_cli/services/report/__init__.py +++ b/codecov_cli/services/report/__init__.py @@ -2,14 +2,11 @@ import logging import time -import requests - from codecov_cli.helpers import request from codecov_cli.helpers.config import CODECOV_API_URL, CODECOV_INGEST_URL -from codecov_cli.helpers.encoder import decode_slug, encode_slug +from codecov_cli.helpers.encoder import encode_slug from codecov_cli.helpers.request import ( get_token_header, - get_token_header_or_fail, log_warnings_and_errors_if_any, request_result, send_post_request, @@ -24,7 +21,7 @@ def create_report_logic( code: str, slug: str, service: str, - token: str, + token: str | None, enterprise_url: str, pull_request_number: int, fail_on_error: bool = False, @@ -70,7 +67,7 @@ def create_report_results_logic( code: str, slug: str, service: str, - token: str, + token: str | None, enterprise_url: str, fail_on_error: bool = False, args: dict = None, @@ -103,7 +100,7 @@ def send_reports_result_request( data = { "cli_args": args, } - headers = get_token_header_or_fail(token) + 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/{report_code}/results" return send_post_request(url=url, data=data, headers=headers) @@ -118,7 +115,7 @@ def send_reports_result_get_request( enterprise_url, fail_on_error=False, ): - headers = get_token_header_or_fail(token) + 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/{report_code}/results" number_tries = 0 diff --git a/codecov_cli/services/upload/__init__.py b/codecov_cli/services/upload/__init__.py index ae1a22a2..45d52a23 100644 --- a/codecov_cli/services/upload/__init__.py +++ b/codecov_cli/services/upload/__init__.py @@ -56,7 +56,7 @@ def do_upload_logic( report_code: str, slug: typing.Optional[str], swift_project: typing.Optional[str], - token: str, + token: str | None, upload_file_type: str = "coverage", use_legacy_uploader: bool = False, ): diff --git a/codecov_cli/services/upload/upload_sender.py b/codecov_cli/services/upload/upload_sender.py index bfd5a07f..88aed683 100644 --- a/codecov_cli/services/upload/upload_sender.py +++ b/codecov_cli/services/upload/upload_sender.py @@ -27,7 +27,7 @@ def send_upload_data( self, upload_data: UploadCollectionResult, commit_sha: str, - token: str, + token: str | None, env_vars: typing.Dict[str, str], report_code: str, upload_file_type: str = "coverage", diff --git a/codecov_cli/services/upload_completion/__init__.py b/codecov_cli/services/upload_completion/__init__.py index b595ba7f..6d56636a 100644 --- a/codecov_cli/services/upload_completion/__init__.py +++ b/codecov_cli/services/upload_completion/__init__.py @@ -4,7 +4,7 @@ from codecov_cli.helpers.config import CODECOV_API_URL from codecov_cli.helpers.encoder import encode_slug from codecov_cli.helpers.request import ( - get_token_header, + get_token_header_or_fail, log_warnings_and_errors_if_any, send_post_request, ) @@ -22,7 +22,7 @@ def upload_completion_logic( args=None, ): encoded_slug = encode_slug(slug) - headers = get_token_header(token) + 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}/upload-complete" data = { diff --git a/tests/services/commit/test_commit_service.py b/tests/services/commit/test_commit_service.py index 31ac3c02..2c21441b 100644 --- a/tests/services/commit/test_commit_service.py +++ b/tests/services/commit/test_commit_service.py @@ -178,3 +178,33 @@ def test_commit_sender_with_forked_repo(mocker): }, headers=None, ) + + +def test_commit_without_token(mocker): + mocked_response = mocker.patch( + "codecov_cli.services.commit.send_post_request", + return_value=mocker.MagicMock(status_code=200, text="success"), + ) + + res = send_commit_data( + "commit_sha", + "parent_sha", + "1", + "branch", + "codecov::::codecov-cli", + None, + "github", + None, + None, + ) + mocked_response.assert_called_with( + url="https://api.codecov.io/upload/github/codecov::::codecov-cli/commits", + data={ + "branch": "branch", + "cli_args": None, + "commitid": "commit_sha", + "parent_commit_id": "parent_sha", + "pullid": "1", + }, + headers=None, + ) diff --git a/tests/services/empty_upload/test_empty_upload.py b/tests/services/empty_upload/test_empty_upload.py index b49d055e..4b1eaad6 100644 --- a/tests/services/empty_upload/test_empty_upload.py +++ b/tests/services/empty_upload/test_empty_upload.py @@ -1,6 +1,8 @@ import json import uuid +import click +import pytest from click.testing import CliRunner from codecov_cli.services.empty_upload import empty_upload_logic @@ -147,3 +149,16 @@ def test_empty_upload_force(mocker): assert res.error is None assert res.warnings == [] mocked_response.assert_called_once() + + +def test_empty_upload_no_token(mocker): + mocked_response = mocker.patch("codecov_cli.helpers.request.requests.post") + with pytest.raises(click.ClickException) as exp: + empty_upload_logic( + "commit_sha", "owner/repo", None, "service", None, False, False, None + ) + + assert "Codecov token not found. Please provide Codecov token with -t flag." in str( + exp.value + ) + mocked_response.assert_not_called() diff --git a/tests/services/report/test_report_results.py b/tests/services/report/test_report_results.py index 27808d23..268f56e3 100644 --- a/tests/services/report/test_report_results.py +++ b/tests/services/report/test_report_results.py @@ -108,6 +108,19 @@ def test_report_results_request_200(mocker): mocked_response.assert_called_once() +def test_report_results_request_no_token(mocker): + mocked_response = mocker.patch( + "codecov_cli.helpers.request.requests.post", + return_value=mocker.MagicMock(status_code=200), + ) + res = send_reports_result_request( + "commit_sha", "report_code", "encoded_slug", "service", None, None, None + ) + assert res.error is None + assert res.warnings == [] + mocked_response.assert_called_once() + + def test_report_results_403(mocker): mocked_response = mocker.patch( "codecov_cli.helpers.request.requests.post", @@ -127,7 +140,7 @@ def test_report_results_403(mocker): def test_get_report_results_200_completed(mocker, capsys): mocked_response = mocker.patch( - "codecov_cli.services.report.requests.get", + "codecov_cli.helpers.request.requests.get", return_value=mocker.MagicMock( status_code=200, text='{"state": "completed", "result": {"state": "failure","message": "33.33% of diff hit (target 77.77%)"}}', @@ -147,11 +160,27 @@ def test_get_report_results_200_completed(mocker, capsys): ) in output +def test_get_report_results_no_token(mocker, capsys): + mocked_response = mocker.patch( + "codecov_cli.helpers.request.requests.get", + return_value=mocker.MagicMock( + status_code=200, + text='{"state": "completed", "result": {"state": "failure","message": "33.33% of diff hit (target 77.77%)"}}', + ), + ) + res = send_reports_result_get_request( + "commit_sha", "report_code", "encoded_slug", "service", None, None + ) + assert res.error is None + assert res.warnings == [] + mocked_response.assert_called_once() + + @patch("codecov_cli.services.report.MAX_NUMBER_TRIES", 1) def test_get_report_results_200_pending(mocker, capsys): mocker.patch("codecov_cli.services.report.time.sleep") mocked_response = mocker.patch( - "codecov_cli.services.report.requests.get", + "codecov_cli.helpers.request.requests.get", return_value=mocker.MagicMock( status_code=200, text='{"state": "pending", "result": {}}' ), @@ -169,7 +198,7 @@ def test_get_report_results_200_pending(mocker, capsys): def test_get_report_results_200_error(mocker, capsys): mocked_response = mocker.patch( - "codecov_cli.services.report.requests.get", + "codecov_cli.helpers.request.requests.get", return_value=mocker.MagicMock( status_code=200, text='{"state": "error", "result": {}}' ), @@ -190,7 +219,7 @@ def test_get_report_results_200_error(mocker, capsys): def test_get_report_results_200_undefined_state(mocker, capsys): mocked_response = mocker.patch( - "codecov_cli.services.report.requests.get", + "codecov_cli.helpers.request.requests.get", return_value=mocker.MagicMock( status_code=200, text='{"state": "undefined_state", "result": {}}' ), @@ -208,7 +237,7 @@ def test_get_report_results_200_undefined_state(mocker, capsys): def test_get_report_results_401(mocker, capsys): mocked_response = mocker.patch( - "codecov_cli.services.report.requests.get", + "codecov_cli.helpers.request.requests.get", return_value=mocker.MagicMock( status_code=401, text='{"detail": "Invalid token."}' ), diff --git a/tests/services/report/test_report_service.py b/tests/services/report/test_report_service.py index ac4c222b..153e803a 100644 --- a/tests/services/report/test_report_service.py +++ b/tests/services/report/test_report_service.py @@ -9,7 +9,7 @@ def test_send_create_report_request_200(mocker): mocked_response = mocker.patch( - "codecov_cli.services.report.requests.post", + "codecov_cli.helpers.request.requests.post", return_value=mocker.MagicMock(status_code=200), ) res = send_create_report_request( @@ -27,9 +27,29 @@ def test_send_create_report_request_200(mocker): mocked_response.assert_called_once() +def test_send_create_report_request_no_token(mocker): + mocked_response = mocker.patch( + "codecov_cli.helpers.request.requests.post", + return_value=mocker.MagicMock(status_code=200), + ) + res = send_create_report_request( + "commit_sha", + "code", + "github", + None, + "owner::::repo", + "enterprise_url", + 1, + None, + ) + assert res.error is None + assert res.warnings == [] + mocked_response.assert_called_once() + + def test_send_create_report_request_403(mocker): mocked_response = mocker.patch( - "codecov_cli.services.report.requests.post", + "codecov_cli.helpers.request.requests.post", return_value=mocker.MagicMock(status_code=403, text="Permission denied"), ) res = send_create_report_request( diff --git a/tests/services/upload_completion/test_upload_completion.py b/tests/services/upload_completion/test_upload_completion.py index 6ab5ea3b..3b8aed6e 100644 --- a/tests/services/upload_completion/test_upload_completion.py +++ b/tests/services/upload_completion/test_upload_completion.py @@ -1,6 +1,8 @@ import json import uuid +import click +import pytest from click.testing import CliRunner from codecov_cli.services.upload_completion import upload_completion_logic @@ -93,6 +95,18 @@ def test_upload_completion_200(mocker): mocked_response.assert_called_once() +def test_upload_completion_no_token(mocker): + mocked_response = mocker.patch( + "codecov_cli.helpers.request.requests.post", + ) + with pytest.raises(click.ClickException) as exp: + upload_completion_logic("commit_sha", "owner/repo", None, "service", None) + assert "Codecov token not found. Please provide Codecov token with -t flag." in str( + exp.value + ) + mocked_response.assert_not_called() + + def test_upload_completion_403(mocker): mocked_response = mocker.patch( "codecov_cli.helpers.request.requests.post", From ae00a3010c9f07a785d0be6d986353fe1241f09f Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Tue, 15 Oct 2024 16:01:44 -0700 Subject: [PATCH 03/13] backwards compatability --- codecov_cli/services/commit/__init__.py | 2 +- codecov_cli/services/report/__init__.py | 5 +++-- codecov_cli/services/upload/__init__.py | 2 +- codecov_cli/services/upload/upload_sender.py | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/codecov_cli/services/commit/__init__.py b/codecov_cli/services/commit/__init__.py index e23957d7..549b791d 100644 --- a/codecov_cli/services/commit/__init__.py +++ b/codecov_cli/services/commit/__init__.py @@ -19,7 +19,7 @@ def create_commit_logic( pr: typing.Optional[str], branch: typing.Optional[str], slug: typing.Optional[str], - token: str | None, + token: typing.Union[str, None], service: typing.Optional[str], enterprise_url: typing.Optional[str] = None, fail_on_error: bool = False, diff --git a/codecov_cli/services/report/__init__.py b/codecov_cli/services/report/__init__.py index 06b3f563..13f6da50 100644 --- a/codecov_cli/services/report/__init__.py +++ b/codecov_cli/services/report/__init__.py @@ -1,6 +1,7 @@ import json import logging import time +import typing from codecov_cli.helpers import request from codecov_cli.helpers.config import CODECOV_API_URL, CODECOV_INGEST_URL @@ -21,7 +22,7 @@ def create_report_logic( code: str, slug: str, service: str, - token: str | None, + token: typing.Union[str, None], enterprise_url: str, pull_request_number: int, fail_on_error: bool = False, @@ -67,7 +68,7 @@ def create_report_results_logic( code: str, slug: str, service: str, - token: str | None, + token: typing.Union[str, None], enterprise_url: str, fail_on_error: bool = False, args: dict = None, diff --git a/codecov_cli/services/upload/__init__.py b/codecov_cli/services/upload/__init__.py index 45d52a23..762adc85 100644 --- a/codecov_cli/services/upload/__init__.py +++ b/codecov_cli/services/upload/__init__.py @@ -56,7 +56,7 @@ def do_upload_logic( report_code: str, slug: typing.Optional[str], swift_project: typing.Optional[str], - token: str | None, + token: typing.Union[str, None], upload_file_type: str = "coverage", use_legacy_uploader: bool = False, ): diff --git a/codecov_cli/services/upload/upload_sender.py b/codecov_cli/services/upload/upload_sender.py index 88aed683..a663b8b7 100644 --- a/codecov_cli/services/upload/upload_sender.py +++ b/codecov_cli/services/upload/upload_sender.py @@ -27,7 +27,7 @@ def send_upload_data( self, upload_data: UploadCollectionResult, commit_sha: str, - token: str | None, + token: typing.Union[str, None], env_vars: typing.Dict[str, str], report_code: str, upload_file_type: str = "coverage", From 72370b6782c34204a3ed238d414a77975208f257 Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Wed, 16 Oct 2024 11:09:05 -0700 Subject: [PATCH 04/13] remove pytest-mock --- requirements.txt | 12 +----------- setup.py | 1 - 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/requirements.txt b/requirements.txt index 0a4e2252..2d33a3d3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ # -# This file is autogenerated by pip-compile with Python 3.12 +# This file is autogenerated by pip-compile with Python 3.11 # by the following command: # # pip-compile setup.py @@ -28,16 +28,6 @@ idna==3.4 # rfc3986 ijson==3.2.3 # via codecov-cli (setup.py) -iniconfig==2.0.0 - # via pytest -packaging==24.1 - # via pytest -pluggy==1.5.0 - # via pytest -pytest==8.3.3 - # via pytest-mock -pytest-mock==3.14.0 - # via codecov-cli (setup.py) pyyaml==6.0.1 # via codecov-cli (setup.py) regex==2023.12.25 diff --git a/setup.py b/setup.py index 72eb5bb7..b1c59f32 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,6 @@ "tree-sitter==0.20.*", "test-results-parser==0.5.*", "regex", - "pytest-mock" ], entry_points={ "console_scripts": [ From af8960327b9d5a182bc114cb856e137d2e0fc0fc Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Wed, 16 Oct 2024 15:36:49 -0700 Subject: [PATCH 05/13] typing fixes --- codecov_cli/helpers/request.py | 4 ++-- codecov_cli/services/commit/__init__.py | 2 +- codecov_cli/services/report/__init__.py | 4 ++-- codecov_cli/services/upload/__init__.py | 2 +- codecov_cli/services/upload/upload_sender.py | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/codecov_cli/helpers/request.py b/codecov_cli/helpers/request.py index f9cf8ab1..49b96ee1 100644 --- a/codecov_cli/helpers/request.py +++ b/codecov_cli/helpers/request.py @@ -102,7 +102,7 @@ def send_get_request( return request_result(get(url=url, headers=headers, params=params)) -def get_token_header_or_fail(token: str) -> dict: +def get_token_header_or_fail(token: Optional[str]) -> dict: if token is None: raise click.ClickException( "Codecov token not found. Please provide Codecov token with -t flag." @@ -110,7 +110,7 @@ def get_token_header_or_fail(token: str) -> dict: return {"Authorization": f"token {token}"} -def get_token_header(token: str) -> Optional[dict]: +def get_token_header(token: Optional[str]) -> Optional[dict]: if token is None: return None return {"Authorization": f"token {token}"} diff --git a/codecov_cli/services/commit/__init__.py b/codecov_cli/services/commit/__init__.py index 549b791d..54518de7 100644 --- a/codecov_cli/services/commit/__init__.py +++ b/codecov_cli/services/commit/__init__.py @@ -19,7 +19,7 @@ def create_commit_logic( pr: typing.Optional[str], branch: typing.Optional[str], slug: typing.Optional[str], - token: typing.Union[str, None], + token: typing.Optional[str], service: typing.Optional[str], enterprise_url: typing.Optional[str] = None, fail_on_error: bool = False, diff --git a/codecov_cli/services/report/__init__.py b/codecov_cli/services/report/__init__.py index 13f6da50..b19a8c19 100644 --- a/codecov_cli/services/report/__init__.py +++ b/codecov_cli/services/report/__init__.py @@ -22,7 +22,7 @@ def create_report_logic( code: str, slug: str, service: str, - token: typing.Union[str, None], + token: typing.Optional[str], enterprise_url: str, pull_request_number: int, fail_on_error: bool = False, @@ -68,7 +68,7 @@ def create_report_results_logic( code: str, slug: str, service: str, - token: typing.Union[str, None], + token: typing.Optional[str], enterprise_url: str, fail_on_error: bool = False, args: dict = None, diff --git a/codecov_cli/services/upload/__init__.py b/codecov_cli/services/upload/__init__.py index 762adc85..7b3c884f 100644 --- a/codecov_cli/services/upload/__init__.py +++ b/codecov_cli/services/upload/__init__.py @@ -56,7 +56,7 @@ def do_upload_logic( report_code: str, slug: typing.Optional[str], swift_project: typing.Optional[str], - token: typing.Union[str, None], + token: typing.Optional[str], upload_file_type: str = "coverage", use_legacy_uploader: bool = False, ): diff --git a/codecov_cli/services/upload/upload_sender.py b/codecov_cli/services/upload/upload_sender.py index a663b8b7..84dc8189 100644 --- a/codecov_cli/services/upload/upload_sender.py +++ b/codecov_cli/services/upload/upload_sender.py @@ -27,7 +27,7 @@ def send_upload_data( self, upload_data: UploadCollectionResult, commit_sha: str, - token: typing.Union[str, None], + token: typing.Optional[str], env_vars: typing.Dict[str, str], report_code: str, upload_file_type: str = "coverage", From 199872e081ff928162dc42f50a8bb576272d907a Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Tue, 22 Oct 2024 15:45:10 -0500 Subject: [PATCH 06/13] fix: bump version --- codecov_cli/commands/process_test_results.py | 6 +++--- codecov_cli/helpers/request.py | 3 +-- setup.py | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/codecov_cli/commands/process_test_results.py b/codecov_cli/commands/process_test_results.py index 6d3d89f2..b95cc30c 100644 --- a/codecov_cli/commands/process_test_results.py +++ b/codecov_cli/commands/process_test_results.py @@ -103,9 +103,9 @@ def process_test_results( dir, exclude_folders, files, disable_search, report_type="test_results" ) - upload_collection_results: List[ - UploadCollectionResultFile - ] = file_finder.find_files() + upload_collection_results: List[UploadCollectionResultFile] = ( + file_finder.find_files() + ) if len(upload_collection_results) == 0: raise click.ClickException( "No JUnit XML files were found. Make sure to specify them using the --file option." diff --git a/codecov_cli/helpers/request.py b/codecov_cli/helpers/request.py index 49b96ee1..6dba11f7 100644 --- a/codecov_cli/helpers/request.py +++ b/codecov_cli/helpers/request.py @@ -52,8 +52,7 @@ def backoff_time(curr_retry): return 2 ** (curr_retry - 1) -class RetryException(Exception): - ... +class RetryException(Exception): ... def retry_request(func): diff --git a/setup.py b/setup.py index b1c59f32..11ef6de7 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="codecov-cli", - version="0.8.0", + version="0.9.0-beta", packages=find_packages(exclude=["contrib", "docs", "tests*"]), description="Codecov Command Line Interface", long_description=long_description, From b7676a4e4dcd27c29d3db7df741ec57afe5d09ff Mon Sep 17 00:00:00 2001 From: Tom Hu Date: Tue, 22 Oct 2024 15:52:20 -0500 Subject: [PATCH 07/13] fix: logs --- codecov_cli/commands/upload_process.py | 1 + setup.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/codecov_cli/commands/upload_process.py b/codecov_cli/commands/upload_process.py index 1ee77eca..0bae14ea 100644 --- a/codecov_cli/commands/upload_process.py +++ b/codecov_cli/commands/upload_process.py @@ -60,6 +60,7 @@ def upload_process( use_legacy_uploader: bool, ): args = get_cli_args(ctx) + logger.info("HI FRANDS") logger.debug( "Starting upload process", extra=dict( diff --git a/setup.py b/setup.py index 11ef6de7..eab4ec49 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="codecov-cli", - version="0.9.0-beta", + version="0.9.0-b", packages=find_packages(exclude=["contrib", "docs", "tests*"]), description="Codecov Command Line Interface", long_description=long_description, From d0eab6914a9b5462763e06ab81ffb0660bea6299 Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Wed, 6 Nov 2024 12:47:36 -0800 Subject: [PATCH 08/13] all upload endpoints can do tokenless --- codecov_cli/helpers/request.py | 9 +++++- codecov_cli/services/commit/base_picking.py | 4 +-- codecov_cli/services/empty_upload/__init__.py | 4 +-- .../services/upload_completion/__init__.py | 4 +-- tests/services/commit/test_base_picking.py | 24 +++++++++++++++ .../empty_upload/test_empty_upload.py | 29 ++++++++++++++----- .../test_upload_completion.py | 29 +++++++++++++++---- 7 files changed, 83 insertions(+), 20 deletions(-) diff --git a/codecov_cli/helpers/request.py b/codecov_cli/helpers/request.py index 6dba11f7..e5c04f8f 100644 --- a/codecov_cli/helpers/request.py +++ b/codecov_cli/helpers/request.py @@ -52,7 +52,8 @@ def backoff_time(curr_retry): return 2 ** (curr_retry - 1) -class RetryException(Exception): ... +class RetryException(Exception): + ... def retry_request(func): @@ -102,6 +103,9 @@ def send_get_request( def get_token_header_or_fail(token: Optional[str]) -> dict: + """ + Rejects requests with no Authorization token. Prevents tokenless uploads. + """ if token is None: raise click.ClickException( "Codecov token not found. Please provide Codecov token with -t flag." @@ -110,6 +114,9 @@ def get_token_header_or_fail(token: Optional[str]) -> dict: def get_token_header(token: Optional[str]) -> Optional[dict]: + """ + Allows requests with no Authorization token. + """ if token is None: return None return {"Authorization": f"token {token}"} diff --git a/codecov_cli/services/commit/base_picking.py b/codecov_cli/services/commit/base_picking.py index 20767132..7332a462 100644 --- a/codecov_cli/services/commit/base_picking.py +++ b/codecov_cli/services/commit/base_picking.py @@ -2,7 +2,7 @@ from codecov_cli.helpers.config import CODECOV_API_URL from codecov_cli.helpers.request import ( - get_token_header_or_fail, + get_token_header, log_warnings_and_errors_if_any, send_put_request, ) @@ -15,7 +15,7 @@ def base_picking_logic(base_sha, pr, slug, token, service, enterprise_url, args) "cli_args": args, "user_provided_base_sha": base_sha, } - headers = get_token_header_or_fail(token) + headers = get_token_header(token) upload_url = enterprise_url or CODECOV_API_URL url = f"{upload_url}/api/v1/{service}/{slug}/pulls/{pr}" sending_result = send_put_request(url=url, data=data, headers=headers) diff --git a/codecov_cli/services/empty_upload/__init__.py b/codecov_cli/services/empty_upload/__init__.py index 7c8b0682..587bb756 100644 --- a/codecov_cli/services/empty_upload/__init__.py +++ b/codecov_cli/services/empty_upload/__init__.py @@ -4,7 +4,7 @@ from codecov_cli.helpers.config import CODECOV_API_URL from codecov_cli.helpers.encoder import encode_slug from codecov_cli.helpers.request import ( - get_token_header_or_fail, + get_token_header, log_warnings_and_errors_if_any, send_post_request, ) @@ -23,7 +23,7 @@ def empty_upload_logic( args, ): encoded_slug = encode_slug(slug) - headers = get_token_header_or_fail(token) + 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}/empty-upload" sending_result = send_post_request( diff --git a/codecov_cli/services/upload_completion/__init__.py b/codecov_cli/services/upload_completion/__init__.py index 6d56636a..b595ba7f 100644 --- a/codecov_cli/services/upload_completion/__init__.py +++ b/codecov_cli/services/upload_completion/__init__.py @@ -4,7 +4,7 @@ from codecov_cli.helpers.config import CODECOV_API_URL from codecov_cli.helpers.encoder import encode_slug from codecov_cli.helpers.request import ( - get_token_header_or_fail, + get_token_header, log_warnings_and_errors_if_any, send_post_request, ) @@ -22,7 +22,7 @@ def upload_completion_logic( args=None, ): encoded_slug = encode_slug(slug) - headers = get_token_header_or_fail(token) + 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" data = { diff --git a/tests/services/commit/test_base_picking.py b/tests/services/commit/test_base_picking.py index 9b560a51..2c295fe2 100644 --- a/tests/services/commit/test_base_picking.py +++ b/tests/services/commit/test_base_picking.py @@ -139,3 +139,27 @@ def test_base_picking_command_error(mocker): "error", "Base picking failed: Unauthorized", ) in parse_outstreams_into_log_lines(result.output) + + +def test_base_picking_no_token(mocker): + mocked_response = mocker.patch( + "codecov_cli.services.commit.base_picking.send_put_request", + return_value=RequestResult(status_code=200, error=None, warnings=[], text=""), + ) + runner = CliRunner() + result = runner.invoke( + pr_base_picking, + [ + "--pr", + "11", + "--base-sha", + "9a6902ee94c18e8e27561ce316b16d75a02c7bc1", + "--service", + "github", + "--slug", + "owner/repo", + ], + obj=mocker.MagicMock(), # context object + ) + assert result.exit_code == 0 + mocked_response.assert_called_once() diff --git a/tests/services/empty_upload/test_empty_upload.py b/tests/services/empty_upload/test_empty_upload.py index 4b1eaad6..1e354d50 100644 --- a/tests/services/empty_upload/test_empty_upload.py +++ b/tests/services/empty_upload/test_empty_upload.py @@ -152,13 +152,28 @@ def test_empty_upload_force(mocker): def test_empty_upload_no_token(mocker): - mocked_response = mocker.patch("codecov_cli.helpers.request.requests.post") - with pytest.raises(click.ClickException) as exp: - empty_upload_logic( + res = { + "result": "All changed files are ignored. Triggering passing notifications.", + "non_ignored_files": [], + } + mocked_response = mocker.patch( + "codecov_cli.helpers.request.requests.post", + return_value=RequestResult( + status_code=200, error=None, warnings=[], text=json.dumps(res) + ), + ) + runner = CliRunner() + with runner.isolation() as outstreams: + res = empty_upload_logic( "commit_sha", "owner/repo", None, "service", None, False, False, None ) - assert "Codecov token not found. Please provide Codecov token with -t flag." in str( - exp.value - ) - mocked_response.assert_not_called() + out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) + assert out_bytes == [ + ("info", "Process Empty Upload complete"), + ("info", "All changed files are ignored. Triggering passing notifications."), + ("info", "Non ignored files []"), + ] + assert res.error is None + assert res.warnings == [] + mocked_response.assert_called_once() diff --git a/tests/services/upload_completion/test_upload_completion.py b/tests/services/upload_completion/test_upload_completion.py index 3b8aed6e..6a6d5494 100644 --- a/tests/services/upload_completion/test_upload_completion.py +++ b/tests/services/upload_completion/test_upload_completion.py @@ -96,15 +96,32 @@ def test_upload_completion_200(mocker): def test_upload_completion_no_token(mocker): + res = { + "uploads_total": 2, + "uploads_success": 2, + "uploads_processing": 0, + "uploads_error": 0, + } mocked_response = mocker.patch( "codecov_cli.helpers.request.requests.post", + return_value=RequestResult( + status_code=200, error=None, warnings=[], text=json.dumps(res) + ), ) - with pytest.raises(click.ClickException) as exp: - upload_completion_logic("commit_sha", "owner/repo", None, "service", None) - assert "Codecov token not found. Please provide Codecov token with -t flag." in str( - exp.value - ) - mocked_response.assert_not_called() + runner = CliRunner() + with runner.isolation() as outstreams: + res = upload_completion_logic("commit_sha", "owner/repo", None, "service", None) + out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) + assert out_bytes == [ + ("info", "Process Upload Completion complete"), + ( + "info", + "{'uploads_total': 2, 'uploads_success': 2, 'uploads_processing': 0, 'uploads_error': 0}", + ), + ] + assert res.error is None + assert res.warnings == [] + mocked_response.assert_called_once() def test_upload_completion_403(mocker): From 53289f820ca3f2c05994292aa77d179732d1ed5a Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Wed, 6 Nov 2024 12:56:28 -0800 Subject: [PATCH 09/13] fix failing test --- tests/services/commit/test_commit_service.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/services/commit/test_commit_service.py b/tests/services/commit/test_commit_service.py index 2c21441b..2fd5157d 100644 --- a/tests/services/commit/test_commit_service.py +++ b/tests/services/commit/test_commit_service.py @@ -186,7 +186,7 @@ def test_commit_without_token(mocker): return_value=mocker.MagicMock(status_code=200, text="success"), ) - res = send_commit_data( + send_commit_data( "commit_sha", "parent_sha", "1", @@ -198,7 +198,7 @@ def test_commit_without_token(mocker): None, ) mocked_response.assert_called_with( - url="https://api.codecov.io/upload/github/codecov::::codecov-cli/commits", + url="https://ingest.codecov.io/upload/github/codecov::::codecov-cli/commits", data={ "branch": "branch", "cli_args": None, From ea49a532b124da47dbeff2bf3859306c2f89e68f Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Thu, 7 Nov 2024 11:24:31 -0800 Subject: [PATCH 10/13] remove test log --- codecov_cli/commands/upload_process.py | 1 - 1 file changed, 1 deletion(-) diff --git a/codecov_cli/commands/upload_process.py b/codecov_cli/commands/upload_process.py index 0bae14ea..1ee77eca 100644 --- a/codecov_cli/commands/upload_process.py +++ b/codecov_cli/commands/upload_process.py @@ -60,7 +60,6 @@ def upload_process( use_legacy_uploader: bool, ): args = get_cli_args(ctx) - logger.info("HI FRANDS") logger.debug( "Starting upload process", extra=dict( From 32bc331822f92867290b5e589e014f9092c44a79 Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Thu, 7 Nov 2024 11:28:11 -0800 Subject: [PATCH 11/13] bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index eab4ec49..d905447e 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="codecov-cli", - version="0.9.0-b", + version="0.9.0", packages=find_packages(exclude=["contrib", "docs", "tests*"]), description="Codecov Command Line Interface", long_description=long_description, From 83da9aaef96e881c6fd827e8d5cf551325e62ce3 Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Thu, 7 Nov 2024 11:37:25 -0800 Subject: [PATCH 12/13] revert changes to version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index d905447e..b1c59f32 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="codecov-cli", - version="0.9.0", + version="0.8.0", packages=find_packages(exclude=["contrib", "docs", "tests*"]), description="Codecov Command Line Interface", long_description=long_description, From 10b24f7241daa58833ae186a7f9afa94e98ddac7 Mon Sep 17 00:00:00 2001 From: Nora Shapiro Date: Thu, 7 Nov 2024 11:43:36 -0800 Subject: [PATCH 13/13] linter changes --- codecov_cli/commands/process_test_results.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/codecov_cli/commands/process_test_results.py b/codecov_cli/commands/process_test_results.py index b95cc30c..6d3d89f2 100644 --- a/codecov_cli/commands/process_test_results.py +++ b/codecov_cli/commands/process_test_results.py @@ -103,9 +103,9 @@ def process_test_results( dir, exclude_folders, files, disable_search, report_type="test_results" ) - upload_collection_results: List[UploadCollectionResultFile] = ( - file_finder.find_files() - ) + upload_collection_results: List[ + UploadCollectionResultFile + ] = file_finder.find_files() if len(upload_collection_results) == 0: raise click.ClickException( "No JUnit XML files were found. Make sure to specify them using the --file option."