Skip to content

Add --json support to gh agent-task view#12807

Merged
BagToad merged 4 commits intocli:trunkfrom
maxbeizer:agent-task-view-json
Mar 2, 2026
Merged

Add --json support to gh agent-task view#12807
BagToad merged 4 commits intocli:trunkfrom
maxbeizer:agent-task-view-json

Conversation

@maxbeizer
Copy link
Contributor

@maxbeizer maxbeizer commented Feb 28, 2026

Summary

Add --json, --jq, and --template flags to gh agent-task view, consistent with the pattern used by gh pr view --json, gh issue view --json, etc.

When --json is provided, the view command outputs structured JSON instead of human-readable formatted output. This uses the same ExportData interface and SessionFields as the companion list PR.

Available fields

id, name, state, repository, user, createdAt, updatedAt, completedAt, pullRequestNumber, pullRequestUrl, pullRequestTitle, pullRequestState

Example usage

# Get session details as JSON
gh agent-task view <session-id> --json id,name,state,pullRequestUrl

# Filter with jq
gh agent-task view <session-id> --json state --jq '.state'

# Use a Go template
gh agent-task view <session-id> --json name,state --template '{{.name}}: {{.state}}'

Companion to #12806 — together they address the --json portion of #12805.

Add --json, --jq, and --template flags to `gh agent-task view`,
consistent with the pattern used by `gh pr view --json`,
`gh issue view --json`, etc.

This reuses the same ExportData interface and SessionFields defined
for list, applying them to the single-session view output.

Closes cli#12805 (partial)

Co-authored-by: Copilot <[email protected]>
@github-actions github-actions bot added external pull request originating outside of the CLI core team needs-triage needs to be reviewed labels Feb 28, 2026
maxbeizer and others added 2 commits March 1, 2026 08:57
- Rename 'status' field to 'state' for consistency with struct and UI
- Add missing JSON fields: completedAt, user, pullRequestTitle, pullRequestState
- Add test for nil PullRequest with --json
- Expand existing JSON test to cover new fields

Co-authored-by: Copilot <[email protected]>
@maxbeizer maxbeizer marked this pull request as ready for review March 1, 2026 15:17
@maxbeizer maxbeizer requested a review from a team as a code owner March 1, 2026 15:17
@maxbeizer maxbeizer requested review from babakks and Copilot March 1, 2026 15:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds structured output support to gh agent-task view by wiring the standard --json/--jq/--template flags into the command and making capi.Session exportable via the existing JSON exporter mechanism.

Changes:

  • Add --json, --jq, and --template flags to gh agent-task view and route output through cmdutil.Exporter when enabled.
  • Define capi.SessionFields and implement (*capi.Session).ExportData to provide a stable JSON schema for sessions.
  • Add tests covering JSON export output for a populated session and a session with a nil pull request.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
pkg/cmd/agent-task/view/view.go Adds exporter plumbing + JSON flags to the view command and emits JSON when enabled.
pkg/cmd/agent-task/capi/sessions.go Introduces SessionFields and ExportData implementation for JSON export of sessions.
pkg/cmd/agent-task/view/view_test.go Adds unit tests validating JSON output behavior from viewRun.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +291 to +297
@@ -289,6 +292,10 @@ func viewRun(opts *ViewOptions) error {
return printLogs(opts, capiClient, session.ID)
}

if opts.Exporter != nil {
return opts.Exporter.Write(opts.IO, session)
}
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

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

--json can currently be combined with --log/--follow, but in that case the command will print logs (non-JSON) because the opts.Log branch runs before the exporter. This makes --json effectively ignored for that flag combination and is inconsistent with other commands where JSON output takes precedence. Consider either (1) erroring when --json is used with --log/--follow, or (2) prioritizing opts.Exporter output over the log path (and documenting/handling what happens to --log).

See below for a potential fix:

	if opts.Exporter != nil {
		return opts.Exporter.Write(opts.IO, session)
	}

	if opts.Log {
		return printLogs(opts, capiClient, session.ID)
	}

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

@maxbeizer I think we should do this one. I think it makes sense to guard here to communicate how we intend this flag to be used - wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

I think it should be a cmdutil.MutuallyExclusive call though. I think that's simpler than what Copilot suggests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah I think that's a good call. I'll fire it up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

ah yeah I missed that too. I think we're good then 🙌

- Prioritize --json output over --log/--follow so JSON is not silently ignored
- Emit null for zero createdAt/updatedAt values, consistent with completedAt

Co-authored-by: Copilot <[email protected]>
maxbeizer added a commit to maxbeizer/cli that referenced this pull request Mar 1, 2026
Consistent with completedAt handling. Addresses Copilot review feedback
from companion PR cli#12807.

Co-authored-by: Copilot <[email protected]>
Copy link
Member

@BagToad BagToad left a comment

Choose a reason for hiding this comment

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

LGTM - thanks for doing this.

@BagToad BagToad merged commit ad41bb9 into cli:trunk Mar 2, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team needs-triage needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants