Skip to content

Commit cd3edef

Browse files
authored
feature: validate the token user (#45)
1 parent 5f2cab3 commit cd3edef

File tree

5 files changed

+58
-25
lines changed

5 files changed

+58
-25
lines changed

codecov/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class GithubBaseException(CoreBaseException):
1414
pass
1515

1616

17+
class CannotGetUser(GithubBaseException):
18+
pass
19+
20+
1721
class CannotGetBranch(GithubBaseException):
1822
pass
1923

@@ -34,6 +38,10 @@ class NotFound(ApiError):
3438
pass
3539

3640

41+
class Unauthorized(ApiError):
42+
pass
43+
44+
3745
class Forbidden(ApiError):
3846
pass
3947

codecov/github.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
ApiError,
55
CannotGetBranch,
66
CannotGetPullRequest,
7+
CannotGetUser,
78
CannotPostComment,
89
Conflict,
910
Forbidden,
1011
NotFound,
12+
Unauthorized,
1113
ValidationFailed,
1214
)
1315
from codecov.github_client import GitHubClient
1416
from codecov.groups import Annotation
1517
from codecov.log import log
1618

17-
GITHUB_CODECOV_LOGIN = 'CI-codecov[bot]'
1819
COMMIT_MESSAGE = 'Update annotations data'
1920

2021

@@ -41,7 +42,6 @@ def __init__( # pylint: disable=too-many-arguments, too-many-positional-argumen
4142
self.user: User = self._init_user()
4243
self.pr_number, self.base_ref = self._init_pr_number(pr_number=pr_number, ref=ref)
4344
self.pr_diff: str = self._init_pr_diff()
44-
# TODO: Validate the user and email if annotations are not empty. We need these for committing to the branch
4545

4646
def _init_user(self) -> User:
4747
log.info('Getting user details.')
@@ -52,13 +52,12 @@ def _init_user(self) -> User:
5252
email=response.email or f'{response.id}+{response.login}@users.noreply.github.com',
5353
login=response.login,
5454
)
55-
except Forbidden:
56-
# The GitHub actions user cannot access its own details
57-
# and I'm not sure there's a way to see that we're using
58-
# the GitHub actions user except noting that it fails
59-
log.warning('Cannot get user details. Using default user.')
60-
# TODO: Abort if we can't get the user details
61-
return User(name=GITHUB_CODECOV_LOGIN, email='', login=GITHUB_CODECOV_LOGIN)
55+
except Unauthorized as exc:
56+
log.error('Unauthorized access to user details. Invalid token.')
57+
raise CannotGetUser from exc
58+
except Forbidden as exc:
59+
log.error('Cannot get user details.')
60+
raise CannotGetUser from exc
6261

6362
def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None) -> tuple[int, str]:
6463
if pr_number:

codecov/github_client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44

55
import httpx
66

7-
from codecov.exceptions import ApiError, ConfigurationException, Conflict, Forbidden, NotFound, ValidationFailed
7+
from codecov.exceptions import (
8+
ApiError,
9+
ConfigurationException,
10+
Conflict,
11+
Forbidden,
12+
NotFound,
13+
Unauthorized,
14+
ValidationFailed,
15+
)
816
from codecov.log import log
917

1018
TIMEOUT = 60
@@ -110,6 +118,8 @@ def _http(self, method: str, path: str, *, use_bytes: bool = False, use_text: bo
110118
except httpx.HTTPStatusError as exc:
111119
exc_cls = ApiError
112120
match exc.response.status_code:
121+
case 401:
122+
exc_cls = Unauthorized
113123
case 403:
114124
exc_cls = Forbidden
115125
case 404:

run.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import sys
2+
3+
from codecov.exceptions import CoreBaseException
14
from codecov.main import Main
25

36

47
def main_call(name):
58
if name == '__main__':
6-
Main().run()
9+
try:
10+
Main().run()
11+
except CoreBaseException:
12+
sys.exit(1)
713

814

915
main_call(name=__name__)

tests/test_github.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
import pytest
99

10-
from codecov.exceptions import CannotGetBranch, CannotGetPullRequest, CannotPostComment
11-
from codecov.github import COMMIT_MESSAGE, GITHUB_CODECOV_LOGIN, Github, User
10+
from codecov.exceptions import CannotGetBranch, CannotGetPullRequest, CannotGetUser, CannotPostComment
11+
from codecov.github import COMMIT_MESSAGE, Github, User
1212
from codecov.groups import Annotation, AnnotationEncoder
1313

1414
TEST_DATA_PR_DIFF = 'diff --git a/file.py b/file.py\nindex 1234567..abcdefg 100644\n--- a/file.py\n+++ b/file.py\n@@ -1,2 +1,2 @@\n-foo\n+bar\n-baz\n+qux\n'
@@ -54,19 +54,29 @@ def test_init_user_login(
5454
test_config,
5555
gh_client,
5656
):
57+
session.register('GET', '/user')(status_code=401)
58+
with pytest.raises(CannotGetUser):
59+
Github(
60+
client=gh_client,
61+
repository=test_config.GITHUB_REPOSITORY,
62+
pr_number=test_config.GITHUB_PR_NUMBER,
63+
ref=test_config.GITHUB_REF,
64+
annotations_data_branch=test_config.ANNOTATIONS_DATA_BRANCH,
65+
)
66+
gh_init_pr_number_mock.assert_not_called()
67+
gh_init_pr_diff_mock.assert_not_called()
68+
5769
session.register('GET', '/user')(status_code=403)
58-
gh = Github(
59-
client=gh_client,
60-
repository=test_config.GITHUB_REPOSITORY,
61-
pr_number=test_config.GITHUB_PR_NUMBER,
62-
ref=test_config.GITHUB_REF,
63-
annotations_data_branch=test_config.ANNOTATIONS_DATA_BRANCH,
64-
)
65-
assert gh.user == User(name=GITHUB_CODECOV_LOGIN, email='', login=GITHUB_CODECOV_LOGIN)
66-
gh_init_pr_number_mock.assert_called_once()
67-
gh_init_pr_diff_mock.assert_called_once()
68-
gh_init_pr_number_mock.reset_mock()
69-
gh_init_pr_diff_mock.reset_mock()
70+
with pytest.raises(CannotGetUser):
71+
Github(
72+
client=gh_client,
73+
repository=test_config.GITHUB_REPOSITORY,
74+
pr_number=test_config.GITHUB_PR_NUMBER,
75+
ref=test_config.GITHUB_REF,
76+
annotations_data_branch=test_config.ANNOTATIONS_DATA_BRANCH,
77+
)
78+
gh_init_pr_number_mock.assert_not_called()
79+
gh_init_pr_diff_mock.assert_not_called()
7080

7181
session.register('GET', '/user')(json={'login': 'foo', 'id': 123, 'name': 'bar', 'email': 'baz'})
7282
gh = Github(

0 commit comments

Comments
 (0)