Skip to content

Commit 7a9f863

Browse files
authored
chore: fix log messages (#48)
1 parent f32cad7 commit 7a9f863

File tree

8 files changed

+101
-52
lines changed

8 files changed

+101
-52
lines changed

codecov/badge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def get_badge_color(
2727

2828
def get_static_badge_url(label: str, message: str, color: str) -> str:
2929
if not color or not message:
30-
log.error('color and message are required')
30+
log.error('Both "color" and "message" are required to generate the badge URL.')
3131
raise ValueError
3232
code = '-'.join(e.replace('_', '__').replace('-', '--') for e in (label, message, color) if e)
3333
return 'https://img.shields.io/badge/' + urllib.parse.quote(f'{code}.svg')

codecov/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ def clean_annotation_type(cls, value: str) -> AnnotationType:
104104
def clean_github_pr_number(cls, value: str) -> int:
105105
return int(value)
106106

107+
@classmethod
108+
def clean_max_files_in_comment(cls, value: str) -> int:
109+
return int(value)
110+
107111
@classmethod
108112
def clean_coverage_path(cls, value: str) -> pathlib.Path:
109113
return path_below(value)

codecov/coverage/base.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@ class FileDiffCoverage:
6464
# the statements (so it includes comments, blank lines, etc.)
6565
added_lines: list[int]
6666

67-
# TODO: Remove this property and use `missing_statements` instead
68-
# for backward compatibility
69-
@property
70-
def violation_lines(self) -> list[int]:
71-
return self.missing_statements
72-
7367

7468
@dataclasses.dataclass
7569
class DiffCoverage:
@@ -91,7 +85,7 @@ def get_coverage_info(self, coverage_path: pathlib.Path) -> Coverage:
9185
with coverage_path.open() as coverage_data:
9286
json_coverage = json.loads(coverage_data.read())
9387
except FileNotFoundError as exc:
94-
log.error('Coverage report file not found: %s', coverage_path)
88+
log.error('Coverage report file not found at the specified location: %s', coverage_path)
9589
raise ConfigurationException from exc
9690
except json.JSONDecodeError as exc:
9791
log.error('Invalid JSON format in coverage report file: %s', coverage_path)

codecov/github.py

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ def _init_user(self) -> User:
5353
login=response.login,
5454
)
5555
except Unauthorized as exc:
56-
log.error('Unauthorized access to user details. Invalid token.')
56+
log.error('Authentication failed. The provided token is invalid. Please verify the token.')
5757
raise CannotGetUser from exc
5858
except Forbidden as exc:
59-
log.error('Cannot get user details.')
59+
log.error(
60+
'Insufficient permissions. Unable to retrieve user details with the provided token. Please verify the token permissions and try again.',
61+
)
6062
raise CannotGetUser from exc
6163

6264
def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None) -> tuple[int, str]:
@@ -70,10 +72,18 @@ def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None)
7072

7173
return pull_request.number, pull_request.head.ref
7274
except Forbidden as exc:
73-
log.error('Forbidden access to pull request #%d.', pr_number)
75+
log.error(
76+
'Forbidden access to pull request #%d. Insufficient permissions to retrieve details. Please verify the token permissions and try again.',
77+
pr_number,
78+
)
79+
7480
raise CannotGetPullRequest from exc
7581
except NotFound as exc:
76-
log.error('Pull request #%d does not exist or not in open state.', pr_number)
82+
log.error(
83+
'Pull request #%d could not be found or is not in an open state. Please verify the pull request status.',
84+
pr_number,
85+
)
86+
7787
raise CannotGetPullRequest from exc
7888

7989
# If we're not on a PR, we need to find the PR number from the branch name
@@ -84,16 +94,26 @@ def _init_pr_number(self, pr_number: int | None = None, ref: str | None = None)
8494
for pull_request in pull_requests:
8595
if pull_request.head.ref == ref:
8696
return pull_request.number, pull_request.head.ref
87-
log.debug('No open pull request found for branch %s.', ref)
97+
log.debug(
98+
'No open pull request found for branch %s. Please ensure the branch has an active pull request.',
99+
ref,
100+
)
101+
88102
raise NotFound
89103
except Forbidden as exc:
90-
log.error('Forbidden access to pull requests for branch %s.', ref)
104+
log.error(
105+
'Forbidden access to pull requests created for branch %s. Insufficient permissions to view pull request details.',
106+
ref,
107+
)
91108
raise CannotGetPullRequest from exc
92109
except NotFound as exc:
93-
log.error('Checked the recent updated 100 PRs, No open pull request found for branch %s.', ref)
110+
log.error(
111+
'Checked the 100 most recent PRs in the repository, but no open pull request found for branch %s.',
112+
ref,
113+
)
94114
raise CannotGetPullRequest from exc
95115

