diff --git a/mergify_cli/ci/cli.py b/mergify_cli/ci/cli.py index 7850f87..9ee84bd 100644 --- a/mergify_cli/ci/cli.py +++ b/mergify_cli/ci/cli.py @@ -4,6 +4,7 @@ from mergify_cli import utils from mergify_cli.ci import detector +from mergify_cli.ci.git_refs import detector as git_refs_detector from mergify_cli.ci.junit_processing import cli as junit_processing_cli from mergify_cli.ci.scopes import cli as scopes_cli from mergify_cli.ci.scopes import exceptions as scopes_exc @@ -193,6 +194,17 @@ async def junit_process( ) +@ci.command( + help="""Give the base/head git references of the pull request""", + short_help="""Give the base/head git references of the pull request""", +) +def git_refs() -> None: + ref = git_refs_detector.detect() + click.echo(f"Base: {ref.base}") + click.echo(f"Head: {ref.head}") + ref.maybe_write_to_github_outputs() + + @ci.command( help="""Give the list scope impacted by changed files""", short_help="""Give the list scope impacted by changed files""", diff --git a/mergify_cli/tests/ci/test_cli.py b/mergify_cli/tests/ci/test_cli.py index d84a690..8f7f3c9 100644 --- a/mergify_cli/tests/ci/test_cli.py +++ b/mergify_cli/tests/ci/test_cli.py @@ -458,3 +458,28 @@ def test_scopes_send( assert result.exit_code == 0, result.output payload = json.loads(post_mock.calls[0].request.content) assert sorted(payload["scopes"]) == ["backend", "foobar", "frontend"] + + +def test_git_refs( + tmp_path: pathlib.Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + event_data = {"before": "abc123", "after": "xyz987"} + event_file = tmp_path / "event.json" + event_file.write_text(json.dumps(event_data)) + + output_file = tmp_path / "github_output" + + monkeypatch.setenv("GITHUB_OUTPUT", str(output_file)) + monkeypatch.setenv("GITHUB_EVENT_NAME", "push") + monkeypatch.setenv("GITHUB_EVENT_PATH", str(event_file)) + + runner = testing.CliRunner() + result = runner.invoke(ci_cli.git_refs, []) + assert result.exit_code == 0, result.output + + content = output_file.read_text() + expected = """base=abc123 +head=xyz987 +""" + assert content == expected