mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
* Nest jobs under an organization * Rename project parameter to parameter schema * Update references when computing project parameters * Add files endpoint * Allow one-off project import jobs * Allow variables to be injected that are not defined by the schema * Update API to use jobs first * Fix CLI tests * Fix linting * Fix hex length for files table * Reduce memory allocation for windows
68 lines
2.1 KiB
Go
68 lines
2.1 KiB
Go
package httpmw
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/coder/coder/database"
|
|
"github.com/coder/coder/httpapi"
|
|
)
|
|
|
|
type projectVersionParamContextKey struct{}
|
|
|
|
// ProjectVersionParam returns the project version from the ExtractProjectVersionParam handler.
|
|
func ProjectVersionParam(r *http.Request) database.ProjectVersion {
|
|
projectVersion, ok := r.Context().Value(projectVersionParamContextKey{}).(database.ProjectVersion)
|
|
if !ok {
|
|
panic("developer error: project version param middleware not provided")
|
|
}
|
|
return projectVersion
|
|
}
|
|
|
|
// ExtractProjectVersionParam grabs project version from the "projectversion" URL parameter.
|
|
func ExtractProjectVersionParam(db database.Store) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
project := ProjectParam(r)
|
|
projectVersionName := chi.URLParam(r, "projectversion")
|
|
if projectVersionName == "" {
|
|
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
|
Message: "project version name must be provided",
|
|
})
|
|
return
|
|
}
|
|
var projectVersion database.ProjectVersion
|
|
uuid, err := uuid.Parse(projectVersionName)
|
|
if err == nil {
|
|
projectVersion, err = db.GetProjectVersionByID(r.Context(), uuid)
|
|
} else {
|
|
projectVersion, err = db.GetProjectVersionByProjectIDAndName(r.Context(), database.GetProjectVersionByProjectIDAndNameParams{
|
|
ProjectID: project.ID,
|
|
Name: projectVersionName,
|
|
})
|
|
}
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
|
|
Message: fmt.Sprintf("project version %q does not exist", projectVersionName),
|
|
})
|
|
return
|
|
}
|
|
if err != nil {
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
Message: fmt.Sprintf("get project version: %s", err.Error()),
|
|
})
|
|
return
|
|
}
|
|
|
|
ctx := context.WithValue(r.Context(), projectVersionParamContextKey{}, projectVersion)
|
|
next.ServeHTTP(rw, r.WithContext(ctx))
|
|
})
|
|
}
|
|
}
|