Add --json support to gh agent-task view

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 https://github.com/cli/cli/issues/12805 (partial)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Max Beizer 2026-02-28 09:22:28 -06:00
parent cf862d65df
commit 7241b42ecf
3 changed files with 101 additions and 0 deletions

View file

@ -102,6 +102,58 @@ type SessionError struct {
Message string
}
// SessionFields defines the available fields for JSON export of a Session.
var SessionFields = []string{
"id",
"name",
"status",
"repository",
"createdAt",
"updatedAt",
"pullRequestNumber",
"pullRequestUrl",
}
// ExportData implements the exportable interface for JSON output.
func (s *Session) ExportData(fields []string) map[string]interface{} {
data := make(map[string]interface{}, len(fields))
for _, f := range fields {
switch f {
case "id":
data[f] = s.ID
case "name":
data[f] = s.Name
case "status":
data[f] = s.State
case "repository":
if s.PullRequest != nil && s.PullRequest.Repository != nil {
data[f] = s.PullRequest.Repository.NameWithOwner
} else {
data[f] = nil
}
case "createdAt":
data[f] = s.CreatedAt
case "updatedAt":
data[f] = s.LastUpdatedAt
case "pullRequestNumber":
if s.PullRequest != nil {
data[f] = s.PullRequest.Number
} else {
data[f] = nil
}
case "pullRequestUrl":
if s.PullRequest != nil {
data[f] = s.PullRequest.URL
} else {
data[f] = nil
}
default:
data[f] = nil
}
}
return data
}
type resource struct {
ID string `json:"id"`
UserID uint64 `json:"user_id"`

View file

@ -37,6 +37,7 @@ type ViewOptions struct {
Finder prShared.PRFinder
Prompter prompter.Prompter
Browser browser.Browser
Exporter cmdutil.Exporter
LogRenderer func() shared.LogRenderer
Sleep func(d time.Duration)
@ -125,6 +126,8 @@ func NewCmdView(f *cmdutil.Factory, runF func(*ViewOptions) error) *cobra.Comman
cmd.Flags().BoolVar(&opts.Log, "log", false, "Show agent session logs")
cmd.Flags().BoolVar(&opts.Follow, "follow", false, "Follow agent session logs")
cmdutil.AddJSONFlags(cmd, &opts.Exporter, capi.SessionFields)
return cmd
}
@ -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)
}
printSession(opts, session)
return nil
}

View file

@ -168,6 +168,7 @@ func Test_viewRun(t *testing.T) {
promptStubs func(*testing.T, *prompter.MockPrompter)
capiStubs func(*testing.T, *capi.CapiClientMock)
logRendererStubs func(*testing.T, *shared.LogRendererMock)
jsonFields []string
wantOut string
wantErr error
wantStderr string
@ -1209,6 +1210,41 @@ func Test_viewRun(t *testing.T) {
(rendered:) <raw-logs-two>
`),
},
{
name: "json output (tty)",
tty: true,
opts: ViewOptions{
SelectorArg: "some-session-id",
SessionID: "some-session-id",
},
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
m.GetSessionFunc = func(_ context.Context, id string) (*capi.Session, error) {
return &capi.Session{
ID: "some-session-id",
Name: "Fix login bug",
State: "completed",
CreatedAt: sampleDate,
LastUpdatedAt: sampleDate,
CompletedAt: sampleCompletedAt,
ResourceType: "pull",
PullRequest: &api.PullRequest{
Number: 42,
URL: "https://github.com/OWNER/REPO/pull/42",
Title: "Fix login bug",
State: "OPEN",
Repository: &api.PRRepository{
NameWithOwner: "OWNER/REPO",
},
},
User: &api.GitHubUser{
Login: "testuser",
},
}, nil
}
},
wantOut: "{\"id\":\"some-session-id\",\"name\":\"Fix login bug\",\"pullRequestNumber\":42,\"pullRequestUrl\":\"https://github.com/OWNER/REPO/pull/42\",\"repository\":\"OWNER/REPO\",\"status\":\"completed\"}\n",
jsonFields: []string{"id", "name", "status", "repository", "pullRequestNumber", "pullRequestUrl"},
},
}
for _, tt := range tests {
@ -1244,6 +1280,12 @@ func Test_viewRun(t *testing.T) {
return logRenderer
}
if tt.jsonFields != nil {
exporter := cmdutil.NewJSONExporter()
exporter.SetFields(tt.jsonFields)
opts.Exporter = exporter
}
err := viewRun(&opts)
if tt.wantErr != nil {
assert.Error(t, err)