diff --git a/mergify_cli/ci/scopes/cli.py b/mergify_cli/ci/scopes/cli.py index 7d765dc..432178a 100644 --- a/mergify_cli/ci/scopes/cli.py +++ b/mergify_cli/ci/scopes/cli.py @@ -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 @@ -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) diff --git a/mergify_cli/tests/ci/scopes/test_cli.py b/mergify_cli/tests/ci/scopes/test_cli.py index 17fa06b..0e75880 100644 --- a/mergify_cli/tests/ci/scopes/test_cli.py +++ b/mergify_cli/tests/ci/scopes/test_cli.py @@ -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: