forked from py-cov-action/python-coverage-comment-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
443 lines (397 loc) · 14.9 KB
/
main.py
File metadata and controls
443 lines (397 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
from __future__ import annotations
import functools
import logging
import os
import sys
import httpx
from coverage_comment import activity as activity_module
from coverage_comment import (
comment_file,
communication,
diff_grouper,
files,
github,
github_client,
log,
log_utils,
settings,
storage,
subprocess,
template,
)
from coverage_comment import coverage as coverage_module
def main():
try:
logging.basicConfig(level="DEBUG")
logging.getLogger().handlers[0].formatter = log_utils.GitHubFormatter()
log.info("Starting action")
config = settings.Config.from_environ(environ=os.environ)
git = subprocess.Git()
with (
httpx.Client(
base_url=config.GITHUB_BASE_URL,
follow_redirects=True,
headers={"Authorization": f"token {config.GITHUB_TOKEN}"},
) as github_session,
httpx.Client() as http_session,
):
exit_code = action(
config=config,
github_session=github_session,
http_session=http_session,
git=git,
)
log.info("Ending action")
sys.exit(exit_code)
except Exception:
log.exception(
"Critical error. This error possibly occurred because the permissions of the workflow are set incorrectly. You can see the correct setting of permissions here: https://github.com/py-cov-action/python-coverage-comment-action#basic-usage\nOtherwise please look for open issues or open one in https://github.com/py-cov-action/python-coverage-comment-action/"
)
sys.exit(1)
def action(
config: settings.Config,
github_session: httpx.Client,
http_session: httpx.Client,
git: subprocess.Git,
) -> int:
log.debug(f"Operating on {config.GITHUB_REF}")
gh = github_client.GitHub(session=github_session)
event_name = config.GITHUB_EVENT_NAME
repo_info = github.get_repository_info(
github=gh, repository=config.GITHUB_REPOSITORY
)
try:
activity = activity_module.find_activity(
event_name=event_name,
is_default_branch=repo_info.is_default_branch(ref=config.GITHUB_REF),
event_type=config.GITHUB_EVENT_TYPE,
is_pr_merged=config.IS_PR_MERGED,
)
except activity_module.ActivityNotFound:
log.error(
'This action has only been designed to work for "pull_request", "push", '
f'"workflow_run", "schedule" or "merge_group" actions, not "{event_name}". Because there '
"are security implications. If you have a different usecase, please open an issue, "
"we'll be glad to add compatibility."
)
return 1
if activity == "save_coverage_data_files":
return save_coverage_data_files(
config=config,
git=git,
http_session=http_session,
repo_info=repo_info,
)
elif activity == "process_pr":
return process_pr(
config=config,
gh=gh,
repo_info=repo_info,
git=git,
)
else:
# activity == "post_comment":
return post_comment(
config=config,
gh=gh,
)
def process_pr(
config: settings.Config,
gh: github_client.GitHub,
repo_info: github.RepositoryInfo,
git: subprocess.Git,
) -> int:
log.info("Generating comment for PR")
if not config.GITHUB_PR_NUMBER and not config.GITHUB_BRANCH_NAME:
log.info(
"This worflow is not triggered on a pull_request event, "
"nor on a push event on a branch. Consequently, there's nothing to do. "
"Exiting."
)
return 0
_, coverage = coverage_module.get_coverage_info(
merge=config.MERGE_COVERAGE_FILES,
coverage_path=config.COVERAGE_PATH,
)
base_ref = config.GITHUB_BASE_REF or repo_info.default_branch
if config.GITHUB_BRANCH_NAME:
diff = github.get_branch_diff(
github=gh,
repository=config.GITHUB_REPOSITORY,
base_branch=base_ref,
head_branch=config.GITHUB_BRANCH_NAME,
)
elif config.GITHUB_PR_NUMBER:
diff = github.get_pr_diff(
github=gh,
repository=config.GITHUB_REPOSITORY,
pr_number=config.GITHUB_PR_NUMBER,
)
added_lines = coverage_module.get_added_lines(diff=diff)
diff_coverage = coverage_module.get_diff_coverage_info(
coverage=coverage, added_lines=added_lines
)
# It only really makes sense to display a comparison with the previous
# coverage if the PR target is the branch in which the coverage data is
# stored, e.g. the default branch.
# In the case we're running on a branch without a PR yet, we can't know
# if it's going to target the default branch, so we display it.
previous_coverage_data_file = None
pr_targets_default_branch = base_ref == repo_info.default_branch
if pr_targets_default_branch:
previous_coverage_data_file = storage.get_datafile_contents(
github=gh,
repository=config.GITHUB_REPOSITORY,
branch=config.FINAL_COVERAGE_DATA_BRANCH,
)
previous_coverage, previous_coverage_rate = None, None
if previous_coverage_data_file:
previous_coverage, previous_coverage_rate = files.parse_datafile(
contents=previous_coverage_data_file
)
marker = template.get_marker(marker_id=config.SUBPROJECT_ID)
files_info, count_files = template.select_files(
coverage=coverage,
diff_coverage=diff_coverage,
previous_coverage=previous_coverage,
max_files=config.MAX_FILES_IN_COMMENT,
)
try:
comment = template.get_comment_markdown(
coverage=coverage,
diff_coverage=diff_coverage,
previous_coverage=previous_coverage,
previous_coverage_rate=previous_coverage_rate,
files=files_info,
count_files=count_files,
max_files=config.MAX_FILES_IN_COMMENT,
minimum_green=config.MINIMUM_GREEN,
minimum_orange=config.MINIMUM_ORANGE,
github_host=github.extract_github_host(config.GITHUB_BASE_URL),
repo_name=config.GITHUB_REPOSITORY,
pr_number=config.GITHUB_PR_NUMBER,
base_template=template.read_template_file("comment.md.j2"),
custom_template=config.COMMENT_TEMPLATE,
pr_targets_default_branch=pr_targets_default_branch,
marker=marker,
subproject_id=config.SUBPROJECT_ID,
)
# Same as above except `max_files` is None
summary_comment = template.get_comment_markdown(
coverage=coverage,
diff_coverage=diff_coverage,
previous_coverage=previous_coverage,
previous_coverage_rate=previous_coverage_rate,
files=files_info,
count_files=count_files,
max_files=None,
minimum_green=config.MINIMUM_GREEN,
minimum_orange=config.MINIMUM_ORANGE,
github_host=github.extract_github_host(config.GITHUB_BASE_URL),
repo_name=config.GITHUB_REPOSITORY,
pr_number=config.GITHUB_PR_NUMBER,
base_template=template.read_template_file("comment.md.j2"),
custom_template=config.COMMENT_TEMPLATE,
pr_targets_default_branch=pr_targets_default_branch,
marker=marker,
subproject_id=config.SUBPROJECT_ID,
)
except template.MissingMarker:
log.error(
"Marker not found. This error can happen if you defined a custom comment "
"template that doesn't inherit the base template and you didn't include "
"``{{ marker }}``. The marker is necessary for this action to recognize "
"its own comment and avoid making new comments or overwriting someone else's "
"comment."
)
return 1
except template.TemplateError:
log.exception(
"There was a rendering error when computing the text of the comment to post "
"on the PR. Please see the traceback, in particular if you're using a custom "
"template."
)
return 1
github.add_job_summary(
content=summary_comment, github_step_summary=config.GITHUB_STEP_SUMMARY
)
pr_number: int | None = config.GITHUB_PR_NUMBER
if pr_number is None:
# If we don't have a PR number, we're launched from a push event,
# so we need to find the PR number from the branch name
try:
pr_number = github.find_pr_for_branch(
github=gh,
# A push event cannot be initiated from a forked repository
repository=config.GITHUB_REPOSITORY,
owner=config.GITHUB_REPOSITORY.split("/")[0],
branch=config.GITHUB_BRANCH_NAME,
)
except github.CannotDeterminePR:
pr_number = None
if pr_number is not None and config.ANNOTATE_MISSING_LINES:
annotations = diff_grouper.get_diff_missing_groups(
coverage=coverage, diff_coverage=diff_coverage
)
github.create_missing_coverage_annotations(
annotation_type=config.ANNOTATION_TYPE,
annotations=[
(annotation.file, annotation.line_start, annotation.line_end)
for annotation in annotations
],
)
try:
if config.FORCE_WORKFLOW_RUN or not pr_number:
raise github.CannotPostComment
github.post_comment(
github=gh,
me=github.get_my_login(github=gh),
repository=config.GITHUB_REPOSITORY,
pr_number=pr_number,
contents=comment,
marker=marker,
)
except github.CannotPostComment:
log.debug("Exception when posting comment", exc_info=True)
log.info(
"Cannot post comment. This is probably because this is an external PR, so "
"it's expected. Ensure you have an additional `workflow_run` step "
"configured as explained in the documentation (or alternatively, give up "
"on PR comments for external PRs)."
)
comment_file.store_file(
filename=config.FINAL_COMMENT_FILENAME,
content=comment,
)
github.set_output(github_output=config.GITHUB_OUTPUT, COMMENT_FILE_WRITTEN=True)
log.debug("Comment stored locally on disk")
else:
github.set_output(
github_output=config.GITHUB_OUTPUT, COMMENT_FILE_WRITTEN=False
)
log.debug("Comment not generated")
return 0
def post_comment(
config: settings.Config,
gh: github_client.GitHub,
) -> int:
log.info("Posting comment to PR")
if not config.GITHUB_PR_RUN_ID:
log.error("Missing input GITHUB_PR_RUN_ID. Please consult the documentation.")
return 1
me = github.get_my_login(github=gh)
log.info(f"Search for PR associated with run id {config.GITHUB_PR_RUN_ID}")
owner, branch = github.get_branch_from_workflow_run(
github=gh,
run_id=config.GITHUB_PR_RUN_ID,
repository=config.GITHUB_REPOSITORY,
)
try:
pr_number = github.find_pr_for_branch(
github=gh,
repository=config.GITHUB_REPOSITORY,
owner=owner,
branch=branch,
)
except github.CannotDeterminePR:
log.error(
"The PR cannot be found. That's strange. Please open an "
"issue at https://github.com/py-cov-action/python-coverage-comment-action",
exc_info=True,
)
return 1
log.info(f"PR number: {pr_number}")
log.info("Download associated artifacts")
try:
comment = github.download_artifact(
github=gh,
repository=config.GITHUB_REPOSITORY,
artifact_name=config.COMMENT_ARTIFACT_NAME,
run_id=config.GITHUB_PR_RUN_ID,
filename=config.FINAL_COMMENT_FILENAME,
)
except github.NoArtifact:
log.info(
"Artifact was not found, which is probably because it was probably "
"already posted by a previous step.",
exc_info=True,
)
return 0
log.info("Comment file found in artifact, posting to PR")
github.post_comment(
github=gh,
me=me,
repository=config.GITHUB_REPOSITORY,
pr_number=pr_number,
contents=comment,
marker=template.get_marker(marker_id=config.SUBPROJECT_ID),
)
log.info("Comment posted in PR")
return 0
def save_coverage_data_files(
config: settings.Config,
git: subprocess.Git,
http_session: httpx.Client,
repo_info: github.RepositoryInfo,
) -> int:
log.info("Computing coverage files & badge")
raw_coverage_data, coverage = coverage_module.get_coverage_info(
merge=config.MERGE_COVERAGE_FILES,
coverage_path=config.COVERAGE_PATH,
)
operations: list[files.Operation] = files.compute_files(
line_rate=coverage.info.percent_covered,
raw_coverage_data=raw_coverage_data,
coverage_path=config.COVERAGE_PATH,
minimum_green=config.MINIMUM_GREEN,
minimum_orange=config.MINIMUM_ORANGE,
http_session=http_session,
)
is_public = repo_info.is_public()
if is_public:
log.info("Generating HTML coverage report")
operations.append(
files.get_coverage_html_files(coverage_path=config.COVERAGE_PATH)
)
markdown_report = coverage_module.generate_coverage_markdown(
coverage_path=config.COVERAGE_PATH
)
github.add_job_summary(
content=f"## Coverage report\n\n{markdown_report}",
github_step_summary=config.GITHUB_STEP_SUMMARY,
)
github_host = github.extract_github_host(config.GITHUB_BASE_URL)
url_getter = functools.partial(
storage.get_raw_file_url,
github_host=github_host,
is_public=is_public,
repository=config.GITHUB_REPOSITORY,
branch=config.FINAL_COVERAGE_DATA_BRANCH,
)
readme_url = storage.get_repo_file_url(
github_host=github_host,
branch=config.FINAL_COVERAGE_DATA_BRANCH,
repository=config.GITHUB_REPOSITORY,
)
html_report_url = storage.get_html_report_url(
github_host=github_host,
branch=config.FINAL_COVERAGE_DATA_BRANCH,
repository=config.GITHUB_REPOSITORY,
use_gh_pages_html_url=config.USE_GH_PAGES_HTML_URL,
)
readme_file, log_message = communication.get_readme_and_log(
is_public=is_public,
readme_url=readme_url,
image_urls=files.get_urls(url_getter=url_getter),
html_report_url=html_report_url,
markdown_report=markdown_report,
subproject_id=config.SUBPROJECT_ID,
)
operations.append(readme_file)
storage.commit_operations(
operations=operations,
git=git,
branch=config.FINAL_COVERAGE_DATA_BRANCH,
)
log.info(log_message)
return 0