From cce83c0ba4dd5b6547fbc9446877100d1ca37f69 Mon Sep 17 00:00:00 2001 From: Daniel Petty Date: Mon, 26 Oct 2026 20:00:44 +0000 Subject: [PATCH] fix(ai): analyze pr previews under --dry-run and contacts no service --- .../.openspec.yaml | 2 ++ .../design.md | 8 +++++ .../proposal.md | 36 +++++++++++++++++++ .../specs/analyze-pr-dry-run/spec.md | 17 +++++++++ .../tasks.md | 12 +++++++ openspec/specs/analyze-pr-dry-run/spec.md | 17 +++++++++ src/devops_cli/commands/analyze.py | 21 +++++++---- tests/test_analyze.py | 25 +++++++++++++ 8 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/.openspec.yaml create mode 100644 openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/design.md create mode 100644 openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/proposal.md create mode 100644 openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/specs/analyze-pr-dry-run/spec.md create mode 100644 openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/tasks.md create mode 100644 openspec/specs/analyze-pr-dry-run/spec.md diff --git a/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/.openspec.yaml b/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/.openspec.yaml new file mode 100644 index 0000000000..d58ba02327 --- /dev/null +++ b/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/.openspec.yaml @@ -0,0 +1,2 @@ +schema: spec-driven +created: 2026-10-26 diff --git a/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/design.md b/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/design.md new file mode 100644 index 0000000000..71ecd3746f --- /dev/null +++ b/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/design.md @@ -0,0 +1,8 @@ +# Design + +The dry-run check sits right after the `--explain` branch, before the GitHub session is created. + +## Rejected alternatives + +- **Keep the call and skip only the write.** The call to `get_pull` is the external communication that #412 forbids. Rejected. +- **Add a shared decorator.** One command needs the check. A decorator adds code with no second user. Rejected. diff --git a/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/proposal.md b/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/proposal.md new file mode 100644 index 0000000000..a44031e8ed --- /dev/null +++ b/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/proposal.md @@ -0,0 +1,36 @@ +# Proposal + +## Why + +Issue #2314: `devops ai analyze pr` declares `--dry-run`, but it opens a GitHub session and calls `get_pull` and `get_pull_files` first. +These calls contact the GitHub service. Under #412 and #986, this is external communication in a dry run. + +## What Changes + +- `analyze pr` checks `--dry-run` and `is_dry_run()` first. It renders `render_dry_run_result` and returns. +- In a dry run the command opens no GitHub session, starts no child process and writes no file. +- The dry-run branches that the check makes dead are removed from the rest of the command. +- The change adds the capability `analyze-pr-dry-run`: a command option is observable behavior. +- This change fits in one pull request. + +## Capabilities + +### New Capabilities + +- `analyze-pr-dry-run`: dry-run behavior of `devops ai analyze pr`. + +### Modified Capabilities + +None. + +## Impact + +- `src/devops_cli/commands/analyze.py`, `tests/test_analyze.py`. + +## Feasibility + +- Command output: the issue cites `analyze.py` lines 523 to 524. The code at the base commit calls `session.client.get_pull` before any dry-run check. The new test fails on it. + +## Existing Tool Considered + +No outside tool applies. The change reuses `render_dry_run_result` and deletes the dead dry-run branches of `analyze pr`. diff --git a/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/specs/analyze-pr-dry-run/spec.md b/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/specs/analyze-pr-dry-run/spec.md new file mode 100644 index 0000000000..23c10787bd --- /dev/null +++ b/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/specs/analyze-pr-dry-run/spec.md @@ -0,0 +1,17 @@ +# analyze-pr-dry-run Specification + +## Purpose +Defines how `devops ai analyze pr` behaves under dry run: it previews the planned action and makes no external call. + +## ADDED Requirements + +### Requirement: Dry run makes no external call +The system SHALL render a dry-run preview for `devops ai analyze pr` and SHALL NOT open a GitHub session, start a child process or write a file when `--dry-run` or the dry-run environment is set. + +#### Scenario: Option dry run +- **WHEN** the user runs `devops ai analyze pr 42 --dry-run` +- **THEN** the command prints the dry-run preview, contacts no GitHub service and exits 0 + +#### Scenario: Environment dry run +- **WHEN** `DEVOPS_CLI_DRY_RUN` is true and the user runs `devops ai analyze pr 42` +- **THEN** the command prints the dry-run preview, contacts no GitHub service and exits 0 diff --git a/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/tasks.md b/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/tasks.md new file mode 100644 index 0000000000..35fdd2a1bf --- /dev/null +++ b/openspec/changes/archive/2026-10-26-2314-analyze-pr-dry-run/tasks.md @@ -0,0 +1,12 @@ +# Tasks + +## 1. Dry run of analyze pr + +- [x] Add a failing test for `analyze pr` under `--dry-run` and the dry-run environment + - Verification: `uv run devops ci test tests/test_analyze.py -k dry_run_previews` +- [x] Render the preview before the GitHub session and remove the dead dry-run branches + - Verification: `uv run devops ci test tests/test_analyze.py` +- [x] Regenerate documentation + - Verification: `uv run devops docs generate --sync-readme` +- [x] Archive the change + - Verification: `uv run devops openspec archive 2314-analyze-pr-dry-run` diff --git a/openspec/specs/analyze-pr-dry-run/spec.md b/openspec/specs/analyze-pr-dry-run/spec.md new file mode 100644 index 0000000000..56bcfafc9c --- /dev/null +++ b/openspec/specs/analyze-pr-dry-run/spec.md @@ -0,0 +1,17 @@ +# analyze-pr-dry-run Specification + +## Purpose +Defines how `devops ai analyze pr` behaves under dry run: it previews the planned action and makes no external call. + +## Requirements + +### Requirement: Dry run makes no external call +The system SHALL render a dry-run preview for `devops ai analyze pr` and SHALL NOT open a GitHub session, start a child process or write a file when `--dry-run` or the dry-run environment is set. + +#### Scenario: Option dry run +- **WHEN** the user runs `devops ai analyze pr 42 --dry-run` +- **THEN** the command prints the dry-run preview, contacts no GitHub service and exits 0 + +#### Scenario: Environment dry run +- **WHEN** `DEVOPS_CLI_DRY_RUN` is true and the user runs `devops ai analyze pr 42` +- **THEN** the command prints the dry-run preview, contacts no GitHub service and exits 0 diff --git a/src/devops_cli/commands/analyze.py b/src/devops_cli/commands/analyze.py index ccaedae801..69fecb60f0 100644 --- a/src/devops_cli/commands/analyze.py +++ b/src/devops_cli/commands/analyze.py @@ -490,6 +490,16 @@ def analyze_pr( # noqa: C901 render_explanation("analyze") return + if dry_run or is_dry_run(): + from devops_cli.output.console import render_dry_run_result + + render_dry_run_result( + command="devops ai analyze pr", + action="analyze pull request and save metadata", + target=str(pr_number), + details={"enhanced": enhanced, "update_all": update_all}, + ) + return from devops_cli.github.session import get_github_session repo = find_repo_root() @@ -497,7 +507,7 @@ def analyze_pr( # noqa: C901 session = get_github_session() ai_client = None - if enhanced and not (dry_run or is_dry_run()): + if enhanced: try: from devops_cli.ai.client import get_llm_client @@ -573,10 +583,7 @@ def analyze_pr( # noqa: C901 title = f"{repo.name} PR #{pr_number} analysis: {pull.title}" ref_str = str(pr_number) - out_file = save_analysis_metadata( - "pr", ref_str, title, file_metas, repo, enhanced=enhanced, dry_run=dry_run - ) + out_file = save_analysis_metadata("pr", ref_str, title, file_metas, repo, enhanced=enhanced) - if not (dry_run or is_dry_run()): - payload_data = json.loads(out_file.read_text(encoding="utf-8")) - _render_analysis_summary(AnalysisMetadata.model_validate(payload_data), out_file) + payload_data = json.loads(out_file.read_text(encoding="utf-8")) + _render_analysis_summary(AnalysisMetadata.model_validate(payload_data), out_file) diff --git a/tests/test_analyze.py b/tests/test_analyze.py index d2067c2e00..2f9cf71fd8 100644 --- a/tests/test_analyze.py +++ b/tests/test_analyze.py @@ -940,3 +940,28 @@ def read(repo: Path, revision: str, rel_path: str) -> str: [("merge-sha", "old_name.py")], [("new_name.py", "renamed", ["fresh"], ["dropped"], ["moved"])], ) + + +def test_analyze_pr_dry_run_previews_and_contacts_no_service( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + """`analyze pr --dry-run` renders the preview and opens no GitHub session.""" + (tmp_path / ".git").mkdir(exist_ok=True) + monkeypatch.chdir(tmp_path) + + def forbidden(*_args: object, **_kwargs: object) -> None: + raise AssertionError("dry run contacted GitHub") + + monkeypatch.setattr("devops_cli.github.session.get_github_session", forbidden) + monkeypatch.setattr("subprocess.run", forbidden) + monkeypatch.setattr("subprocess.Popen", forbidden) + with patch("devops_cli.commands.analyze.find_repo_root", return_value=tmp_path): + result = runner.invoke(analyze_app, ["pr", "42", "--dry-run"]) + monkeypatch.setenv("DEVOPS_CLI_DRY_RUN", "true") + env_result = runner.invoke(analyze_app, ["pr", "42"]) + + assert result.exit_code == 0, result.output + assert "devops ai analyze pr" in result.output + assert "42" in result.output + assert env_result.exit_code == 0, env_result.output + assert not (tmp_path / ".data" / "analysis").exists()