96-
log.error('No pull request number or branch reference provided.')
116+
log.error('Pull request number or branch reference missing.')
97117
raise CannotGetPullRequest
98118

99119
def _init_pr_diff(self) -> str:
@@ -105,18 +125,26 @@ def _init_pr_diff(self) -> str:
105125
.get(use_text=True, headers={'Accept': 'application/vnd.github.v3.diff'})
106126
)
107127
except Forbidden as exc:
108-
log.error('Forbidden access to pull request #%d diff.', self.pr_number)
128+
log.error(
129+
'Insufficient permissions to retrieve the diff of pull request #%d. Please verify the token permissions and try again.',
130+
self.pr_number,
131+
)
109132
raise CannotGetPullRequest from exc
110133
except NotFound as exc:
111-
log.error('Pull request #%d does not exist or not in open state.', self.pr_number)
134+
log.error(
135+
'Pull request #%d does not exist or is not in an open state. Please ensure the branch has an active pull request.',
136+
self.pr_number,
137+
)
112138
raise CannotGetPullRequest from exc
113139

114140
return pull_request_diff
115141

116142
def post_comment(self, contents: str, marker: str) -> None:
117143
log.info('Posting comment on pull request #%d.', self.pr_number)
118144
if len(contents) > 65536:
119-
log.error('Comment exceeds allowed size(65536)')
145+
log.error(
146+
'Comment exceeds the 65536 character limit (GitHub limitation). Reduce the number of files to be reported in the comment using "MAX_FILES_IN_COMMENT" and try again.'
147+
)
120148
raise CannotPostComment
121149

122150
# Pull request review comments are comments made on a portion of the unified diff during a pull request review.
@@ -130,17 +158,26 @@ def post_comment(self, contents: str, marker: str) -> None:
130158
comments_path(comment.id).patch(body=contents)
131159
return
132160
except Forbidden as exc:
133-
log.error('Forbidden access to update comment.')
161+
log.error(
162+
'Insufficient permissions to update the comment on pull request #%d. Please verify the token permissions and try again.'
163+
)
134164
raise CannotPostComment from exc
135165
except ApiError as exc:
136-
log.error('Unknown Api error while updating comment.')
166+
log.error(
167+
'Error occurred while updating the comment on pull request #%d. Details: %s',
168+
self.pr_number,
169+
str(exc),
170+
)
137171
raise CannotPostComment from exc
138172

139173
log.info('Adding new comment on pull request')
140174
try:
141175
issue_comments_path.post(body=contents)
142176
except Forbidden as exc:
143-
log.error('Forbidden access to post comment.')
177+
log.error(
178+
'Insufficient permissions to post a comment on pull request #%d. Please check the token permissions and try again.',
179+
self.pr_number,
180+
)
144181
raise CannotPostComment from exc
145182

146183
def write_annotations_to_branch(self, annotations: list[Annotation]) -> None:
@@ -155,10 +192,16 @@ def write_annotations_to_branch(self, annotations: list[Annotation]) -> None:
155192
log.debug('Branch "%s/%s" is protected.', self.repository, self.annotations_data_branch)
156193
raise NotFound
157194
except Forbidden as exc:
158-
log.error('Forbidden access to branch "%s/%s".', self.repository, self.annotations_data_branch)
195+
log.error(
196+
'Insufficient permissions to write annotations to the branch "%s/%s". Please verify the token permissions and ensure it has content read and write access.',
197+
self.repository,
198+
self.annotations_data_branch,
199+
)
159200
raise CannotGetBranch from exc
160201
except NotFound as exc:
161-
log.error('Branch "%s/%s" does not exist.', self.repository, self.annotations_data_branch)
202+
log.error(
203+
'Branch "%s/%s" either does not exist or is protected.', self.repository, self.annotations_data_branch
204+
)
162205
raise CannotGetBranch from exc
163206

164207
log.info('Writing annotations to branch.')
@@ -175,7 +218,11 @@ def write_annotations_to_branch(self, annotations: list[Annotation]) -> None:
175218
self.annotations_data_branch,
176219
)
177220
except Forbidden as exc:
178-
log.error('Forbidden access to branch "%s/%s".', self.repository, self.annotations_data_branch)
221+
log.error(
222+
'Insufficient permissions to write annotations to the branch "%s/%s". Please verify the token permissions and ensure it has content read and write access.',
223+
self.repository,
224+
self.annotations_data_branch,
225+
)
179226
raise CannotGetBranch from exc
180227

