merge: name a workflow's declared inputs on the work-request form (06nq)

# Conflicts:
#	ui/dist/assets/index.js
This commit is contained in:
sam committed 2026-09-30 07:23:07 +10:00
commit 173c0dacba
7 files changed
+333 -22

No files matched your search

+30 -10
View File
@@ -50,6 +50,12 @@ type workRequest struct {
Workflow string `json:"workflow"`
Title string `json:"title"`
Instructions string `json:"instructions"`
// Inputs assigns the inputs the named workflow declares -- pr-review's
// pr_number, for instance. The handler checks the assignment against the
// declaration before the task is admitted, so a request that could only
// fail at the workflow's first stage is refused here instead
// (archie-core-06nq).
Inputs map[string]any `json:"inputs"`
}
func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
@@ -85,18 +91,22 @@ func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
http.Error(w, "identity, repository, workflow, title, and instructions are required", http.StatusBadRequest)
return
}
available, err := s.hasWorkflow(r.Context(), request.Workflow)
iface, enabled, err := s.enabledWorkflowInterface(r.Context(), request.Workflow)
if err != nil {
http.Error(w, "workflow definitions unavailable", http.StatusServiceUnavailable)
return
}
if !available {
if !enabled {
http.Error(w, "workflow is not enabled", http.StatusConflict)
return
}
if err := iface.CheckInputs(request.Inputs); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
taskID, err := s.WorkRequests.CreateTask(r.Context(), messaging.SpawnRequest{
Identity: request.Identity, Repo: request.Repository, Workflow: request.Workflow,
Title: request.Title, Body: request.Instructions,
Title: request.Title, Body: request.Instructions, Inputs: request.Inputs,
})
if err != nil {
http.Error(w, "work request rejected", http.StatusBadRequest)
@@ -107,16 +117,26 @@ func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
writeJSON(w, map[string]any{"ok": true, "task_id": taskID})
}
// hasWorkflow reports whether workflow id is defined and enabled for the
// default org, the org every dashboard caller acts in until access resolves
// a caller's own.
func (s *Server) hasWorkflow(ctx context.Context, id string) (bool, error) {
found, err := s.hasWorkflowDefinition(ctx, id)
// enabledWorkflowInterface returns the interface workflow id declares, and
// whether it is defined and enabled for the default org -- the org every
// dashboard caller acts in until access resolves a caller's own. The interface
// is what the work-request handler checks a request's inputs against, so a task
// whose workflow declaration the request cannot satisfy is refused before it is
// admitted.
func (s *Server) enabledWorkflowInterface(ctx context.Context, id string) (task.WorkflowInterface, bool, error) {
entry, found, err := s.workflowEntry(ctx, id)
if err != nil || !found {
return false, err
return task.WorkflowInterface{}, false, err
}
enablement, _, err := s.workflowEnablement(ctx)
return enablement.Enabled(org.DefaultOrgID, id), err
if err != nil || !enablement.Enabled(org.DefaultOrgID, id) {
return task.WorkflowInterface{}, false, err
}
iface, err := task.ParseWorkflowInterface(entry.YAML)
if err != nil {
return task.WorkflowInterface{}, false, err
}
return iface, true, nil
}
func (s *Server) hasWorkflowDefinition(ctx context.Context, id string) (bool, error) {
+101 -10
View File
@@ -49,18 +49,113 @@ func TestWorkflowsIncludeInstalledZeroRunDefinitions(t *testing.T) {
}
}
// declaredInputsDefinition is a pr-review-shaped definition: it declares the
// inputs the handler checks a work request against, one of them required, so a
// request that does not name a pull request number cannot be admitted.
const declaredInputsDefinition = `id: pr-review
inputs:
pr_number:
type: number
required: true
depth:
type: string
steps:
- type: implement.prepare
`
func postWorkRequest(t *testing.T, srv *Server, body string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/work-requests", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Archie-CSRF", "1")
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
return w
}
// TestWorkRequestCarriesDeclaredInputs: a dashboard request names the workflow's
// declared inputs and the admitted task carries them, so a pr-review request
// reaches its workflow with the pull request number stagePRIntake reads
// (archie-core-06nq).
func TestWorkRequestCarriesDeclaredInputs(t *testing.T) {
srv := newTestServer(t)
creator := &recordingWorkRequestCreator{}
srv.WorkRequests = creator
srv.ControlPlane = workflowControlPlane(t, workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "pr-review", YAML: declaredInputsDefinition}}})
w := postWorkRequest(t, srv, `{"identity":"archie","repository":"acme/widget","workflow":"pr-review","title":"Review 77","instructions":"Review it.","inputs":{"pr_number":77,"depth":"deep"}}`)
if w.Code != http.StatusCreated {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}
if got := creator.request.Inputs["pr_number"]; got != float64(77) {
t.Errorf("pr_number input = %#v, want the number the request sent", got)
}
if got := creator.request.Inputs["depth"]; got != "deep" {
t.Errorf("depth input = %#v, want \"deep\"", got)
}
// EffectivePRNumber is how the daemon and stagePRIntake read the input, so
// the assignment is proven against the consumer rather than against the
// map's shape alone.
if got := (workflow.Task{Inputs: creator.request.Inputs}).EffectivePRNumber(); got != 77 {
t.Errorf("EffectivePRNumber() = %d, want 77", got)
}
}
// TestWorkRequestRejectsUnmetDeclaredInputs: the request is refused at the
// producer, naming the input, instead of being admitted and then parked or
// failed at the workflow's first stage.
func TestWorkRequestRejectsUnmetDeclaredInputs(t *testing.T) {
tests := []struct {
name string
body string
want string
}{
{
name: "missing a required input",
body: `{"identity":"archie","repository":"acme/widget","workflow":"pr-review","title":"Review","instructions":"Review it."}`,
want: `input "pr_number" is required`,
},
{
name: "naming an input the workflow does not declare",
body: `{"identity":"archie","repository":"acme/widget","workflow":"pr-review","title":"Review","instructions":"Review it.","inputs":{"pr_number":77,"pull_request":77}}`,
want: `input "pull_request" is not declared by the workflow`,
},
{
name: "a value of the wrong type",
body: `{"identity":"archie","repository":"acme/widget","workflow":"pr-review","title":"Review","instructions":"Review it.","inputs":{"pr_number":"seventy-seven"}}`,
want: `input "pr_number" is string, want number`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
srv := newTestServer(t)
creator := &recordingWorkRequestCreator{}
srv.WorkRequests = creator
srv.ControlPlane = workflowControlPlane(t, workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "pr-review", YAML: declaredInputsDefinition}}})
w := postWorkRequest(t, srv, tt.body)
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}
if !strings.Contains(w.Body.String(), tt.want) {
t.Errorf("body = %q, want it to name the failure %q", w.Body, tt.want)
}
if got := creator.request; got.Title != "" || got.Body != "" || len(got.Inputs) != 0 {
t.Errorf("creator called with %#v", got)
}
})
}
}
func TestWorkRequestUsesNormalTaskAdmission(t *testing.T) {
srv := newTestServer(t)
creator := &recordingWorkRequestCreator{}
srv.WorkRequests = creator
srv.ControlPlane = workflowControlPlane(t, workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "implement", YAML: "id: implement\nsteps:\n - type: implement.prepare\n"}}})
// An inputless workflow keeps taking an inputless request.
body := `{"identity":"archie","repository":"acme/widget","workflow":"implement","title":"Fix login","instructions":"Reproduce and fix it."}`
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/work-requests", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Archie-CSRF", "1")
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
w := postWorkRequest(t, srv, body)
if w.Code != http.StatusCreated {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
@@ -75,11 +170,7 @@ func TestWorkRequestRejectsDisabledWorkflow(t *testing.T) {
creator := &recordingWorkRequestCreator{}
srv.WorkRequests = creator
srv.ControlPlane = workflowControlPlane(t, workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "custom", YAML: "id: custom\nsteps:\n - type: implement.prepare\n"}}})
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/work-requests", strings.NewReader(`{"identity":"archie","repository":"acme/widget","workflow":"implement","title":"Fix","instructions":"Do it"}`))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Archie-CSRF", "1")
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
w := postWorkRequest(t, srv, `{"identity":"archie","repository":"acme/widget","workflow":"implement","title":"Fix","instructions":"Do it"}`)
if w.Code != http.StatusConflict {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}