Skip to content

feat: add JSON output to taskrun delete - #3086

Open
Debashich wants to merge 1 commit into
tektoncd:mainfrom
Debashich:feat/consistent-delete
Open

feat: add JSON output to taskrun delete#3086
Debashich wants to merge 1 commit into
tektoncd:mainfrom
Debashich:feat/consistent-delete

Conversation

@Debashich

@Debashich Debashich commented Aug 3, 2026

Copy link
Copy Markdown

Changes

Part of #2850

Add consistent -o/--output json support to taskrun delete, including machine-readable output of successfully deleted TaskRuns. Interactive confirmation prompts are suppressed when JSON output is requested.

Extend the shared deleter package 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/deleter passes.

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

See [the contribution guide](https://github.com/tektoncd/cli/blob/master/CONTRIBUTING.md)
for more details.

Release Notes

@tekton-robot tekton-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Aug 3, 2026
@tekton-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please assign pratap0007 after the PR has been reviewed.
You can assign the PR to them by writing /assign @pratap0007 in a comment when ready.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Aug 3, 2026
@Debashich
Debashich marked this pull request as ready for review August 3, 2026 22:22
@tekton-robot tekton-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 3, 2026
@tekton-robot
tekton-robot requested a review from pratap0007 August 3, 2026 22:22
@divyansh42
divyansh42 requested a lite review from Copilot August 28, 2026 10:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/deleter with 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.

Comment thread pkg/deleter/deleter.go
Comment thread pkg/cmd/taskrun/delete.go Outdated
Comment thread pkg/cmd/taskrun/delete.go
Comment thread pkg/cmd/taskrun/delete.go Outdated
Comment thread pkg/cmd/taskrun/delete.go Outdated
Comment thread pkg/cmd/taskrun/delete.go
Comment thread pkg/cmd/taskrun/delete_test.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 CheckOptions entirely, but that method performs validation before prompting. As a result, -o json accepts invalid destructive combinations such as named TaskRuns with --all/--keep, and even an invocation with neither names nor --all succeeds 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 return d.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 --keep cases above because they return after printing human-readable status text. For example, when --keep equals the number of matching TaskRuns, -o json does not produce JSON at all. Route those early exits through the JSON renderer and return an empty deleted array.
	if output == "json" {

pkg/cmd/taskrun/delete.go:237

  • The --task deletion path records deleted TaskRun names in successfulRelatedDeletes, so this field becomes null even when TaskRuns were successfully deleted. Include both direct and related successes in the JSON result.
			Deleted: d.SuccessfulDeletes(),

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 json mode this function currently returns after encoding JSON, so any accumulated deletion errors from d.Errors() are dropped (command can exit 0 even when deletes failed). Also, some early returns in the --task/--keep paths print human output and bypass JSON entirely, and when nothing is deleted the current slice construction can encode as null instead of []. Consider (1) emitting JSON for the early-return paths, (2) always encoding an empty array when nothing was deleted, and (3) returning d.Errors() after successfully writing JSON.
	if output == "json" {
		result := struct {
			Deleted []string `json:"deleted"`
		}{
			Deleted: append(append([]string(nil), d.SuccessfulRelatedDeletes()...), d.SuccessfulDeletes()...),

Comment thread pkg/deleter/deleter.go
@tekton-robot tekton-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 28, 2026
@Debashich
Debashich requested a balanced review from Copilot August 28, 2026 18:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Signed-off-by: Debashich <debashishsinha555@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/json emits {"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()...),

Comment thread pkg/cmd/taskrun/delete.go
Comment on lines +233 to +237
} 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
}
Comment thread pkg/cmd/taskrun/delete.go
Comment on lines +132 to +137
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants