Skip to content

Commit ceeaddc

Browse files
fix: add log messages and tests for tokenless (#475)
1 parent 193e4be commit ceeaddc

2 files changed

Lines changed: 46 additions & 13 deletions

File tree

codecov_cli/services/commit/__init__.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,21 @@ def send_commit_data(
5353
enterprise_url,
5454
args,
5555
):
56-
# this is how the CLI receives the username of the user to whom the fork belongs
57-
# to and the branch name from the action
58-
tokenless = os.environ.get("TOKENLESS")
59-
if tokenless:
60-
headers = None # type: ignore
61-
branch = tokenless # type: ignore
62-
logger.info("The PR is happening in a forked repo. Using tokenless upload.")
56+
# Old versions of the GHA use this env var instead of the regular branch
57+
# argument to provide an unprotected branch name
58+
if tokenless := os.environ.get("TOKENLESS"):
59+
branch = tokenless
60+
61+
if branch and ":" in branch:
62+
logger.info(f"Creating a commit for an unprotected branch: {branch}")
63+
elif token is None:
64+
logger.warning(
65+
f"Branch `{branch}` is protected but no token was provided\nFor information on Codecov upload tokens, see https://docs.codecov.com/docs/codecov-tokens"
66+
)
6367
else:
64-
headers = get_token_header(token)
68+
logger.info("Using token to create a commit for protected branch `{branch}`")
69+
70+
headers = get_token_header(token)
6571

6672
data = {
6773
"branch": branch,

tests/services/commit/test_commit_service.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import json
21
import uuid
32

4-
import requests
53
from click.testing import CliRunner
6-
from requests import Response
74

85
from codecov_cli.services.commit import create_commit_logic, send_commit_data
96
from codecov_cli.types import RequestError, RequestResult, RequestResultWarning
@@ -155,12 +152,11 @@ def test_commit_sender_with_forked_repo(mocker):
155152
return_value=mocker.MagicMock(status_code=200, text="success"),
156153
)
157154

158-
mocker.patch("os.environ", dict(TOKENLESS="user_forked_repo/codecov-cli:branch"))
159155
_ = send_commit_data(
160156
"commit_sha",
161157
"parent_sha",
162158
"1",
163-
"branch",
159+
"user_forked_repo/codecov-cli:branch",
164160
"codecov::::codecov-cli",
165161
None,
166162
"github",
@@ -208,3 +204,34 @@ def test_commit_without_token(mocker):
208204
},
209205
headers=None,
210206
)
207+
208+
209+
def test_commit_sender_with_forked_repo_bad_branch(mocker):
210+
mocked_response = mocker.patch(
211+
"codecov_cli.services.commit.send_post_request",
212+
return_value=mocker.MagicMock(status_code=200, text="success"),
213+
)
214+
mocker.patch("os.environ", dict(TOKENLESS="user_forked_repo/codecov-cli:branch"))
215+
_res = send_commit_data(
216+
"commit_sha",
217+
"parent_sha",
218+
"1",
219+
"branch",
220+
"codecov::::codecov-cli",
221+
None,
222+
"github",
223+
None,
224+
None,
225+
)
226+
227+
mocked_response.assert_called_with(
228+
url="https://ingest.codecov.io/upload/github/codecov::::codecov-cli/commits",
229+
data={
230+
"branch": "user_forked_repo/codecov-cli:branch",
231+
"cli_args": None,
232+
"commitid": "commit_sha",
233+
"parent_commit_id": "parent_sha",
234+
"pullid": "1",
235+
},
236+
headers=None,
237+
)

0 commit comments

Comments
 (0)