Files
Yonghun Shin 2ef5b7623b fix(packages): generic package deletion ignores terraform state lock and latest version (#40302)
`IsLocked` and `IsLatest` in `services/packages/terraform/state.go`
return early when the package **is** a terraform state (`==` instead of
`!=`), so the `OnBeforeRemovePackageAll` /
`OnBeforeRemovePackageVersion` guards never fire. As a result, deleting
through the generic package API (`DELETE
/api/v1/packages/{owner}/terraform/{name}[/{version}]`) or the web UI
ignores the state lock and allows removing the latest state version,
while the terraform state endpoints reject both.

Changes:

- Flip the type check in `IsLocked` and `IsLatest`.
- `DeleteState` (terraform state `DELETE` endpoint) relied on the bug:
after checking the lock it removed every version one by one with
`RemovePackageVersion`, including the latest. With the guard fixed, it
failed with 500 partway through and left a half-deleted state (the
existing `TestPackageTerraform` subtests catch this). It now calls
`RemovePackage`, which checks the lock and deletes everything in one
transaction.
- The generic package delete API used `APIErrorInternal` for every
error, so the guard errors became 500. It now uses `APIErrorAuto`, which
returns 422 for these.
- Added a `GenericPackageAPI` subtest to `TestPackageTerraform`:
deleting an older version works, deleting the latest version or a locked
state returns 422, and deleting an unlocked state works. It fails on
current `main`.

Tested locally with `go test -run '^TestPackage' ./tests/integration`
(SQLite): all pass.

AI disclosure: I used an AI assistant (Claude) to find the bug, trace
the code paths, and draft the change and this description. I reviewed
the change and ran the tests myself.

Signed-off-by: choam2426 <choam2426@gmail.com>
2026-11-10 17:10:59 +00:00
..
…