Skip to content

Commit 85a0f10

Browse files
authored
feat: add new cli tool (#849)
This introduces `mergify ci git-refs` to get the shas the monorepo tooling must use for testing batches. Depends-On: #848
1 parent b3a03f2 commit 85a0f10

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

mergify_cli/ci/cli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from mergify_cli import utils
66
from mergify_cli.ci import detector
7+
from mergify_cli.ci.git_refs import detector as git_refs_detector
78
from mergify_cli.ci.junit_processing import cli as junit_processing_cli
89
from mergify_cli.ci.scopes import cli as scopes_cli
910
from mergify_cli.ci.scopes import exceptions as scopes_exc
@@ -193,6 +194,17 @@ async def junit_process(
193194
)
194195

195196

197+
@ci.command(
198+
help="""Give the base/head git references of the pull request""",
199+
short_help="""Give the base/head git references of the pull request""",
200+
)
201+
def git_refs() -> None:
202+
ref = git_refs_detector.detect()
203+
click.echo(f"Base: {ref.base}")
204+
click.echo(f"Head: {ref.head}")
205+
ref.maybe_write_to_github_outputs()
206+
207+
196208
@ci.command(
197209
help="""Give the list scope impacted by changed files""",
198210
short_help="""Give the list scope impacted by changed files""",

mergify_cli/tests/ci/test_cli.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,28 @@ def test_scopes_send(
458458
assert result.exit_code == 0, result.output
459459
payload = json.loads(post_mock.calls[0].request.content)
460460
assert sorted(payload["scopes"]) == ["backend", "foobar", "frontend"]
461+
462+
463+
def test_git_refs(
464+
tmp_path: pathlib.Path,
465+
monkeypatch: pytest.MonkeyPatch,
466+
) -> None:
467+
event_data = {"before": "abc123", "after": "xyz987"}
468+
event_file = tmp_path / "event.json"
469+
event_file.write_text(json.dumps(event_data))
470+
471+
output_file = tmp_path / "github_output"
472+
473+
monkeypatch.setenv("GITHUB_OUTPUT", str(output_file))
474+
monkeypatch.setenv("GITHUB_EVENT_NAME", "push")
475+
monkeypatch.setenv("GITHUB_EVENT_PATH", str(event_file))
476+
477+
runner = testing.CliRunner()
478+
result = runner.invoke(ci_cli.git_refs, [])
479+
assert result.exit_code == 0, result.output
480+
481+
content = output_file.read_text()
482+
expected = """base=abc123
483+
head=xyz987
484+
"""
485+
assert content == expected

0 commit comments

Comments
 (0)