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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-10-26
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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`
17 changes: 17 additions & 0 deletions openspec/specs/analyze-pr-dry-run/spec.md
Original file line number Diff line number Diff line change
@@ -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
21 changes: 14 additions & 7 deletions src/devops_cli/commands/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,24 @@ 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()
with exit_on_error(GitHubOperationError):
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

Expand Down Expand Up @@ -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)
25 changes: 25 additions & 0 deletions tests/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading