Skip to content

fix(tf): tf notify-plan counts a plan by a fixed text match #4929

Closed

Description

@dan-petty

Problem Description

devops tf notify-plan prints a wrong summary for a real plan, and it prints a fixed sample plan when the plan file does not exist.

  • The command counts by a fixed text match: adds = 2 if "2 to add" in raw_text else 0, changes = 1 if "1 to change" in raw_text else 0, and destroys = 0. A plan with other counts gets +0 ~0 -0.
  • When --plan-file names a path that does not exist, the command prints the sample plan Plan: 2 to add, 1 to change, 0 to destroy. and exits 0. The caller does not see that the file is missing.

Source: tf_notify_plan in src/devops_cli/commands/tf.py.

Reproduction Steps

  1. Write Plan: 5 to add, 3 to change, 1 to destroy. to plan.txt.
  2. Run uv run devops tf notify-plan --plan-file plan.txt --json.
  3. Run uv run devops tf notify-plan --plan-file no-such-file.txt --json.

Expected vs Actual Behavior

Expected: step 2 gives adds 5, changes 3 and destroys 1. Step 3 fails with an error that names the missing file.

Actual: step 2 gives adds 0, changes 0 and destroys 0. Step 3 exits 0 and prints the sample plan with +2 ~1 -0.

Environment Diagnostics

devops-cli at origin/main, 2026-11-12.

Logs, Traces, or Terminal Output

$ devops tf notify-plan --plan-file plan.txt --json
{"pr": null, "adds": 0, "changes": 0, "destroys": 0, ...}
$ devops tf notify-plan --plan-file no-such-file.txt --json
{"pr": null, "adds": 2, "changes": 1, "destroys": 0, ...}

Existing tool considered

tofu show -json <planfile> gives the planned actions of each resource in resource_changes, so the counts can come from OpenTofu and not from a text match. Typer can refuse a path that does not exist (exists=True on the option).

Related

Related: #3644, which makes the words of the command say what it does.

Source: #3644

Alternatives check of 2026-11-15: in-use.
The repository already uses this tool.
Tool: opentofu/opentofu (https://github.com/opentofu/opentofu), MPL-2.0, last activity 2026-11-13, maintained.

Proposed design

Problem statement

The devops tf notify-plan command reports the wrong counts for a real plan and prints a fixed sample plan when the plan file is missing. It counts by a fixed text match, so a plan with other numbers gets +0 ~0 -0. A caller cannot see that the named plan file does not exist.

Acceptance criteria

Requirement: Plan counts from the summary line

The devops tf notify-plan command SHALL read the add, change and destroy counts from the plan text.

Scenario: A plan with five adds, three changes and one destroy

  • WHEN the plan file holds Plan: 5 to add, 3 to change, 1 to destroy. and the caller passes --json
  • THEN the JSON output holds "adds": 5, "changes": 3 and "destroys": 1.

Requirement: Counts with an import segment

The command MUST read each count from its own N to <action> segment, whatever the number of segments in the summary line.

Scenario: A plan that imports one resource and changes two

  • WHEN the plan file holds Plan: 1 to import, 0 to add, 2 to change, 0 to destroy. and the caller passes --json
  • THEN the JSON output holds "adds": 0, "changes": 2 and "destroys": 0.

Requirement: A plan with no changes

The command SHALL report zero counts when the plan text holds no change summary.

Scenario: A plan that matches the configuration

  • WHEN the plan file holds No changes. Your infrastructure matches the configuration. and the caller passes --json
  • THEN the JSON output holds "adds": 0, "changes": 0 and "destroys": 0.

Requirement: A plan file that does not exist

The command MUST stop with a non-zero exit code and an error that names the path when --plan-file names a file that does not exist.

Scenario: A missing plan file

  • WHEN the caller runs devops tf notify-plan --plan-file no-such-file.txt --json
  • THEN the exit code is not 0, the error text holds no-such-file.txt, and no plan summary is printed.

Requirement: A plan file is required

The command MUST refuse a call that names no plan file.

Scenario: A call with no plan file

  • WHEN the caller runs devops tf notify-plan --json with no --plan-file
  • THEN the exit code is not 0 and the error text names --plan-file.

Requirement: No sample plan in the printed comment

The command SHALL print only the plan text that the named file holds.

Scenario: The table output for a plan with other counts

  • WHEN the plan file holds Plan: 5 to add, 3 to change, 1 to destroy. and the caller runs the command with no --json
  • THEN the printed comment holds +5 ~3 -1 and holds no +2 ~1 -0.

Existing Tool Considered

OpenTofu (opentofu/opentofu, MPL-2.0, maintained, last activity 2026-11-13) is already in use in the repository, so the verdict is in-use. Its tofu show -json <planfile> renders the planned actions of each resource in resource_changes, so it covers the counting half. It lacks the refusal of a --plan-file path that does not exist; the CLI option gives that refusal with Typer exists=True. It gives no ready-made adds/changes/destroys summary, so the command aggregates the counts and prints its own JSON. It also reads a saved plan file, not the plan output text that this option accepts, so the counts come from the summary line of that text.

Key questions

  • Where does the fixed text match live?: tf_notify_plan in src/devops_cli/commands/tf.py sets adds from a fixed text match at line 843 and changes at line 844, and sets destroys to 0 at line 845. [sources: src/devops_cli/commands/tf.py:817, src/devops_cli/commands/tf.py:843, src/devops_cli/commands/tf.py:845]
  • How does the command get the add, change and destroy counts?: Parse the summary line of the plan text with one regular expression for each count, and take the plan text that the named file holds. (best practice) [sources: src/devops_cli/commands/tf.py:817, src/devops_cli/commands/tf.py:843]
    • Chosen: Parse each N to <action> segment of the plan text. The option reads the plan output text, as the reproduction shows, and this covers plans with an import segment and plans with no changes.
    • Rejected: Run tofu show -json on the plan file. tofu show -json reads a saved plan file, not the plan output text that the option accepts, and the command runs under the no-external-request dry-run class.
    • Rejected: Keep the fixed text match. It reports 0 for each count unless the plan holds the sample numbers, which is the defect.
  • How does the command refuse a --plan-file path that does not exist?: Declare --plan-file as a Path option with Typer exists=True, dir_okay=False and readable=True, so Typer stops the call with exit code 2 and an error that names the path. (best practice) [sources: src/devops_cli/commands/tf.py:817]
    • Chosen: Typer exists=True on the option. Typer checks the path before the command body runs, names the path in the error, and exits non-zero.
    • Rejected: A manual Path.exists() check in the body. It repeats a check that the option type gives, and it needs its own message entry.
  • Is --plan-file required?: Yes. Remove the sample fallback and make the option required, so a call with no plan file stops with a Typer error that names --plan-file. (best practice) [sources: src/devops_cli/commands/tf.py:817, tests/commands/test_tf_notify_plan.py]
    • Chosen: Require --plan-file. The sample plan filled the absent-file case; with it gone the command has no input, and Typer names the missing option.
    • Rejected: Read the plan text from stdin when the option is absent. The issue asks for no new input mode, and it adds a test surface that no requirement covers.
    • Rejected: Keep the sample plan for the absent case. It is the fake output that the issue reports.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    on Nov 12, 2026
  2. added
    type/bug
    status/new
    value/medium
    effort/low
    on Nov 12, 2026
  3. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: none. The title has the scope tf, and .github/labels.yml declares no scope/tf label. Add the label to the file to give the scope to this item.
    • Priority: P2-Medium. The command returns wrong counts and hides a missing file, but it does not crash or corrupt data.
    • Value: Medium. Users who rely on the summary get incorrect numbers and no error for a missing file.
    • Effort: Low. The fix replaces a text match with a JSON parse and adds a file-exists check, both small changes.
  4. added
    priority/p2-medium
    on Nov 12, 2026
  5. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

    Alternatives check: in-use

    Reason: Searches found no maintained Python library that turns a plan file into an adds/changes/destroys summary; the candidates are Go/TypeScript tools (tfplan2md, terraform-plan-parser) or thin binary wrappers (tofupy). OpenTofu itself, whose tofu show -json the issue names, already exposes each resource's actions in resource_changes, so it covers the counting half, but the missing-file refusal and the JSON summary of the command remain with the CLI (Typer exists=True).

    The repository already uses this tool.

    Tool: opentofu/opentofu (https://github.com/opentofu/opentofu)

    • License: MPL-2.0
    • Last activity: 2026-11-13
    • Maintained: yes

    Covers:

    • tofu show -json <planfile> renders the saved plan as JSON, with each resource's planned actions in resource_changes.
    • The add, change and destroy counts can be derived from those actions instead of from a fixed text match.

    Lacks:

    • It does not reject a --plan-file path that does not exist; that check has to come from the CLI option (Typer exists=True).
    • It produces no ready-made adds/changes/destroys summary, so the command must still aggregate the actions and print its own JSON.
    • No Python library was found that wraps plan JSON into such a summary; the plan parsers found are Go/TypeScript or need the plan printed as text.

    Tools considered:

    Searches run:

    • terraform plan parser library open source tool, results: 5
    • python library parse terraform plan JSON resource_changes count add change destroy, results: 5
    • tfplan python package terraform plan parse PyPI, results: 5
    • opentofu show -json plan resource_changes library summarize counts, results: 5
    • "terraform plan" json parser github python, results: 5
    • python-hcl2 terraform plan json resource changes module, results: 5
    • tfplan, results: 5
    • terraform plan json to markdown summary tool github, results: 5
    • terraform-plan-parser, results: 5
    • opentofu show json, results: 5

    Sources:

    Checked on 2026-11-15. A person decides what this means for the item. The check changes no label and closes no issue.

  6. added
    status/ready
    and removed
    status/new
    on Nov 16, 2026
  7. devops-cli-service commented on Nov 16, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at fccb781 (see proposed design in issue body).

  8. added
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 18, 2026
  9. removed
    status/in-review
    on Nov 18, 2026
  10. devops-cli-service commented on Nov 18, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-18 02:58 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumsource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions