Skip to content

Commit cb9808f

Browse files
committed
fix(agent-task create): avoid prompting when problem statement is provided
Signed-off-by: Babak K. Shandiz <babakks@github.com>
1 parent 2116327 commit cb9808f

2 files changed

Lines changed: 65 additions & 89 deletions

File tree

pkg/cmd/agent-task/create/create.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,30 @@ func createRun(opts *CreateOptions) error {
121121
}
122122

123123
if opts.ProblemStatement == "" {
124-
// Load initial problem statement from file, if provided
125124
if opts.ProblemStatementFile != "" {
126125
fileContent, err := cmdutil.ReadFile(opts.ProblemStatementFile, opts.IO.In)
127126
if err != nil {
128-
return cmdutil.FlagErrorf("could not read task description file: %v", err)
127+
return fmt.Errorf("could not read task description file: %w", err)
128+
}
129+
130+
trimmed := strings.TrimSpace(string(fileContent))
131+
if trimmed == "" {
132+
return errors.New("task description file cannot be empty")
129133
}
130-
opts.ProblemStatement = strings.TrimSpace(string(fileContent))
131-
}
132134

133-
if opts.IO.CanPrompt() {
135+
opts.ProblemStatement = trimmed
136+
} else {
134137
desc, err := opts.Prompter.MarkdownEditor("Enter the task description", opts.ProblemStatement, false)
135138
if err != nil {
136139
return err
137140
}
138-
opts.ProblemStatement = strings.TrimSpace(desc)
139-
}
140-
}
141141

142-
if opts.ProblemStatement == "" {
143-
fmt.Fprintf(opts.IO.ErrOut, "a task description is required.\n")
144-
return cmdutil.SilentError
145-
}
142+
trimmed := strings.TrimSpace(string(desc))
143+
if trimmed == "" {
144+
return errors.New("a task description is required")
145+
}
146146

147-
if opts.IO.CanPrompt() {
148-
confirm, err := opts.Prompter.Confirm("Submit agent task", true)
149-
if err != nil {
150-
return err
151-
}
152-
if !confirm {
153-
return cmdutil.SilentError
147+
opts.ProblemStatement = trimmed
154148
}
155149
}
156150

pkg/cmd/agent-task/create/create_test.go

