Add --json support to gh agent-task view#12807
Conversation
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]>
Co-authored-by: Copilot <[email protected]>
- 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]>
There was a problem hiding this comment.
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--templateflags togh agent-task viewand route output throughcmdutil.Exporterwhen enabled. - Define
capi.SessionFieldsand implement(*capi.Session).ExportDatato 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.
pkg/cmd/agent-task/view/view.go
Outdated
| @@ -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) | |||
| } | |||
There was a problem hiding this comment.
--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)
}
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
I think it should be a cmdutil.MutuallyExclusive call though. I think that's simpler than what Copilot suggests.
There was a problem hiding this comment.
yeah I think that's a good call. I'll fire it up
There was a problem hiding this comment.
actually maybe I already did that in https://github.com/cli/cli/pull/12807/changes#diff-5760262b356b8dfb67879a14750425ac861491f46fe9f4112c6f7dd17a3317e7 🤔 yesterday was kind a blur
There was a problem hiding this comment.
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]>
Consistent with completedAt handling. Addresses Copilot review feedback from companion PR cli#12807. Co-authored-by: Copilot <[email protected]>
BagToad
left a comment
There was a problem hiding this comment.
LGTM - thanks for doing this.
Summary
Add
--json,--jq, and--templateflags togh agent-task view, consistent with the pattern used bygh pr view --json,gh issue view --json, etc.When
--jsonis provided, the view command outputs structured JSON instead of human-readable formatted output. This uses the sameExportDatainterface andSessionFieldsas the companion list PR.Available fields
id,name,state,repository,user,createdAt,updatedAt,completedAt,pullRequestNumber,pullRequestUrl,pullRequestTitle,pullRequestStateExample usage
Companion to #12806 — together they address the
--jsonportion of #12805.