Skip to content

Commit 14fb28a

Browse files
daniel-anyaewjoachim
authored andcommitted
Updates the action to support saving coverage artifacts on pull request merge to the default branch.
This is functionally equivalent to the currently supported pattern of pushing to the default branch.
1 parent eeefd7d commit 14fb28a

File tree

5 files changed

+30
-9
lines changed

5 files changed

+30
-9
lines changed

coverage_comment/activity.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ class ActivityNotFound(Exception):
1616
def find_activity(
1717
event_name: str,
1818
is_default_branch: bool,
19+
event_action: str | None = None,
1920
) -> str:
2021
"""Find the activity to perform based on the event type and payload."""
2122
if event_name == "workflow_run":
2223
return "post_comment"
2324

24-
if (event_name == "push" and is_default_branch) or event_name == "schedule":
25+
if (event_name == "push" and is_default_branch) or (event_name == "pull_request" and event_action == "merged" and is_default_branch) or event_name == "schedule":
2526
return "save_coverage_data_files"
2627

2728
if event_name not in {"pull_request", "push"}:

coverage_comment/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import os
66
import sys
7+
import json
78

89
import httpx
910

@@ -69,12 +70,23 @@ def action(
6970
log.debug(f"Operating on {config.GITHUB_REF}")
7071
gh = github_client.GitHub(session=github_session)
7172
event_name = config.GITHUB_EVENT_NAME
73+
event_path = config.GITHUB_EVENT_PATH
74+
event_action = None
75+
76+
if event_path and os.path.exists(event_path):
77+
with open(event_path, "r") as event_file:
78+
event_payload = json.load(event_file)
79+
is_merged_pr_action = event_payload.get("pull_request", {}).get("merged", False)
80+
if is_merged_pr_action:
81+
event_action = "merged"
82+
7283
repo_info = github.get_repository_info(
7384
github=gh, repository=config.GITHUB_REPOSITORY
7485
)
7586
try:
7687
activity = activity_module.find_activity(
7788
event_name=event_name,
89+
event_action=event_action,
7890
is_default_branch=repo_info.is_default_branch(ref=config.GITHUB_REF),
7991
)
8092
except activity_module.ActivityNotFound:

coverage_comment/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Config:
4747
# (from https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables )
4848
GITHUB_REF: str
4949
GITHUB_EVENT_NAME: str
50+
GITHUB_EVENT_PATH: pathlib.Path | None = None
5051
GITHUB_PR_RUN_ID: int | None
5152
GITHUB_STEP_SUMMARY: pathlib.Path
5253
COMMENT_TEMPLATE: str | None = None

tests/unit/test_activity.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66

77

88
@pytest.mark.parametrize(
9-
"event_name, is_default_branch, expected_activity",
9+
"event_name, event_action, is_default_branch, expected_activity",
1010
[
11-
("workflow_run", True, "post_comment"),
12-
("push", True, "save_coverage_data_files"),
13-
("push", False, "process_pr"),
14-
("pull_request", True, "process_pr"),
15-
("pull_request", False, "process_pr"),
11+
("workflow_run", None, True, "post_comment"),
12+
("push", None, True, "save_coverage_data_files"),
13+
("push", None, False, "process_pr"),
14+
("pull_request", "merged", True, "save_coverage_data_files"),
15+
("pull_request", None, True, "process_pr"),
16+
("pull_request", None, False, "process_pr"),
17+
("schedule", None, False, "save_coverage_data_files"),
1618
],
1719
)
18-
def test_find_activity(event_name, is_default_branch, expected_activity):
20+
def test_find_activity(event_name, event_action, is_default_branch, expected_activity):
1921
result = activity.find_activity(
20-
event_name=event_name, is_default_branch=is_default_branch
22+
event_name=event_name, event_action=event_action, is_default_branch=is_default_branch
2123
)
2224
assert result == expected_activity
2325

tests/unit/test_settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def test_config__from_environ__ok():
3333
"GITHUB_REF": "master",
3434
"GITHUB_OUTPUT": "foo.txt",
3535
"GITHUB_EVENT_NAME": "pull",
36+
"GITHUB_EVENT_PATH": pathlib.Path("test_event_path"),
3637
"GITHUB_PR_RUN_ID": "123",
3738
"GITHUB_STEP_SUMMARY": "step_summary",
3839
"COMMENT_ARTIFACT_NAME": "baz",
@@ -56,6 +57,7 @@ def test_config__from_environ__ok():
5657
GITHUB_REF="master",
5758
GITHUB_OUTPUT=pathlib.Path("foo.txt"),
5859
GITHUB_EVENT_NAME="pull",
60+
GITHUB_EVENT_PATH=pathlib.Path("test_event_path"),
5961
GITHUB_PR_RUN_ID=123,
6062
GITHUB_STEP_SUMMARY=pathlib.Path("step_summary"),
6163
COMMENT_ARTIFACT_NAME="baz",
@@ -82,6 +84,7 @@ def test_config__verbose_deprecated(get_logs):
8284
"GITHUB_REPOSITORY": "owner/repo",
8385
"GITHUB_REF": "master",
8486
"GITHUB_EVENT_NAME": "pull",
87+
"GITHUB_EVENT_PATH": pathlib.Path("test_event_path"),
8588
"GITHUB_PR_RUN_ID": "123",
8689
"GITHUB_STEP_SUMMARY": "step_summary",
8790
"VERBOSE": "true",
@@ -92,6 +95,7 @@ def test_config__verbose_deprecated(get_logs):
9295
GITHUB_REPOSITORY="owner/repo",
9396
GITHUB_REF="master",
9497
GITHUB_EVENT_NAME="pull",
98+
GITHUB_EVENT_PATH=pathlib.Path("test_event_path"),
9599
GITHUB_PR_RUN_ID=123,
96100
GITHUB_STEP_SUMMARY=pathlib.Path("step_summary"),
97101
VERBOSE=False,
@@ -107,6 +111,7 @@ def config():
107111
"GITHUB_REPOSITORY": "owner/repo",
108112
"GITHUB_REF": "master",
109113
"GITHUB_EVENT_NAME": "pull",
114+
"GITHUB_EVENT_PATH": pathlib.Path("test_event_path"),
110115
"GITHUB_PR_RUN_ID": 123,
111116
"GITHUB_STEP_SUMMARY": pathlib.Path("step_summary"),
112117
"COMMENT_ARTIFACT_NAME": "baz",

0 commit comments

Comments
 (0)