Skip to content
Closed
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
12 changes: 12 additions & 0 deletions mergify_cli/ci/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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""",
Expand Down
25 changes: 25 additions & 0 deletions mergify_cli/tests/ci/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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