From a49994defa653c0375664a83e162ee857cb3ee59 Mon Sep 17 00:00:00 2001 From: Kynan Ware <47394200+BagToad@users.noreply.github.com> Date: Fri, 29 Aug 2025 17:52:27 -0600 Subject: [PATCH 1/2] Add limit flag to agent-task list command Introduces a --limit flag to control the maximum number of agent tasks fetched. Validates that the limit is greater than zero and updates tests to cover custom and invalid limit scenarios. --- pkg/cmd/agent-task/list/list.go | 5 +++ pkg/cmd/agent-task/list/list_test.go | 46 +++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/pkg/cmd/agent-task/list/list.go b/pkg/cmd/agent-task/list/list.go index 64c9e7d33..767ad2cfc 100644 --- a/pkg/cmd/agent-task/list/list.go +++ b/pkg/cmd/agent-task/list/list.go @@ -44,6 +44,9 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman if f != nil { opts.BaseRepo = f.BaseRepo } + if opts.Limit < 1 { + return cmdutil.FlagErrorf("invalid limit: %v", opts.Limit) + } if runF != nil { return runF(opts) } @@ -55,6 +58,8 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman cmdutil.EnableRepoOverride(cmd, f) } + cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of agent tasks to fetch") + opts.CapiClient = func() (*capi.CAPIClient, error) { cfg, err := opts.Config() if err != nil { diff --git a/pkg/cmd/agent-task/list/list_test.go b/pkg/cmd/agent-task/list/list_test.go index a653d51be..f05d61b9e 100644 --- a/pkg/cmd/agent-task/list/list_test.go +++ b/pkg/cmd/agent-task/list/list_test.go @@ -3,6 +3,7 @@ package list import ( "errors" "net/http" + "strings" "testing" "time" @@ -23,6 +24,7 @@ func TestNewCmdList(t *testing.T) { name string args string wantOpts ListOptions + wantErr string }{ { name: "no arguments", @@ -30,18 +32,35 @@ func TestNewCmdList(t *testing.T) { Limit: defaultLimit, }, }, + { + name: "custom limit", + args: "--limit 15", + wantOpts: ListOptions{ + Limit: 15, + }, + }, + { + name: "invalid limit", + args: "--limit 0", + wantErr: "invalid limit: 0", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { f := &cmdutil.Factory{} var gotOpts *ListOptions - cmd := NewCmdList(f, func(opts *ListOptions) error { - gotOpts = opts - return nil - }) - cmd.ExecuteC() - + cmd := NewCmdList(f, func(opts *ListOptions) error { gotOpts = opts; return nil }) + if tt.args != "" { + cmd.SetArgs(strings.Split(tt.args, " ")) + } + _, err := cmd.ExecuteC() + if tt.wantErr != "" { + require.Error(t, err) + assert.Contains(t, err.Error(), tt.wantErr) + return + } + require.NoError(t, err) assert.Equal(t, tt.wantOpts.Limit, gotOpts.Limit) }) } @@ -58,6 +77,7 @@ func Test_listRun(t *testing.T) { stubs func(*httpmock.Registry) baseRepo ghrepo.Interface baseRepoErr error + limit int wantOut string wantErr error }{ @@ -67,6 +87,18 @@ func Test_listRun(t *testing.T) { stubs: func(reg *httpmock.Registry) { registerEmptySessionsMock(reg) }, wantOut: "no agent tasks found\n", }, + { + name: "limit truncates sessions", + tty: true, + limit: 3, + stubs: func(reg *httpmock.Registry) { registerManySessionsMock(reg, createdAt) }, + wantOut: heredoc.Doc(` + SESSION ID PULL REQUEST REPO SESSION STATE CREATED + s1 #101 OWNER/REPO completed about 6 hours ago + s2 #102 OWNER/REPO failed about 6 hours ago + s3 #103 OWNER/REPO in_progress about 6 hours ago + `), + }, { name: "single session (tty)", tty: true, @@ -155,7 +187,7 @@ func Test_listRun(t *testing.T) { opts := &ListOptions{ IO: ios, Config: func() (gh.Config, error) { return cfg, nil }, - Limit: 30, + Limit: tt.limit, CapiClient: func() (*capi.CAPIClient, error) { return capiClient, nil }, } if tt.baseRepo != nil || tt.baseRepoErr != nil { From 83c597ff533bd5204333f10bf0d344da6a5d459f Mon Sep 17 00:00:00 2001 From: Kynan Ware <47394200+BagToad@users.noreply.github.com> Date: Fri, 29 Aug 2025 17:56:04 -0600 Subject: [PATCH 2/2] Show default limit in agent-task list flag help Updates the help text for the --limit flag in the agent-task list command to display the default value, improving clarity for users. --- pkg/cmd/agent-task/list/list.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/agent-task/list/list.go b/pkg/cmd/agent-task/list/list.go index 767ad2cfc..43f409ffe 100644 --- a/pkg/cmd/agent-task/list/list.go +++ b/pkg/cmd/agent-task/list/list.go @@ -58,7 +58,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman cmdutil.EnableRepoOverride(cmd, f) } - cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, "Maximum number of agent tasks to fetch") + cmd.Flags().IntVarP(&opts.Limit, "limit", "L", defaultLimit, fmt.Sprintf("Maximum number of agent tasks to fetch (default %d)", defaultLimit)) opts.CapiClient = func() (*capi.CAPIClient, error) { cfg, err := opts.Config()