From 75bd3ec022994563118a6c447b6aaceeb31dbd3d Mon Sep 17 00:00:00 2001 From: Daniel Petty Date: Tue, 22 Sep 2026 20:02:09 +0000 Subject: [PATCH] fix(pr): say why a pull request could not be created `devops pr create` printed nothing when it failed. `run_gh` captures the GitHub CLI's output, so the reason never reached the user: the command exited non-zero with no message beyond its own elapsed time. Three invocations in one session produced no pull request and no explanation. The base branch had been deleted when its release merged into main, and nothing said so -- I assumed the command had succeeded until checking whether the pull request existed. The underlying error is now relayed, masked and bounded, alongside the base that was attempted. An unpushed branch is the most common cause and gets its one-line remedy named, since that saves a round trip through the CLI's own help. --- src/devops_cli/commands/pr.py | 21 ++++++++++++++ tests/test_pr_cmd.py | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/src/devops_cli/commands/pr.py b/src/devops_cli/commands/pr.py index 7e303681f2..314799f38e 100644 --- a/src/devops_cli/commands/pr.py +++ b/src/devops_cli/commands/pr.py @@ -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, @@ -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]") diff --git a/tests/test_pr_cmd.py b/tests/test_pr_cmd.py index 71e1e98861..171c8280ac 100644 --- a/tests/test_pr_cmd.py +++ b/tests/test_pr_cmd.py @@ -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