Skip to content

Adds additional support for Github enterprise usecases #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
3 changes: 2 additions & 1 deletion coverage_comment/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ class ActivityNotFound(Exception):
def find_activity(
event_name: str,
is_default_branch: bool,
event_action: str | None = None,
) -> str:
"""Find the activity to perform based on the event type and payload."""
if event_name == "workflow_run":
return "post_comment"

if (event_name == "push" and is_default_branch) or event_name == "schedule":
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":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there should be a mention of this in the README too?

return "save_coverage_data_files"

if event_name not in {"pull_request", "push"}:
Expand Down
12 changes: 12 additions & 0 deletions coverage_comment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import os
import sys
import json

import httpx

Expand Down Expand Up @@ -69,12 +70,23 @@ def action(
log.debug(f"Operating on {config.GITHUB_REF}")
gh = github_client.GitHub(session=github_session)
event_name = config.GITHUB_EVENT_NAME
event_path = config.GITHUB_EVENT_PATH
event_action = None

if event_path and os.path.exists(event_path):
with open(event_path, "r") as event_file:
event_payload = json.load(event_file)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use Pathlib everywhere else in the project, and event_path is already a path

Suggested change
with open(event_path, "r") as event_file:
event_payload = json.load(event_file)
event_payload = json.loads(event_path.read_text())

is_merged_pr_action = event_payload.get("pull_request", {}).get("merged", False)
if is_merged_pr_action:
event_action = "merged"

repo_info = github.get_repository_info(
github=gh, repository=config.GITHUB_REPOSITORY
)
try:
activity = activity_module.find_activity(
event_name=event_name,
event_action=event_action,
is_default_branch=repo_info.is_default_branch(ref=config.GITHUB_REF),
)
except activity_module.ActivityNotFound:
Expand Down
1 change: 1 addition & 0 deletions coverage_comment/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Config:
# (from https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables )
GITHUB_REF: str
GITHUB_EVENT_NAME: str
GITHUB_EVENT_PATH: pathlib.Path | None = None
GITHUB_PR_RUN_ID: int | None
GITHUB_STEP_SUMMARY: pathlib.Path
COMMENT_TEMPLATE: str | None = None
Expand Down
18 changes: 10 additions & 8 deletions tests/unit/test_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@


@pytest.mark.parametrize(
"event_name, is_default_branch, expected_activity",
"event_name, event_action, is_default_branch, expected_activity",
[
("workflow_run", True, "post_comment"),
("push", True, "save_coverage_data_files"),
("push", False, "process_pr"),
("pull_request", True, "process_pr"),
("pull_request", False, "process_pr"),
("workflow_run", None, True, "post_comment"),
("push", None, True, "save_coverage_data_files"),
("push", None, False, "process_pr"),
("pull_request", "merged", True, "save_coverage_data_files"),
("pull_request", None, True, "process_pr"),
("pull_request", None, False, "process_pr"),
("schedule", None, False, "save_coverage_data_files"),
],
)
def test_find_activity(event_name, is_default_branch, expected_activity):
def test_find_activity(event_name, event_action, is_default_branch, expected_activity):
result = activity.find_activity(
event_name=event_name, is_default_branch=is_default_branch
event_name=event_name, event_action=event_action, is_default_branch=is_default_branch
)
assert result == expected_activity

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_config__from_environ__ok():
"GITHUB_REF": "master",
"GITHUB_OUTPUT": "foo.txt",
"GITHUB_EVENT_NAME": "pull",
"GITHUB_EVENT_PATH": pathlib.Path("test_event_path"),
"GITHUB_PR_RUN_ID": "123",
"GITHUB_STEP_SUMMARY": "step_summary",
"COMMENT_ARTIFACT_NAME": "baz",
Expand All @@ -56,6 +57,7 @@ def test_config__from_environ__ok():
GITHUB_REF="master",
GITHUB_OUTPUT=pathlib.Path("foo.txt"),
GITHUB_EVENT_NAME="pull",
GITHUB_EVENT_PATH=pathlib.Path("test_event_path"),
GITHUB_PR_RUN_ID=123,
GITHUB_STEP_SUMMARY=pathlib.Path("step_summary"),
COMMENT_ARTIFACT_NAME="baz",
Expand All @@ -82,6 +84,7 @@ def test_config__verbose_deprecated(get_logs):
"GITHUB_REPOSITORY": "owner/repo",
"GITHUB_REF": "master",
"GITHUB_EVENT_NAME": "pull",
"GITHUB_EVENT_PATH": pathlib.Path("test_event_path"),
"GITHUB_PR_RUN_ID": "123",
"GITHUB_STEP_SUMMARY": "step_summary",
"VERBOSE": "true",
Expand All @@ -92,6 +95,7 @@ def test_config__verbose_deprecated(get_logs):
GITHUB_REPOSITORY="owner/repo",
GITHUB_REF="master",
GITHUB_EVENT_NAME="pull",
GITHUB_EVENT_PATH=pathlib.Path("test_event_path"),
GITHUB_PR_RUN_ID=123,
GITHUB_STEP_SUMMARY=pathlib.Path("step_summary"),
VERBOSE=False,
Expand All @@ -107,6 +111,7 @@ def config():
"GITHUB_REPOSITORY": "owner/repo",
"GITHUB_REF": "master",
"GITHUB_EVENT_NAME": "pull",
"GITHUB_EVENT_PATH": pathlib.Path("test_event_path"),
"GITHUB_PR_RUN_ID": 123,
"GITHUB_STEP_SUMMARY": pathlib.Path("step_summary"),
"COMMENT_ARTIFACT_NAME": "baz",
Expand Down