mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
Merge remote-tracking branch 'origin/main' into jjs/presets-api
This commit is contained in:
9
coderd/apidoc/docs.go
generated
9
coderd/apidoc/docs.go
generated
@ -13175,6 +13175,15 @@ const docTemplate = `{
|
||||
"$ref": "#/definitions/codersdk.ProvisionerJobStatus"
|
||||
}
|
||||
]
|
||||
},
|
||||
"template_display_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"template_icon": {
|
||||
"type": "string"
|
||||
},
|
||||
"template_name": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
9
coderd/apidoc/swagger.json
generated
9
coderd/apidoc/swagger.json
generated
@ -11900,6 +11900,15 @@
|
||||
"$ref": "#/definitions/codersdk.ProvisionerJobStatus"
|
||||
}
|
||||
]
|
||||
},
|
||||
"template_display_name": {
|
||||
"type": "string"
|
||||
},
|
||||
"template_icon": {
|
||||
"type": "string"
|
||||
},
|
||||
"template_name": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -788,6 +788,7 @@ func New(options *Options) *API {
|
||||
httpmw.AttachRequestID,
|
||||
httpmw.ExtractRealIP(api.RealIPConfig),
|
||||
httpmw.Logger(api.Logger),
|
||||
singleSlashMW,
|
||||
rolestore.CustomRoleMW,
|
||||
prometheusMW,
|
||||
// Build-Version is helpful for debugging.
|
||||
@ -1732,3 +1733,31 @@ func ReadExperiments(log slog.Logger, raw []string) codersdk.Experiments {
|
||||
}
|
||||
return exps
|
||||
}
|
||||
|
||||
var multipleSlashesRe = regexp.MustCompile(`/+`)
|
||||
|
||||
func singleSlashMW(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
var path string
|
||||
rctx := chi.RouteContext(r.Context())
|
||||
if rctx != nil && rctx.RoutePath != "" {
|
||||
path = rctx.RoutePath
|
||||
} else {
|
||||
path = r.URL.Path
|
||||
}
|
||||
|
||||
// Normalize multiple slashes to a single slash
|
||||
newPath := multipleSlashesRe.ReplaceAllString(path, "/")
|
||||
|
||||
// Apply the cleaned path
|
||||
// The approach is consistent with: https://github.com/go-chi/chi/blob/e846b8304c769c4f1a51c9de06bebfaa4576bd88/middleware/strip.go#L24-L28
|
||||
if rctx != nil {
|
||||
rctx.RoutePath = newPath
|
||||
} else {
|
||||
r.URL.Path = newPath
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
|
69
coderd/coderd_internal_test.go
Normal file
69
coderd/coderd_internal_test.go
Normal file
@ -0,0 +1,69 @@
|
||||
package coderd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestStripSlashesMW(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
inputPath string
|
||||
wantPath string
|
||||
}{
|
||||
{"No changes", "/api/v1/buildinfo", "/api/v1/buildinfo"},
|
||||
{"Double slashes", "/api//v2//buildinfo", "/api/v2/buildinfo"},
|
||||
{"Triple slashes", "/api///v2///buildinfo", "/api/v2/buildinfo"},
|
||||
{"Leading slashes", "///api/v2/buildinfo", "/api/v2/buildinfo"},
|
||||
{"Root path", "/", "/"},
|
||||
{"Double slashes root", "//", "/"},
|
||||
{"Only slashes", "/////", "/"},
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
|
||||
t.Run("chi/"+tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
req := httptest.NewRequest("GET", tt.inputPath, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
// given
|
||||
rctx := chi.NewRouteContext()
|
||||
rctx.RoutePath = tt.inputPath
|
||||
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
|
||||
|
||||
// when
|
||||
singleSlashMW(handler).ServeHTTP(rec, req)
|
||||
updatedCtx := chi.RouteContext(req.Context())
|
||||
|
||||
// then
|
||||
assert.Equal(t, tt.inputPath, req.URL.Path)
|
||||
assert.Equal(t, tt.wantPath, updatedCtx.RoutePath)
|
||||
})
|
||||
|
||||
t.Run("stdlib/"+tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
req := httptest.NewRequest("GET", tt.inputPath, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
// when
|
||||
singleSlashMW(handler).ServeHTTP(rec, req)
|
||||
|
||||
// then
|
||||
assert.Equal(t, tt.wantPath, req.URL.Path)
|
||||
assert.Nil(t, chi.RouteContext(req.Context()))
|
||||
})
|
||||
}
|
||||
}
|
@ -5742,7 +5742,10 @@ SELECT
|
||||
current_job.id AS current_job_id,
|
||||
current_job.job_status AS current_job_status,
|
||||
previous_job.id AS previous_job_id,
|
||||
previous_job.job_status AS previous_job_status
|
||||
previous_job.job_status AS previous_job_status,
|
||||
COALESCE(tmpl.name, ''::text) AS current_job_template_name,
|
||||
COALESCE(tmpl.display_name, ''::text) AS current_job_template_display_name,
|
||||
COALESCE(tmpl.icon, ''::text) AS current_job_template_icon
|
||||
FROM
|
||||
provisioner_daemons pd
|
||||
JOIN
|
||||
@ -5767,6 +5770,10 @@ LEFT JOIN
|
||||
LIMIT 1
|
||||
)
|
||||
)
|
||||
LEFT JOIN
|
||||
template_versions version ON version.id = (current_job.input->>'template_version_id')::uuid
|
||||
LEFT JOIN
|
||||
templates tmpl ON tmpl.id = version.template_id
|
||||
WHERE
|
||||
pd.organization_id = $2::uuid
|
||||
AND (COALESCE(array_length($3::uuid[], 1), 0) = 0 OR pd.id = ANY($3::uuid[]))
|
||||
@ -5783,13 +5790,16 @@ type GetProvisionerDaemonsWithStatusByOrganizationParams struct {
|
||||
}
|
||||
|
||||
type GetProvisionerDaemonsWithStatusByOrganizationRow struct {
|
||||
ProvisionerDaemon ProvisionerDaemon `db:"provisioner_daemon" json:"provisioner_daemon"`
|
||||
Status ProvisionerDaemonStatus `db:"status" json:"status"`
|
||||
KeyName string `db:"key_name" json:"key_name"`
|
||||
CurrentJobID uuid.NullUUID `db:"current_job_id" json:"current_job_id"`
|
||||
CurrentJobStatus NullProvisionerJobStatus `db:"current_job_status" json:"current_job_status"`
|
||||
PreviousJobID uuid.NullUUID `db:"previous_job_id" json:"previous_job_id"`
|
||||
PreviousJobStatus NullProvisionerJobStatus `db:"previous_job_status" json:"previous_job_status"`
|
||||
ProvisionerDaemon ProvisionerDaemon `db:"provisioner_daemon" json:"provisioner_daemon"`
|
||||
Status ProvisionerDaemonStatus `db:"status" json:"status"`
|
||||
KeyName string `db:"key_name" json:"key_name"`
|
||||
CurrentJobID uuid.NullUUID `db:"current_job_id" json:"current_job_id"`
|
||||
CurrentJobStatus NullProvisionerJobStatus `db:"current_job_status" json:"current_job_status"`
|
||||
PreviousJobID uuid.NullUUID `db:"previous_job_id" json:"previous_job_id"`
|
||||
PreviousJobStatus NullProvisionerJobStatus `db:"previous_job_status" json:"previous_job_status"`
|
||||
CurrentJobTemplateName string `db:"current_job_template_name" json:"current_job_template_name"`
|
||||
CurrentJobTemplateDisplayName string `db:"current_job_template_display_name" json:"current_job_template_display_name"`
|
||||
CurrentJobTemplateIcon string `db:"current_job_template_icon" json:"current_job_template_icon"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) GetProvisionerDaemonsWithStatusByOrganization(ctx context.Context, arg GetProvisionerDaemonsWithStatusByOrganizationParams) ([]GetProvisionerDaemonsWithStatusByOrganizationRow, error) {
|
||||
@ -5824,6 +5834,9 @@ func (q *sqlQuerier) GetProvisionerDaemonsWithStatusByOrganization(ctx context.C
|
||||
&i.CurrentJobStatus,
|
||||
&i.PreviousJobID,
|
||||
&i.PreviousJobStatus,
|
||||
&i.CurrentJobTemplateName,
|
||||
&i.CurrentJobTemplateDisplayName,
|
||||
&i.CurrentJobTemplateIcon,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -44,7 +44,10 @@ SELECT
|
||||
current_job.id AS current_job_id,
|
||||
current_job.job_status AS current_job_status,
|
||||
previous_job.id AS previous_job_id,
|
||||
previous_job.job_status AS previous_job_status
|
||||
previous_job.job_status AS previous_job_status,
|
||||
COALESCE(tmpl.name, ''::text) AS current_job_template_name,
|
||||
COALESCE(tmpl.display_name, ''::text) AS current_job_template_display_name,
|
||||
COALESCE(tmpl.icon, ''::text) AS current_job_template_icon
|
||||
FROM
|
||||
provisioner_daemons pd
|
||||
JOIN
|
||||
@ -69,6 +72,10 @@ LEFT JOIN
|
||||
LIMIT 1
|
||||
)
|
||||
)
|
||||
LEFT JOIN
|
||||
template_versions version ON version.id = (current_job.input->>'template_version_id')::uuid
|
||||
LEFT JOIN
|
||||
templates tmpl ON tmpl.id = version.template_id
|
||||
WHERE
|
||||
pd.organization_id = @organization_id::uuid
|
||||
AND (COALESCE(array_length(@ids::uuid[], 1), 0) = 0 OR pd.id = ANY(@ids::uuid[]))
|
||||
|
@ -59,8 +59,11 @@ func (api *API) provisionerDaemons(rw http.ResponseWriter, r *http.Request) {
|
||||
var currentJob, previousJob *codersdk.ProvisionerDaemonJob
|
||||
if dbDaemon.CurrentJobID.Valid {
|
||||
currentJob = &codersdk.ProvisionerDaemonJob{
|
||||
ID: dbDaemon.CurrentJobID.UUID,
|
||||
Status: codersdk.ProvisionerJobStatus(dbDaemon.CurrentJobStatus.ProvisionerJobStatus),
|
||||
ID: dbDaemon.CurrentJobID.UUID,
|
||||
Status: codersdk.ProvisionerJobStatus(dbDaemon.CurrentJobStatus.ProvisionerJobStatus),
|
||||
TemplateName: dbDaemon.CurrentJobTemplateName,
|
||||
TemplateIcon: dbDaemon.CurrentJobTemplateIcon,
|
||||
TemplateDisplayName: dbDaemon.CurrentJobTemplateDisplayName,
|
||||
}
|
||||
}
|
||||
if dbDaemon.PreviousJobID.Valid {
|
||||
|
Reference in New Issue
Block a user