Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 992826d

Browse files
authored
🐛 Use config-set token as CLI default fallback (#464)
Just like in the old days... Before this patch, the v4-related rewrite of the Codecov uploader CLI lost the ability to use the token set in the config file [[1]]. This change fixes the regression recovering the original behavior. The bug has been discovered during the re-configuration of ``codecov/codecov-action`` in ``pytest-dev/pytest``. We use the clear-text token to improve stability of uploads in PRs from forks so it makes sense to keep it in the config rather than in the GitHub Actions CI/CD definition file. [1]: https://docs.codecov.com/docs/codecovyml-reference#codecovtoken
1 parent 65a32bc commit 992826d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

codecov_cli/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ def cli(
5858
ctx.obj["codecov_yaml"] = load_cli_config(codecov_yml_path)
5959
if ctx.obj["codecov_yaml"] is None:
6060
logger.debug("No codecov_yaml found")
61+
elif (token := ctx.obj["codecov_yaml"].get("codecov", {}).get("token")) is not None:
62+
ctx.default_map = {ctx.invoked_subcommand: {"token": token}}
6163
ctx.obj["enterprise_url"] = enterprise_url
6264

6365

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Tests ensuring that an env-provided token can be found."""
2+
3+
from pathlib import Path
4+
from textwrap import dedent as _dedent_text_block
5+
6+
from click.testing import CliRunner
7+
from pytest import MonkeyPatch
8+
from pytest_mock import MockerFixture
9+
10+
from codecov_cli.commands import upload
11+
from codecov_cli.main import cli
12+
13+
14+
def test_no_cli_token_config_fallback(
15+
mocker: MockerFixture,
16+
monkeypatch: MonkeyPatch,
17+
tmp_path: Path,
18+
) -> None:
19+
"""Test that a config-stored token is used with no CLI argument."""
20+
# NOTE: The pytest's `caplog` fixture is not used in this test as it
21+
# NOTE: doesn't play well with Click's testing CLI runner, and does
22+
# NOTE: not capture any log entries for mysterious reasons.
23+
#
24+
# Refs:
25+
# * https://github.com/pallets/click/issues/2573#issuecomment-1649773563
26+
# * https://github.com/pallets/click/issues/1763#issuecomment-767687608
27+
(tmp_path / ".codecov.yml").write_text(
28+
_dedent_text_block(
29+
"""
30+
---
31+
32+
codecov:
33+
token: sentinel-value
34+
35+
...
36+
"""
37+
)
38+
)
39+
monkeypatch.chdir(tmp_path)
40+
41+
mocker.patch.object(upload, "do_upload_logic")
42+
do_upload_cmd_spy = mocker.spy(upload, "do_upload_logic")
43+
44+
CliRunner().invoke(cli, ["do-upload", "--commit-sha=deadbeef"], obj={})
45+
46+
assert do_upload_cmd_spy.call_args[-1]["token"] == "sentinel-value"

0 commit comments

Comments
 (0)