Skip to content

Commit

Permalink
fix: add log messages and tests for tokenless
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-sentry committed Dec 3, 2024
1 parent 193e4be commit d62c9d8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
22 changes: 14 additions & 8 deletions codecov_cli/services/commit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
37 changes: 32 additions & 5 deletions tests/services/commit/test_commit_service.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
)

0 comments on commit d62c9d8

Please sign in to comment.