Lines changed: 52 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -158,72 +158,85 @@ func Test_createRun(t *testing.T) {
158158
wantErrIs error
159159
}{
160160
{
161-
name: "interactive with file prompts to edit with file contents",
161+
name: "interactive, problem statement from arg",
162+
isTTY: true,
163+
opts: &CreateOptions{
164+
BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
165+
ProblemStatement: "task description from arg",
166+
},
167+
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
168+
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
169+
require.Equal(t, "OWNER", owner)
170+
require.Equal(t, "REPO", repo)
171+
require.Equal(t, "task description from arg", problemStatement)
172+
return &createdJobSuccessWithPR, nil
173+
}
174+
},
175+
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
176+
},
177+
{
178+
name: "non-interactive, problem statement from arg",
179+
opts: &CreateOptions{
180+
BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
181+
ProblemStatement: "task description from arg",
182+
},
183+
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
184+
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
185+
require.Equal(t, "OWNER", owner)
186+
require.Equal(t, "REPO", repo)
187+
require.Equal(t, "task description from arg", problemStatement)
188+
return &createdJobSuccessWithPR, nil
189+
}
190+
},
191+
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
192+
},
193+
{
194+
name: "interactive, problem statement from file",
195+
isTTY: true,
162196
opts: &CreateOptions{
163197
BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
164198
ProblemStatement: "",
165199
ProblemStatementFile: taskDescFile,
166-
Prompter: &prompter.PrompterMock{
167-
MarkdownEditorFunc: func(prompt, defaultValue string, blankAllowed bool) (string, error) {
168-
require.Equal(t, "Enter the task description", prompt)
169-
require.Equal(t, "task description from file", defaultValue)
170-
return "edited task description", nil
171-
},
172-
ConfirmFunc: func(message string, defaultValue bool) (bool, error) {
173-
require.Equal(t, "Submit agent task", message)
174-
return true, nil
175-
},
176-
},
177200
},
178-
isTTY: true,
179201
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
180202
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
181203
require.Equal(t, "OWNER", owner)
182204
require.Equal(t, "REPO", repo)
183-
require.Equal(t, "edited task description", problemStatement)
205+
require.Equal(t, "task description from file", problemStatement)
184206
return &createdJobSuccessWithPR, nil
185207
}
186208
},
187209
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
188210
},
189211
{
190-
name: "interactively rejecting confirmation prompt aborts task creation",
212+
name: "non-interactive, problem statement loaded from file",
191213
opts: &CreateOptions{
192-
BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
193-
ProblemStatement: "",
194-
Prompter: &prompter.PrompterMock{
195-
MarkdownEditorFunc: func(prompt, defaultValue string, blankAllowed bool) (string, error) {
196-
require.Equal(t, "Enter the task description", prompt)
197-
return "From editor", nil
198-
},
199-
ConfirmFunc: func(message string, defaultValue bool) (bool, error) {
200-
require.Equal(t, "Submit agent task", message)
201-
return false, nil
202-
},
203-
},
214+
BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
215+
ProblemStatement: "",
216+
ProblemStatementFile: taskDescFile,
217+
},
218+
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
219+
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
220+
require.Equal(t, "OWNER", owner)
221+
require.Equal(t, "REPO", repo)
222+
require.Equal(t, "task description from file", problemStatement)
223+
return &createdJobSuccessWithPR, nil
224+
}
204225
},
205-
isTTY: true,
206-
wantErr: "SilentError",
207-
wantErrIs: cmdutil.SilentError,
208-
wantStdErr: "",
226+
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
209227
},
210228
{
211-
name: "interactively entering task description with editor, no file",
229+
name: "interactive, problem statement from prompt/editor",
212230
isTTY: true,
213231
opts: &CreateOptions{
214232
BaseRepo: func() (ghrepo.Interface, error) {
215233
return ghrepo.New("OWNER", "REPO"), nil
216234
},
217-
ProblemStatement: "",
218235
Prompter: &prompter.PrompterMock{
219236
MarkdownEditorFunc: func(prompt, defaultValue string, blankAllowed bool) (string, error) {
220237
require.Equal(t, "Enter the task description", prompt)
221238
return "From editor", nil
222239
},
223-
ConfirmFunc: func(message string, defaultValue bool) (bool, error) {
224-
require.Equal(t, "Submit agent task", message)
225-
return true, nil
226-
},
227240
},
228241
},
229242
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
@@ -235,7 +248,7 @@ func Test_createRun(t *testing.T) {
235248
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
236249
},
237250
{
238-
name: "empty task description from interactive prompt returns error",
251+
name: "interactive, empty task description from editor returns error",
239252
isTTY: true,
240253
opts: &CreateOptions{
241254
BaseRepo: func() (ghrepo.Interface, error) {
@@ -247,26 +260,7 @@ func Test_createRun(t *testing.T) {
247260
},
248261
},
249262
},
250-
wantErr: "SilentError",
251-
wantErrIs: cmdutil.SilentError,
252-
wantStdErr: "a task description is required.\n",
253-
},
254-
{
255-
name: "problem statement loaded from file non-interactively doesn't prompt or return error",
256-
opts: &CreateOptions{
257-
BaseRepo: func() (ghrepo.Interface, error) { return ghrepo.New("OWNER", "REPO"), nil },
258-
ProblemStatement: "",
259-
ProblemStatementFile: taskDescFile,
260-
},
261-
capiStubs: func(t *testing.T, m *capi.CapiClientMock) {
262-
m.CreateJobFunc = func(ctx context.Context, owner, repo, problemStatement, baseBranch string) (*capi.Job, error) {
263-
require.Equal(t, "OWNER", owner)
264-
require.Equal(t, "REPO", repo)
265-
require.Equal(t, "task description from file", problemStatement)
266-
return &createdJobSuccessWithPR, nil
267-
}
268-
},
269-
wantStdout: "https://github.com/OWNER/REPO/pull/42/agent-sessions/sess1\n",
263+
wantErr: "a task description is required",
270264
},
271265
{
272266
name: "missing repo returns error",
@@ -276,18 +270,6 @@ func Test_createRun(t *testing.T) {
276270
}},
277271
wantErr: "a repository is required; re-run in a repository or supply one with --repo owner/name",
278272
},
279-
{
280-
name: "non-interactive empty description returns error",
281-
opts: &CreateOptions{
282-
BaseRepo: func() (ghrepo.Interface, error) {
283-
return ghrepo.New("OWNER", "REPO"), nil
284-
},
285-
ProblemStatement: "",
286-
},
287-
wantErr: "SilentError",
288-
wantErrIs: cmdutil.SilentError,
289-
wantStdErr: "a task description is required.\n",
290-
},
291273
{
292274
name: "problem statement loaded from arg non-interactively doesn't prompt or return error",
293275
opts: &CreateOptions{

0 commit comments

Comments
 (0)