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
21 changes: 21 additions & 0 deletions src/devops_cli/commands/pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,26 @@ def _print_created_pr_message(stdout: str, base: str) -> None:
print_success(f"Pull request created successfully targeting base [bold]{base}[/bold]")


def _report_create_failure(res: Any, target_base: str) -> None:
"""Explain why a pull request could not be created.

`run_gh` captures the GitHub CLI's output, so a failure here printed nothing at all --
the command exited non-zero with no message beyond its own elapsed time. Three
invocations in one session produced no pull request and no indication of why: the base
branch had been deleted when its release merged, and nothing said so.
"""
from devops_cli.security.sanitizer import mask_secrets

detail = mask_secrets((res.stderr or res.stdout or "").strip()[:512])
print_error(
f"Could not create the pull request against base '{target_base}'."
+ (f"\n{detail}" if detail else ""),
safe=True,
)
if detail and "must first push" in detail:
print_info("Push the branch first: git push -u origin HEAD")


def _fallback_create_pr(
title: str,
body: str,
Expand Down Expand Up @@ -914,6 +934,7 @@ def create_pr(
if res.returncode != 0:
if _fallback_create_pr(title, body, target_base, draft, repo):
return
_report_create_failure(res, target_base)
raise typer.Exit(res.returncode)
print_success(f"Pull request created successfully targeting base [bold]{target_base}[/bold]")

Expand Down
52 changes: 52 additions & 0 deletions tests/test_pr_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1547,3 +1547,55 @@ def test_running_checks_can_be_excused_for_in_flight_verification() -> None:
def test_a_clean_pull_request_reports_ready() -> None:
"""Adding gates must not make every pull request unmergeable."""
assert _blockers(_ready_pr()) == []


def test_a_failed_create_explains_itself() -> None:
"""`run_gh` captures the CLI's output, so a failure printed nothing at all.

Three invocations in one session produced no pull request and no message beyond the
command's own elapsed time; the base branch had been deleted when its release merged
and nothing said so.
"""
from devops_cli.commands.pr import app as pr_app

failure = MagicMock(returncode=1, stdout="", stderr="Base ref must be a branch")
with (
patch("devops_cli.commands.pr.run_gh", return_value=failure),
patch("devops_cli.commands.pr._fallback_create_pr", return_value=False),
):
result = CliRunner().invoke(
pr_app, ["create", "--title", "t", "--body", "b", "--base", "release/v9.9.9"]
)
assert (result.exit_code, "release/v9.9.9" in result.output) == (1, True)


def test_a_failed_create_relays_the_underlying_reason() -> None:
"""The CLI's own message is the diagnostic; suppressing it hid the cause."""
from devops_cli.commands.pr import app as pr_app

failure = MagicMock(returncode=1, stdout="", stderr="Base ref must be a branch")
with (
patch("devops_cli.commands.pr.run_gh", return_value=failure),
patch("devops_cli.commands.pr._fallback_create_pr", return_value=False),
):
result = CliRunner().invoke(
pr_app, ["create", "--title", "t", "--body", "b", "--base", "release/v9.9.9"]
)
assert "Base ref must be a branch" in result.output


def test_an_unpushed_branch_gets_the_remedy() -> None:
"""The most common cause has a one-line fix; naming it saves a round trip."""
from devops_cli.commands.pr import app as pr_app

failure = MagicMock(
returncode=1, stdout="", stderr="aborted: you must first push the current branch"
)
with (
patch("devops_cli.commands.pr.run_gh", return_value=failure),
patch("devops_cli.commands.pr._fallback_create_pr", return_value=False),
):
result = CliRunner().invoke(
pr_app, ["create", "--title", "t", "--body", "b", "--base", "main"]
)
assert "git push -u origin HEAD" in result.output
Loading