MCPcopy Index your code
hub / github.com/coder/coder / ExtractTaskParam

Function ExtractTaskParam

coderd/httpmw/taskparam.go:38–83  ·  view source on GitHub ↗

ExtractTaskParam grabs a task from the "task" URL parameter. It supports two lookup strategies: 1. Task UUID (primary) 2. Task name scoped to owner (secondary) This middleware depends on ExtractOrganizationMembersParam being in the chain to provide the owner context for name-based lookups.

(db database.Store)

Source from the content-addressed store, hash-verified

36// This middleware depends on ExtractOrganizationMembersParam being in the chain
37// to provide the owner context for name-based lookups.
38func ExtractTaskParam(db database.Store) func(http.Handler) http.Handler {
39 return func(next http.Handler) http.Handler {
40 return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
41 ctx := r.Context()
42
43 // Get the task parameter value. We can't use ParseUUIDParam here because
44 // we need to support non-UUID values (task names) and
45 // attempt all lookup strategies.
46 taskParam := chi.URLParam(r, "task")
47 if taskParam == "" {
48 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
49 Message: "\"task\" must be provided.",
50 })
51 return
52 }
53
54 // Get owner from OrganizationMembersParam middleware for name-based lookups
55 members := OrganizationMembersParam(r)
56 ownerID := members.UserID()
57
58 task, err := fetchTaskWithFallback(ctx, db, taskParam, ownerID)
59 if err != nil {
60 if httpapi.Is404Error(err) {
61 httpapi.ResourceNotFound(rw)
62 return
63 }
64 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
65 Message: "Internal error fetching task.",
66 Detail: err.Error(),
67 })
68 return
69 }
70
71 ctx = context.WithValue(ctx, taskParamContextKey{}, task)
72
73 if rlogger := loggermw.RequestLoggerFromContext(ctx); rlogger != nil {
74 rlogger.WithFields(
75 slog.F("task_id", task.ID),
76 slog.F("task_name", task.Name),
77 )
78 }
79
80 next.ServeHTTP(rw, r.WithContext(ctx))
81 })
82 }
83}
84
85func fetchTaskWithFallback(ctx context.Context, db database.Store, taskParam string, ownerID uuid.UUID) (database.Task, error) {
86 // Attempt to first lookup the task by UUID.

Callers 1

TestTaskParamFunction · 0.92

Calls 12

WriteFunction · 0.92
Is404ErrorFunction · 0.92
ResourceNotFoundFunction · 0.92
RequestLoggerFromContextFunction · 0.92
OrganizationMembersParamFunction · 0.85
fetchTaskWithFallbackFunction · 0.85
UserIDMethod · 0.80
WithContextMethod · 0.80
ContextMethod · 0.65
WithFieldsMethod · 0.65
ErrorMethod · 0.45
ServeHTTPMethod · 0.45

Tested by 1

TestTaskParamFunction · 0.74