feat: add JSON output to taskrun delete - #3086
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Pull request overview
This PR adds -o/--output json support to tkn taskrun delete, enabling machine-readable output of deleted TaskRuns and extending the shared pkg/deleter helper with accessors for successful deletions (plus unit tests).
Changes:
- Added JSON output path for
taskrun delete, and suppressed interactive confirmation when JSON is requested. - Extended
pkg/deleterwith accessors for successful deletes / related deletes. - Added unit tests for the new deleter accessors and JSON output for
taskrun delete.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| pkg/deleter/deleter.go | Adds accessors for successful deletes and successful related deletes. |
| pkg/deleter/deleter_test.go | Adds unit tests validating the new deleter accessors. |
| pkg/cmd/taskrun/delete.go | Implements JSON output mode and adjusts delete flow to suppress prompts in JSON mode. |
| pkg/cmd/taskrun/delete_test.go | Adds tests asserting JSON output for taskrun delete. |
Suppressed comments (1)
pkg/deleter/deleter.go:113
- SuccessfulRelatedDeletes also returns the backing slice directly, which allows external mutation of Deleter state. Returning a copy prevents subtle bugs when callers append/modify the returned slice.
func (d *Deleter) SuccessfulRelatedDeletes() []string {
return d.successfulRelatedDeletes
}
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Suppressed comments (5)
pkg/cmd/taskrun/delete.go:135
- JSON mode skips
CheckOptionsentirely, but that method performs validation before prompting. As a result,-o jsonaccepts invalid destructive combinations such as named TaskRuns with--all/--keep, and even an invocation with neither names nor--allsucceeds with an empty result. Keep the validation and suppress only the confirmation prompt.
if output != "json" {
if err := opts.CheckOptions(s, availableTrs, p.Namespace()); err != nil {
return err
}
}
pkg/cmd/taskrun/delete.go:190
- With
--force -o json, a TaskRun owned by a running PipelineRun writes this warning to stdout before the JSON document, making stdout invalid JSON. Send the warning to stderr so stdout remains machine-readable.
if !prFinished && !opts.ForceDelete && output != "json" {
pkg/cmd/taskrun/delete.go:240
- This returns success immediately after encoding and drops any deletion errors accumulated by
Deleter. A delete that fails after lookup (for example, due to a race or API error) is therefore reported as a successful command with only the failed name omitted; encode the result, then returnd.Errors().
return json.NewEncoder(s.Out).Encode(result)
pkg/cmd/taskrun/delete.go:233
- The JSON branch is unreachable for the two no-op
--task --keepcases above because they return after printing human-readable status text. For example, when--keepequals the number of matching TaskRuns,-o jsondoes not produce JSON at all. Route those early exits through the JSON renderer and return an emptydeletedarray.
if output == "json" {
pkg/cmd/taskrun/delete.go:237
- The
--taskdeletion path records deleted TaskRun names insuccessfulRelatedDeletes, so this field becomesnulleven when TaskRuns were successfully deleted. Include both direct and related successes in the JSON result.
Deleted: d.SuccessfulDeletes(),
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
Suppressed comments (1)
pkg/cmd/taskrun/delete.go:243
- In
-o jsonmode this function currently returns after encoding JSON, so any accumulated deletion errors fromd.Errors()are dropped (command can exit 0 even when deletes failed). Also, some early returns in the--task/--keeppaths print human output and bypass JSON entirely, and when nothing is deleted the current slice construction can encode asnullinstead of[]. Consider (1) emitting JSON for the early-return paths, (2) always encoding an empty array when nothing was deleted, and (3) returningd.Errors()after successfully writing JSON.
if output == "json" {
result := struct {
Deleted []string `json:"deleted"`
}{
Deleted: append(append([]string(nil), d.SuccessfulRelatedDeletes()...), d.SuccessfulDeletes()...),
Signed-off-by: Debashich <debashishsinha555@gmail.com>
57de078 to
019d0a0
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
Suppressed comments (1)
pkg/cmd/taskrun/delete.go:246
- When neither accessor contains an item, these nested appends preserve a nil slice, so
encoding/jsonemits{"deleted":null}rather than the{"deleted":[]}contract asserted by the new no-op test. Initialize the accumulation with a non-nil empty slice so no-op deletions still produce an array.
Deleted: append(append([]string(nil), d.SuccessfulRelatedDeletes()...), d.SuccessfulDeletes()...),
| } else if opts.Keep > len(trToKeep) { | ||
| if output != "json" { | ||
| fmt.Fprintf(s.Out, "There is/are only %d %s(s) associated for %s: %s \n", len(trToKeep), opts.Resource, opts.ParentResource, opts.ParentResourceName) | ||
| return nil | ||
| } |
| if output == "json" { | ||
| checkStreams = &cli.Stream{In: strings.NewReader("y\n"), Out: &strings.Builder{}, Err: s.Err} | ||
| } | ||
| if err := opts.CheckOptions(checkStreams, availableTrs, p.Namespace()); err != nil { | ||
| return err | ||
| } |
Changes
Part of #2850
Add consistent
-o/--output jsonsupport totaskrun delete, including machine-readable output of successfully deleted TaskRuns. Interactive confirmation prompts are suppressed when JSON output is requested.Extend the shared
deleterpackage with accessors for successful and related deletions, with corresponding unit tests.Verified the relevant TaskRun delete/list output paths and existing test coverage.
go test ./pkg/cmd/taskrun ./pkg/deleterpasses.Submitter Checklist
These are the criteria that every PR should meet, please check them off as you
review them:
make checkmake generatedSee [the contribution guide](https://github.com/tektoncd/cli/blob/master/CONTRIBUTING.md)
for more details.
Release Notes