Skip to content

Commit d37c3a1

Browse files
committed
Merge remote-tracking branch 'origin/main' into jjs/presets
2 parents 986a467 + edd982e commit d37c3a1

File tree

29 files changed

+329
-250
lines changed

29 files changed

+329
-250
lines changed

cli/cliui/output.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ func (f *OutputFormatter) Format(ctx context.Context, data any) (string, error)
8383
return "", xerrors.Errorf("unknown output format %q", f.formatID)
8484
}
8585

86+
// FormatID will return the ID of the format selected by `--output`.
87+
// If no flag is present, it returns the 'default' formatter.
88+
func (f *OutputFormatter) FormatID() string {
89+
return f.formatID
90+
}
91+
8692
type tableFormat struct {
8793
defaultColumns []string
8894
allColumns []string

cli/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (r *RootCmd) list() *serpent.Command {
112112
return err
113113
}
114114

115-
if len(res) == 0 {
115+
if len(res) == 0 && formatter.FormatID() != cliui.JSONFormat().ID() {
116116
pretty.Fprintf(inv.Stderr, cliui.DefaultStyles.Prompt, "No workspaces found! Create one:\n")
117117
_, _ = fmt.Fprintln(inv.Stderr)
118118
_, _ = fmt.Fprintln(inv.Stderr, " "+pretty.Sprint(cliui.DefaultStyles.Code, "coder create <name>"))

cli/list_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,30 @@ func TestList(t *testing.T) {
7474
require.NoError(t, json.Unmarshal(out.Bytes(), &workspaces))
7575
require.Len(t, workspaces, 1)
7676
})
77+
78+
t.Run("NoWorkspacesJSON", func(t *testing.T) {
79+
t.Parallel()
80+
client := coderdtest.New(t, nil)
81+
owner := coderdtest.CreateFirstUser(t, client)
82+
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
83+
84+
inv, root := clitest.New(t, "list", "--output=json")
85+
clitest.SetupConfig(t, member, root)
86+
87+
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
88+
defer cancelFunc()
89+
90+
stdout := bytes.NewBuffer(nil)
91+
stderr := bytes.NewBuffer(nil)
92+
inv.Stdout = stdout
93+
inv.Stderr = stderr
94+
err := inv.WithContext(ctx).Run()
95+
require.NoError(t, err)
96+
97+
var workspaces []codersdk.Workspace
98+
require.NoError(t, json.Unmarshal(stdout.Bytes(), &workspaces))
99+
require.Len(t, workspaces, 0)
100+
101+
require.Len(t, stderr.Bytes(), 0)
102+
})
77103
}

coderd/apidoc/docs.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ func New(options *Options) *API {
13041304
func(next http.Handler) http.Handler {
13051305
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
13061306
if !api.Authorize(r, policy.ActionRead, rbac.ResourceDebugInfo) {
1307-
httpapi.ResourceNotFound(rw)
1307+
httpapi.Forbidden(rw)
13081308
return
13091309
}
13101310

coderd/database/dbmem/dbmem.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4170,6 +4170,9 @@ func (q *FakeQuerier) GetProvisionerJobsByOrganizationAndStatusWithQueuePosition
41704170
if len(arg.IDs) > 0 && !slices.Contains(arg.IDs, job.ID) {
41714171
continue
41724172
}
4173+
if len(arg.Tags) > 0 && !tagsSubset(job.Tags, arg.Tags) {
4174+
continue
4175+
}
41734176

41744177
row := database.GetProvisionerJobsByOrganizationAndStatusWithQueuePositionAndProvisionerRow{
41754178
ProvisionerJob: rowQP.ProvisionerJob,

coderd/database/queries.sql.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/provisionerjobs.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ WHERE
158158
(sqlc.narg('organization_id')::uuid IS NULL OR pj.organization_id = @organization_id)
159159
AND (COALESCE(array_length(@ids::uuid[], 1), 0) = 0 OR pj.id = ANY(@ids::uuid[]))
160160
AND (COALESCE(array_length(@status::provisioner_job_status[], 1), 0) = 0 OR pj.job_status = ANY(@status::provisioner_job_status[]))
161+
AND (@tags::tagset = 'null'::tagset OR provisioner_tagset_contains(pj.tags::tagset, @tags::tagset))
161162
GROUP BY
162163
pj.id,
163164
qp.queue_position,

coderd/provisionerjobs.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ func (api *API) provisionerJob(rw http.ResponseWriter, r *http.Request) {
7272
// @Tags Organizations
7373
// @Param organization path string true "Organization ID" format(uuid)
7474
// @Param limit query int false "Page limit"
75+
// @Param ids query []string false "Filter results by job IDs" format(uuid)
7576
// @Param status query codersdk.ProvisionerJobStatus false "Filter results by status" enums(pending,running,succeeded,canceling,canceled,failed)
77+
// @Param tags query object false "Provisioner tags to filter by (JSON of the form {'tag1':'value1','tag2':'value2'})"
7678
// @Success 200 {array} codersdk.ProvisionerJob
7779
// @Router /organizations/{organization}/provisionerjobs [get]
7880
func (api *API) provisionerJobs(rw http.ResponseWriter, r *http.Request) {
@@ -103,6 +105,10 @@ func (api *API) handleAuthAndFetchProvisionerJobs(rw http.ResponseWriter, r *htt
103105
p := httpapi.NewQueryParamParser()
104106
limit := p.PositiveInt32(qp, 50, "limit")
105107
status := p.Strings(qp, nil, "status")
108+
if ids == nil {
109+
ids = p.UUIDs(qp, nil, "ids")
110+
}
111+
tagsRaw := p.String(qp, "", "tags")
106112
p.ErrorExcessParams(qp)
107113
if len(p.Errors) > 0 {
108114
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
@@ -112,11 +118,23 @@ func (api *API) handleAuthAndFetchProvisionerJobs(rw http.ResponseWriter, r *htt
112118
return nil, false
113119
}
114120

121+
tags := database.StringMap{}
122+
if tagsRaw != "" {
123+
if err := tags.Scan([]byte(tagsRaw)); err != nil {
124+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
125+
Message: "Invalid tags query parameter",
126+
Detail: err.Error(),
127+
})
128+
return nil, false
129+
}
130+
}
131+
115132
jobs, err := api.Database.GetProvisionerJobsByOrganizationAndStatusWithQueuePositionAndProvisioner(ctx, database.GetProvisionerJobsByOrganizationAndStatusWithQueuePositionAndProvisionerParams{
116133
OrganizationID: uuid.NullUUID{UUID: org.ID, Valid: true},
117134
Status: slice.StringEnums[database.ProvisionerJobStatus](status),
118135
Limit: sql.NullInt32{Int32: limit, Valid: limit > 0},
119136
IDs: ids,
137+
Tags: tags,
120138
})
121139
if err != nil {
122140
if httpapi.Is404Error(err) {

0 commit comments

Comments
 (0)