Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions mergify_cli/ci/scopes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ def maybe_write_github_outputs(
)


def maybe_write_github_step_summary(
base: str,
all_scopes: abc.Iterable[str],
scopes_hit: set[str],
) -> None:
gha = os.environ.get("GITHUB_STEP_SUMMARY")
if not gha:
return
# Build a pretty Markdown table with emojis
markdown = f"## Mergify CI Scope Matching Results for `{base}...HEAD`\n\n"
markdown += "| 🎯 Scope | ✅ Match |\n|:--|:--|\n"
for scope in sorted(all_scopes):
emoji = "✅" if scope in scopes_hit else "❌"
markdown += f"| `{scope}` | {emoji} |\n"

with pathlib.Path(gha).open("a", encoding="utf-8") as fh:
fh.write(markdown)


class InvalidDetectedScopeError(exceptions.ScopesError):
pass

Expand Down Expand Up @@ -136,6 +155,7 @@ def detect(config_path: str) -> DetectedScope:
click.echo("No scopes matched.")

maybe_write_github_outputs(all_scopes, scopes_hit)
maybe_write_github_step_summary(base.ref, all_scopes, scopes_hit)
return DetectedScope(base_ref=base.ref, scopes=scopes_hit)


Expand Down
24 changes: 24 additions & 0 deletions mergify_cli/tests/ci/scopes/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,30 @@ def test_maybe_write_github_outputs(
assert """{"backend": "true", "docs": "true", "frontend": "false"}""" in content


def test_maybe_write_github_step_summary(
tmp_path: pathlib.Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
output_file = tmp_path / "github_step_summary"
monkeypatch.setenv("GITHUB_STEP_SUMMARY", str(output_file))

all_scopes = ["backend", "frontend", "docs"]
scopes_hit = {"backend", "docs"}

cli.maybe_write_github_step_summary("da26838", all_scopes, scopes_hit)

content = output_file.read_text()
expected = """## Mergify CI Scope Matching Results for `da26838...HEAD`

| 🎯 Scope | ✅ Match |
|:--|:--|
| `backend` | ✅ |
| `docs` | ✅ |
| `frontend` | ❌ |
"""
assert content == expected


def test_maybe_write_github_outputs_no_env(
monkeypatch: pytest.MonkeyPatch,
) -> None:
Expand Down