181228
try:
@@ -192,14 +239,29 @@ def write_annotations_to_branch(self, annotations: list[Annotation]) -> None:
192239
content=encoded_content,
193240
)
194241
except NotFound as exc:
195-
log.error('Branch "%s/%s" does not exist.', self.repository, self.annotations_data_branch)
242+
log.error(
243+
'Branch "%s/%s" either does not exist or is protected.', self.repository, self.annotations_data_branch
244+
)
196245
raise CannotGetBranch from exc
197246
except Forbidden as exc:
198-
log.error('Forbidden access to branch "%s/%s".', self.repository, self.annotations_data_branch)
247+
log.error(
248+
'Insufficient permissions to write annotations to the branch "%s/%s". Please verify the token permissions and ensure it has content read and write access.',
249+
self.repository,
250+
self.annotations_data_branch,
251+
)
199252
raise CannotGetBranch from exc
200253
except Conflict as exc:
201-
log.error('Conflict writing to branch "%s/%s".', self.repository, self.annotations_data_branch)
254+
log.error(
255+
'Conflict while adding #%s pull request annotation to branch "%s/%s".',
256+
self.pr_number,
257+
self.repository,
258+
self.annotations_data_branch,
259+
)
202260
raise CannotGetBranch from exc
203261
except ValidationFailed as exc:
204-
log.error('Validation failed on committer name or email.')
262+
log.error(
263+
'Validation failed for committer name or email, or the endpoint was spammed while writing annotation to branch "%s/%s".',
264+
self.repository,
265+
self.annotations_data_branch,
266+
)
205267
raise CannotGetBranch from exc

codecov/github_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ def _init_session(self) -> httpx.Client:
8181
headers={'Authorization': f'token {self.token}'},
8282
)
8383
if not session:
84-
log.error('GitHub client session could not be created. Invalid token configuration.')
84+
log.error(
85+
'Failed to create GitHub client session. This might be due to an internal configuration or code issue.'
86+
)
8587
raise ConfigurationException
8688
return session
8789

codecov/main.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def _init_log(self):
3030

3131
def _init_required(self):
3232
if self.config.SKIP_COVERAGE and not self.config.ANNOTATE_MISSING_LINES:
33-
log.error('Nothing to do since both SKIP_COVERAGE and ANNOTATE_MISSING_LINES are set to False. Exiting.')
33+
log.error(
34+
'No action taken as both SKIP_COVERAGE and ANNOTATE_MISSING_LINES are set to False. No comments or annotations will be generated.'
35+
)
3436
raise CoreProcessingException
3537

3638
def _init_github(self):
@@ -54,7 +56,7 @@ def _process_coverage(self):
5456
try:
5557
coverage = self.coverage_module.get_coverage_info(coverage_path=self.config.COVERAGE_PATH)
5658
except ConfigurationException as e:
57-
log.error('Error processing coverage data.')
59+
log.error('Error parsing the coverage file. Please check the file and try again.')
5860
raise CoreProcessingException from e
5961

6062
if self.config.BRANCH_COVERAGE:
@@ -105,15 +107,14 @@ def _process_pr(self):
105107
)
106108
except MissingMarker as e:
107109
log.error(
108-
'``{{ %s }}`` marker not found. The marker is necessary for this action to recognize '
109-
"its own comment and avoid making new comments or overwriting someone else's comment.",
110+
'Marker "%s" not found. This marker is required to identify the comment and prevent creating or overwriting comments.',
110111
marker,
111112
)
112113
raise CoreProcessingException from e
113114
except TemplateException as e:
114115
log.error(
115-
'There was a rendering error when computing the text of the comment to post '
116-
'on the PR. Please see the traceback for more information.'
116+
'Rendering error occurred while generating the comment text for the PR. See the traceback for more details. Error: %s',
117+
str(e),
117118
)
118119
raise CoreProcessingException from e
119120

codecov/template.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ def get_comment_markdown( # pylint: disable=too-many-arguments,too-many-locals
131131
coverage_report_url=coverage_report_url,
132132
)
133133
except jinja2.exceptions.TemplateError as exc:
134-
log.error('Error rendering template: %s', exc)
134+
log.error('Template rendering error: %s', str(exc))
135135
raise TemplateException from exc
136136

137137
if marker not in comment:
138-
log.error('Marker not found in the comment template')
138+
log.error('Marker is not found in the comment template.')
139139
raise MissingMarker
140140

141141
return comment

tests/coverage/test_base.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@ def extract_info(self, data):
1919
return Coverage(meta=None, info=None, files={})
2020

2121

22-
def test_diff_violations(make_coverage_and_diff):
23-
_, diff = make_coverage_and_diff(
24-
"""
25-
# file: a.py
26-
+ 1 line missing
27-
2 line missing
28-
+ 3 line missing
29-
4 line covered
30-
+ 5 line covered
31-
"""
32-
)
33-
assert diff.files[pathlib.Path('a.py')].violation_lines == [1, 3]
34-
35-
3622
class TestBase:
3723
@pytest.mark.parametrize(
3824
'num_covered, num_total, expected_coverage',

0 commit comments

Comments
 (0)