diff --git a/codecov_cli/commands/report.py b/codecov_cli/commands/report.py index 1e57bb29..bc617de2 100644 --- a/codecov_cli/commands/report.py +++ b/codecov_cli/commands/report.py @@ -13,15 +13,6 @@ @click.option( "--code", help="The code of the report. If unsure, leave default", default="default" ) -@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, -) @global_options @click.pass_context def create_report( @@ -32,7 +23,6 @@ def create_report( git_service: str, token: str, fail_on_error: bool, - pull_request_number: int, ): enterprise_url = ctx.obj.get("enterprise_url") logger.debug( @@ -55,7 +45,6 @@ def create_report( git_service, token, enterprise_url, - pull_request_number, fail_on_error, ) if not res.error: diff --git a/codecov_cli/helpers/request.py b/codecov_cli/helpers/request.py index d356eed8..74e7a359 100644 --- a/codecov_cli/helpers/request.py +++ b/codecov_cli/helpers/request.py @@ -7,6 +7,7 @@ from codecov_cli import __version__ from codecov_cli.types import RequestError, RequestResult +import os logger = logging.getLogger("codecovcli") @@ -95,6 +96,15 @@ def get_token_header_or_fail(token: str) -> dict: return {"Authorization": f"token {token}"} +def get_auth_header(token): + tokenless = os.environ.get("TOKENLESS", None) + if tokenless: + headers = {"X-Tokenless": tokenless} + else: + headers = get_token_header_or_fail(token) + return headers + + @retry_request def send_put_request( url: str, diff --git a/codecov_cli/services/commit/__init__.py b/codecov_cli/services/commit/__init__.py index 2c8cbdd4..98f97924 100644 --- a/codecov_cli/services/commit/__init__.py +++ b/codecov_cli/services/commit/__init__.py @@ -1,11 +1,11 @@ import logging import typing +import os from codecov_cli.helpers.config import CODECOV_API_URL -from codecov_cli.helpers.encoder import decode_slug, encode_slug -from codecov_cli.helpers.git import get_pull, is_fork_pr +from codecov_cli.helpers.encoder import encode_slug from codecov_cli.helpers.request import ( - get_token_header_or_fail, + get_auth_header, log_warnings_and_errors_if_any, send_post_request, ) @@ -43,15 +43,7 @@ def create_commit_logic( def send_commit_data( commit_sha, parent_sha, pr, branch, slug, token, service, enterprise_url ): - decoded_slug = decode_slug(slug) - pull_dict = get_pull(service, decoded_slug, pr) if not token else None - if is_fork_pr(pull_dict): - headers = {"X-Tokenless": pull_dict["head"]["slug"], "X-Tokenless-PR": pr} - branch = pull_dict["head"]["slug"] + ":" + branch - logger.info("The PR is happening in a forked repo. Using tokenless upload.") - else: - headers = get_token_header_or_fail(token) - + headers = get_auth_header(token) data = { "commitid": commit_sha, "parent_commit_id": parent_sha, diff --git a/codecov_cli/services/report/__init__.py b/codecov_cli/services/report/__init__.py index 1b3cf16a..edae528a 100644 --- a/codecov_cli/services/report/__init__.py +++ b/codecov_cli/services/report/__init__.py @@ -9,6 +9,7 @@ from codecov_cli.helpers.encoder import decode_slug, encode_slug from codecov_cli.helpers.git import get_pull, is_fork_pr from codecov_cli.helpers.request import ( + get_auth_header, get_token_header_or_fail, log_warnings_and_errors_if_any, request_result, @@ -26,7 +27,6 @@ def create_report_logic( service: str, token: str, enterprise_url: str, - pull_request_number: int, fail_on_error: bool = False, ): encoded_slug = encode_slug(slug) @@ -37,27 +37,16 @@ def create_report_logic( token, encoded_slug, enterprise_url, - pull_request_number, ) 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 ): data = {"code": code} - decoded_slug = decode_slug(encoded_slug) - pull_dict = ( - get_pull(service, decoded_slug, pull_request_number) if not token else None - ) - if is_fork_pr(pull_dict): - headers = { - "X-Tokenless": pull_dict["head"]["slug"], - "X-Tokenless-PR": pull_request_number, - } - else: - headers = get_token_header_or_fail(token) + headers = get_auth_header(token) upload_url = enterprise_url or CODECOV_API_URL url = f"{upload_url}/upload/{service}/{encoded_slug}/commits/{commit_sha}/reports" return send_post_request(url=url, headers=headers, data=data) diff --git a/codecov_cli/services/upload/upload_sender.py b/codecov_cli/services/upload/upload_sender.py index 3283f8e9..56a07dd9 100644 --- a/codecov_cli/services/upload/upload_sender.py +++ b/codecov_cli/services/upload/upload_sender.py @@ -10,7 +10,7 @@ from codecov_cli.helpers.encoder import encode_slug from codecov_cli.helpers.git import get_pull, is_fork_pr from codecov_cli.helpers.request import ( - get_token_header_or_fail, + get_auth_header, send_post_request, send_put_request, ) @@ -54,18 +54,7 @@ def send_upload_data( "ci_service": ci_service, } - # Data to upload to Codecov - pull_dict = ( - get_pull(git_service, slug, pull_request_number) if not token else None - ) - - if is_fork_pr(pull_dict): - headers = { - "X-Tokenless": pull_dict["head"]["slug"], - "X-Tokenless-PR": pull_request_number, - } - else: - headers = get_token_header_or_fail(token) + headers = get_auth_header(token) encoded_slug = encode_slug(slug) upload_url = enterprise_url or CODECOV_API_URL url, data = self.get_url_and_possibly_update_data( @@ -100,7 +89,6 @@ def send_upload_data( extra=dict(extra_log_attributes=dict(response=resp_json_obj)), ) put_url = resp_json_obj["raw_upload_location"] - logger.debug("Sending upload to storage") resp_from_storage = send_put_request(put_url, data=reports_payload) return resp_from_storage diff --git a/tests/helpers/test_upload_sender.py b/tests/helpers/test_upload_sender.py index 054974fc..cc8d31b6 100644 --- a/tests/helpers/test_upload_sender.py +++ b/tests/helpers/test_upload_sender.py @@ -5,6 +5,7 @@ import pytest import responses from responses import matchers +import os from codecov_cli import __version__ as codecov_cli_version from codecov_cli.helpers.encoder import encode_slug @@ -231,19 +232,13 @@ def test_upload_sender_post_called_with_right_parameters_tokenless( mocked_storage_server, mocker, ): - headers = {"X-Tokenless": "user-forked/repo", "X-Tokenless-PR": "pr"} - mock_get_pull = mocker.patch( - "codecov_cli.services.upload.upload_sender.get_pull", - return_value={ - "head": {"slug": "user-forked/repo"}, - "base": {"slug": "org/repo"}, - }, - ) + headers = {"X-Tokenless": "user:branch"} mocked_legacy_upload_endpoint.match = [ matchers.json_params_matcher(request_data), matchers.header_matcher(headers), ] + os.environ["TOKENLESS"] = "user:branch" sending_result = UploadSender().send_upload_data( upload_collection, random_sha, None, **named_upload_data ) @@ -263,7 +258,6 @@ def test_upload_sender_post_called_with_right_parameters_tokenless( assert ( post_req_made.headers.items() >= headers.items() ) # test dict is a subset of the other - mock_get_pull.assert_called() def test_upload_sender_put_called_with_right_parameters( self, mocked_responses, mocked_legacy_upload_endpoint, mocked_storage_server diff --git a/tests/services/commit/test_commit_service.py b/tests/services/commit/test_commit_service.py index 2b64e8ee..66326ea5 100644 --- a/tests/services/commit/test_commit_service.py +++ b/tests/services/commit/test_commit_service.py @@ -1,4 +1,5 @@ import json +import os import uuid import requests @@ -177,6 +178,8 @@ def mock_request(*args, headers={}, **kwargs): "get", side_effect=mock_request, ) + + os.environ["TOKENLESS"] = "user:branch" res = send_commit_data( "commit_sha", "parent_sha", @@ -193,7 +196,7 @@ def mock_request(*args, headers={}, **kwargs): "commitid": "commit_sha", "parent_commit_id": "parent_sha", "pullid": "1", - "branch": "user_forked_repo/codecov-cli:branch", + "branch": "branch", }, - headers={"X-Tokenless": "user_forked_repo/codecov-cli", "X-Tokenless-PR": "1"}, + headers={"X-Tokenless": "user:branch"}, ) diff --git a/tests/services/report/test_report_service.py b/tests/services/report/test_report_service.py index e9987abe..ba2fee5b 100644 --- a/tests/services/report/test_report_service.py +++ b/tests/services/report/test_report_service.py @@ -1,5 +1,7 @@ +import os import uuid + from click.testing import CliRunner from codecov_cli.services.report import create_report_logic, send_create_report_request @@ -19,14 +21,13 @@ def test_send_create_report_request_200(mocker): uuid.uuid4(), "owner::::repo", "enterprise_url", - 1, ) assert res.error is None assert res.warnings == [] mocked_response.assert_called_once() -def test_send_create_report_request_200_tokneless(mocker): +def test_send_create_report_request_200_tokenless(mocker): mocked_response = mocker.patch( "codecov_cli.services.report.send_post_request", return_value=RequestResult( @@ -37,13 +38,7 @@ def test_send_create_report_request_200_tokneless(mocker): ), ) - mocked_get_pull = mocker.patch( - "codecov_cli.services.report.get_pull", - return_value={ - "head": {"slug": "user-forked/repo"}, - "base": {"slug": "org/repo"}, - }, - ) + os.environ["TOKENLESS"] = "user:branch" res = send_create_report_request( "commit_sha", "code", @@ -51,16 +46,16 @@ def test_send_create_report_request_200_tokneless(mocker): None, "owner::::repo", "enterprise_url", - 1, ) assert res.error is None assert res.warnings == [] mocked_response.assert_called_with( url=f"enterprise_url/upload/github/owner::::repo/commits/commit_sha/reports", - headers={"X-Tokenless": "user-forked/repo", "X-Tokenless-PR": 1}, + headers={ + "X-Tokenless": "user:branch", + }, data={"code": "code"}, ) - mocked_get_pull.assert_called() def test_send_create_report_request_403(mocker): @@ -69,7 +64,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 ) assert res.error == RequestError( code="HTTP Error 403", @@ -98,7 +93,6 @@ def test_create_report_command_with_warnings(mocker): service="github", token="token", enterprise_url=None, - pull_request_number=1, ) out_bytes = parse_outstreams_into_log_lines(outstreams[0].getvalue()) @@ -114,7 +108,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 ) @@ -140,7 +134,6 @@ def test_create_report_command_with_error(mocker): slug="owner/repo", service="github", token="token", - pull_request_number=1, enterprise_url="enterprise_url", ) @@ -160,5 +153,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" )