From d62c9d87a322c0fed92d236f66f8dee552fe57e5 Mon Sep 17 00:00:00 2001 From: joseph-sentry Date: Tue, 3 Dec 2024 14:05:39 -0500 Subject: [PATCH] fix: add log messages and tests for tokenless --- codecov_cli/services/commit/__init__.py | 22 +++++++----- tests/services/commit/test_commit_service.py | 37 +++++++++++++++++--- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/codecov_cli/services/commit/__init__.py b/codecov_cli/services/commit/__init__.py index 54518de7..a872e1d1 100644 --- a/codecov_cli/services/commit/__init__.py +++ b/codecov_cli/services/commit/__init__.py @@ -53,15 +53,21 @@ def send_commit_data( 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 - tokenless = os.environ.get("TOKENLESS") - if tokenless: - headers = None # type: ignore - branch = tokenless # type: ignore - logger.info("The PR is happening in a forked repo. Using tokenless upload.") + # Old versions of the GHA use this env var instead of the regular branch + # argument to provide an unprotected branch name + if tokenless := os.environ.get("TOKENLESS"): + branch = tokenless + + if branch and ":" in branch: + logger.info(f"Creating a commit for an unprotected branch: {branch}") + elif token is None: + logger.warning( + f"Branch `{branch}` is protected but no token was provided\nFor information on Codecov upload tokens, see https://docs.codecov.com/docs/codecov-tokens" + ) else: - headers = get_token_header(token) + logger.info("Using token to create a commit for protected branch `{branch}`") + + headers = get_token_header(token) data = { "branch": branch, diff --git a/tests/services/commit/test_commit_service.py b/tests/services/commit/test_commit_service.py index 90af6ddf..362cb48e 100644 --- a/tests/services/commit/test_commit_service.py +++ b/tests/services/commit/test_commit_service.py @@ -1,9 +1,6 @@ -import json import uuid -import requests from click.testing import CliRunner -from requests import Response from codecov_cli.services.commit import create_commit_logic, send_commit_data from codecov_cli.types import RequestError, RequestResult, RequestResultWarning @@ -155,12 +152,11 @@ def test_commit_sender_with_forked_repo(mocker): return_value=mocker.MagicMock(status_code=200, text="success"), ) - mocker.patch("os.environ", dict(TOKENLESS="user_forked_repo/codecov-cli:branch")) _ = send_commit_data( "commit_sha", "parent_sha", "1", - "branch", + "user_forked_repo/codecov-cli:branch", "codecov::::codecov-cli", None, "github", @@ -208,3 +204,34 @@ def test_commit_without_token(mocker): }, headers=None, ) + + +def test_commit_sender_with_forked_repo_bad_branch(mocker): + mocked_response = mocker.patch( + "codecov_cli.services.commit.send_post_request", + return_value=mocker.MagicMock(status_code=200, text="success"), + ) + mocker.patch("os.environ", dict(TOKENLESS="user_forked_repo/codecov-cli:branch")) + _res = send_commit_data( + "commit_sha", + "parent_sha", + "1", + "branch", + "codecov::::codecov-cli", + None, + "github", + None, + None, + ) + + mocked_response.assert_called_with( + url="https://ingest.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", + }, + headers=None, + )