Skip to content

Commit 8083d9d

Browse files
authored
fix(cli): attach org option to task create (coder#19554)
Attaches the org context options to the exp task create cmd
1 parent f0cf0ad commit 8083d9d

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

cli/exp_taskcreate.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (r *RootCmd) taskCreate() *serpent.Command {
2323
taskInput string
2424
)
2525

26-
return &serpent.Command{
26+
cmd := &serpent.Command{
2727
Use: "create [template]",
2828
Short: "Create an experimental task",
2929
Middleware: serpent.Chain(
@@ -123,4 +123,6 @@ func (r *RootCmd) taskCreate() *serpent.Command {
123123
return nil
124124
},
125125
}
126+
orgContext.AttachOptions(cmd)
127+
return cmd
126128
}

cli/exp_taskcreate_test.go

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,28 @@ func TestTaskCreate(t *testing.T) {
2929
taskCreatedAt = time.Now()
3030

3131
organizationID = uuid.New()
32+
anotherOrganizationID = uuid.New()
3233
templateID = uuid.New()
3334
templateVersionID = uuid.New()
3435
templateVersionPresetID = uuid.New()
3536
)
3637

37-
templateAndVersionFoundHandler := func(t *testing.T, ctx context.Context, templateName, templateVersionName, presetName, prompt string) http.HandlerFunc {
38+
templateAndVersionFoundHandler := func(t *testing.T, ctx context.Context, orgID uuid.UUID, templateName, templateVersionName, presetName, prompt string) http.HandlerFunc {
3839
t.Helper()
3940

4041
return func(w http.ResponseWriter, r *http.Request) {
4142
switch r.URL.Path {
4243
case "/api/v2/users/me/organizations":
4344
httpapi.Write(ctx, w, http.StatusOK, []codersdk.Organization{
4445
{MinimalOrganization: codersdk.MinimalOrganization{
45-
ID: organizationID,
46+
ID: orgID,
4647
}},
4748
})
48-
case fmt.Sprintf("/api/v2/organizations/%s/templates/my-template/versions/my-template-version", organizationID):
49+
case fmt.Sprintf("/api/v2/organizations/%s/templates/my-template/versions/my-template-version", orgID):
4950
httpapi.Write(ctx, w, http.StatusOK, codersdk.TemplateVersion{
5051
ID: templateVersionID,
5152
})
52-
case fmt.Sprintf("/api/v2/organizations/%s/templates/my-template", organizationID):
53+
case fmt.Sprintf("/api/v2/organizations/%s/templates/my-template", orgID):
5354
httpapi.Write(ctx, w, http.StatusOK, codersdk.Template{
5455
ID: templateID,
5556
ActiveVersionID: templateVersionID,
@@ -94,62 +95,62 @@ func TestTaskCreate(t *testing.T) {
9495
handler func(t *testing.T, ctx context.Context) http.HandlerFunc
9596
}{
9697
{
97-
args: []string{"my-template@my-template-version", "--input", "my custom prompt"},
98+
args: []string{"my-template@my-template-version", "--input", "my custom prompt", "--org", organizationID.String()},
9899
expectOutput: fmt.Sprintf("The task %s has been created at %s!", cliui.Keyword("task-wild-goldfish-27"), cliui.Timestamp(taskCreatedAt)),
99100
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
100-
return templateAndVersionFoundHandler(t, ctx, "my-template", "my-template-version", "", "my custom prompt")
101+
return templateAndVersionFoundHandler(t, ctx, organizationID, "my-template", "my-template-version", "", "my custom prompt")
101102
},
102103
},
103104
{
104-
args: []string{"my-template", "--input", "my custom prompt"},
105+
args: []string{"my-template", "--input", "my custom prompt", "--org", organizationID.String()},
105106
env: []string{"CODER_TASK_TEMPLATE_VERSION=my-template-version"},
106107
expectOutput: fmt.Sprintf("The task %s has been created at %s!", cliui.Keyword("task-wild-goldfish-27"), cliui.Timestamp(taskCreatedAt)),
107108
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
108-
return templateAndVersionFoundHandler(t, ctx, "my-template", "my-template-version", "", "my custom prompt")
109+
return templateAndVersionFoundHandler(t, ctx, organizationID, "my-template", "my-template-version", "", "my custom prompt")
109110
},
110111
},
111112
{
112-
args: []string{"--input", "my custom prompt"},
113+
args: []string{"--input", "my custom prompt", "--org", organizationID.String()},
113114
env: []string{"CODER_TASK_TEMPLATE_NAME=my-template", "CODER_TASK_TEMPLATE_VERSION=my-template-version"},
114115
expectOutput: fmt.Sprintf("The task %s has been created at %s!", cliui.Keyword("task-wild-goldfish-27"), cliui.Timestamp(taskCreatedAt)),
115116
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
116-
return templateAndVersionFoundHandler(t, ctx, "my-template", "my-template-version", "", "my custom prompt")
117+
return templateAndVersionFoundHandler(t, ctx, organizationID, "my-template", "my-template-version", "", "my custom prompt")
117118
},
118119
},
119120
{
120-
env: []string{"CODER_TASK_TEMPLATE_NAME=my-template", "CODER_TASK_TEMPLATE_VERSION=my-template-version", "CODER_TASK_INPUT=my custom prompt"},
121+
env: []string{"CODER_TASK_TEMPLATE_NAME=my-template", "CODER_TASK_TEMPLATE_VERSION=my-template-version", "CODER_TASK_INPUT=my custom prompt", "CODER_ORGANIZATION=" + organizationID.String()},
121122
expectOutput: fmt.Sprintf("The task %s has been created at %s!", cliui.Keyword("task-wild-goldfish-27"), cliui.Timestamp(taskCreatedAt)),
122123
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
123-
return templateAndVersionFoundHandler(t, ctx, "my-template", "my-template-version", "", "my custom prompt")
124+
return templateAndVersionFoundHandler(t, ctx, organizationID, "my-template", "my-template-version", "", "my custom prompt")
124125
},
125126
},
126127
{
127-
args: []string{"my-template", "--input", "my custom prompt"},
128+
args: []string{"my-template", "--input", "my custom prompt", "--org", organizationID.String()},
128129
expectOutput: fmt.Sprintf("The task %s has been created at %s!", cliui.Keyword("task-wild-goldfish-27"), cliui.Timestamp(taskCreatedAt)),
129130
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
130-
return templateAndVersionFoundHandler(t, ctx, "my-template", "", "", "my custom prompt")
131+
return templateAndVersionFoundHandler(t, ctx, organizationID, "my-template", "", "", "my custom prompt")
131132
},
132133
},
133134
{
134-
args: []string{"my-template", "--input", "my custom prompt", "--preset", "my-preset"},
135+
args: []string{"my-template", "--input", "my custom prompt", "--preset", "my-preset", "--org", organizationID.String()},
135136
expectOutput: fmt.Sprintf("The task %s has been created at %s!", cliui.Keyword("task-wild-goldfish-27"), cliui.Timestamp(taskCreatedAt)),
136137
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
137-
return templateAndVersionFoundHandler(t, ctx, "my-template", "", "my-preset", "my custom prompt")
138+
return templateAndVersionFoundHandler(t, ctx, organizationID, "my-template", "", "my-preset", "my custom prompt")
138139
},
139140
},
140141
{
141142
args: []string{"my-template", "--input", "my custom prompt"},
142143
env: []string{"CODER_TASK_PRESET_NAME=my-preset"},
143144
expectOutput: fmt.Sprintf("The task %s has been created at %s!", cliui.Keyword("task-wild-goldfish-27"), cliui.Timestamp(taskCreatedAt)),
144145
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
145-
return templateAndVersionFoundHandler(t, ctx, "my-template", "", "my-preset", "my custom prompt")
146+
return templateAndVersionFoundHandler(t, ctx, organizationID, "my-template", "", "my-preset", "my custom prompt")
146147
},
147148
},
148149
{
149150
args: []string{"my-template", "--input", "my custom prompt", "--preset", "not-real-preset"},
150151
expectError: `preset "not-real-preset" not found`,
151152
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
152-
return templateAndVersionFoundHandler(t, ctx, "my-template", "", "my-preset", "my custom prompt")
153+
return templateAndVersionFoundHandler(t, ctx, organizationID, "my-template", "", "my-preset", "my custom prompt")
153154
},
154155
},
155156
{
@@ -173,7 +174,7 @@ func TestTaskCreate(t *testing.T) {
173174
},
174175
},
175176
{
176-
args: []string{"not-real-template", "--input", "my custom prompt"},
177+
args: []string{"not-real-template", "--input", "my custom prompt", "--org", organizationID.String()},
177178
expectError: httpapi.ResourceNotFoundResponse.Message,
178179
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
179180
return func(w http.ResponseWriter, r *http.Request) {
@@ -192,6 +193,40 @@ func TestTaskCreate(t *testing.T) {
192193
}
193194
},
194195
},
196+
{
197+
args: []string{"template-in-different-org", "--input", "my-custom-prompt", "--org", anotherOrganizationID.String()},
198+
expectError: httpapi.ResourceNotFoundResponse.Message,
199+
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
200+
return func(w http.ResponseWriter, r *http.Request) {
201+
switch r.URL.Path {
202+
case "/api/v2/users/me/organizations":
203+
httpapi.Write(ctx, w, http.StatusOK, []codersdk.Organization{
204+
{MinimalOrganization: codersdk.MinimalOrganization{
205+
ID: anotherOrganizationID,
206+
}},
207+
})
208+
case fmt.Sprintf("/api/v2/organizations/%s/templates/template-in-different-org", anotherOrganizationID):
209+
httpapi.ResourceNotFound(w)
210+
default:
211+
t.Errorf("unexpected path: %s", r.URL.Path)
212+
}
213+
}
214+
},
215+
},
216+
{
217+
args: []string{"no-org", "--input", "my-custom-prompt"},
218+
expectError: "Must select an organization with --org=<org_name>",
219+
handler: func(t *testing.T, ctx context.Context) http.HandlerFunc {
220+
return func(w http.ResponseWriter, r *http.Request) {
221+
switch r.URL.Path {
222+
case "/api/v2/users/me/organizations":
223+
httpapi.Write(ctx, w, http.StatusOK, []codersdk.Organization{})
224+
default:
225+
t.Errorf("unexpected path: %s", r.URL.Path)
226+
}
227+
}
228+
},
229+
},
195230
}
196231

197232
for _, tt := range tests {

0 commit comments

Comments
 (0)