feat(workflows): test-run unsaved drafts and browse workflow version history
A test run pins a validated draft, runs with forge and push writes suppressed and recorded per step, and is excluded from workflow stats. The History tab lists revisions that changed a workflow, diffs them as YAML and on the canvas, restores into the editor, and ties runs to the revision they used.
This commit is contained in:
37 files changed
+677
-132
No files matched your search
@@ -9,8 +9,12 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
||||
"github.com/samcharles93/archie-core/internal/domain/org"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
)
|
||||
@@ -55,6 +59,10 @@ type workRequest struct {
|
||||
// declaration before the task is admitted, so a request that could only
|
||||
// fail at the workflow's first stage is refused here instead
|
||||
Inputs map[string]any `json:"inputs"`
|
||||
// Draft is an unsaved definition of Workflow to test-run instead of the
|
||||
// stored one. The stored definition is untouched and the run writes
|
||||
// nothing to a forge.
|
||||
Draft string `json:"draft"`
|
||||
}
|
||||
|
||||
func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -86,13 +94,9 @@ func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "identity, workflow, title, and instructions are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
iface, enabled, err := s.enabledWorkflowInterface(r.Context(), request.Workflow)
|
||||
iface, code, err := s.workRequestInterface(r.Context(), request)
|
||||
if err != nil {
|
||||
http.Error(w, "workflow definitions unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if !enabled {
|
||||
http.Error(w, "workflow is not enabled", http.StatusConflict)
|
||||
http.Error(w, err.Error(), code)
|
||||
return
|
||||
}
|
||||
if err := iface.CheckInputs(request.Inputs); err != nil {
|
||||
@@ -104,7 +108,7 @@ func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, err.Error(), status)
|
||||
return
|
||||
}
|
||||
created, err := s.Store.EnqueueChatTask(r.Context(), owner, repo, request.Title, request.Instructions, request.Workflow, request.Identity, "", request.Inputs)
|
||||
created, err := s.Store.EnqueueChatTask(r.Context(), owner, repo, request.Title, request.Instructions, request.Workflow, request.Identity, "", request.Draft, request.Inputs)
|
||||
if err != nil {
|
||||
s.logf("work request enqueue failed", "err", err)
|
||||
http.Error(w, "work request rejected", http.StatusBadRequest)
|
||||
@@ -115,6 +119,56 @@ func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, map[string]any{"ok": true, "task_id": created.ID})
|
||||
}
|
||||
|
||||
// workRequestInterface returns the interface a request runs against: the
|
||||
// draft's when it carries one, else the stored definition's. The workflow must
|
||||
// be defined and enabled either way.
|
||||
func (s *Server) workRequestInterface(ctx context.Context, request workRequest) (task.WorkflowInterface, int, error) {
|
||||
iface, enabled, err := s.enabledWorkflowInterface(ctx, request.Workflow)
|
||||
switch {
|
||||
case err != nil:
|
||||
return iface, http.StatusServiceUnavailable, errors.New("workflow definitions unavailable")
|
||||
case !enabled:
|
||||
return iface, http.StatusConflict, errors.New("workflow is not enabled")
|
||||
case request.Draft != "":
|
||||
return s.checkDraft(ctx, request.Workflow, request.Draft)
|
||||
}
|
||||
return iface, 0, nil
|
||||
}
|
||||
|
||||
// checkDraft refuses a draft a save of the same document would refuse, and
|
||||
// returns the interface the draft declares.
|
||||
func (s *Server) checkDraft(ctx context.Context, id, draft string) (task.WorkflowInterface, int, error) {
|
||||
if declared, err := workflow.DefinitionID(draft); err != nil || declared != id {
|
||||
return task.WorkflowInterface{}, http.StatusBadRequest, errors.New("the draft must be a definition of the workflow being run")
|
||||
}
|
||||
collection, err := s.workflowCollection(ctx)
|
||||
if err != nil {
|
||||
return task.WorkflowInterface{}, http.StatusServiceUnavailable, errors.New("workflow definitions unavailable")
|
||||
}
|
||||
replaced := collection.Definitions[:0:0]
|
||||
for _, entry := range collection.Definitions {
|
||||
if entry.ID == id {
|
||||
entry.YAML = draft
|
||||
}
|
||||
replaced = append(replaced, entry)
|
||||
}
|
||||
value, err := json.Marshal(task.WorkflowDefinitionCollection{Definitions: replaced})
|
||||
if err != nil {
|
||||
return task.WorkflowInterface{}, http.StatusInternalServerError, err
|
||||
}
|
||||
if _, err := s.ControlPlane.Command(ctx, &controlpb.CommandRequest{Kind: workflowDefinitionsKind, Command: "validate", ValueJson: value}); err != nil {
|
||||
if status.Code(err) == codes.InvalidArgument {
|
||||
return task.WorkflowInterface{}, http.StatusBadRequest, errors.New(status.Convert(err).Message())
|
||||
}
|
||||
return task.WorkflowInterface{}, http.StatusServiceUnavailable, errors.New("workflow definitions unavailable")
|
||||
}
|
||||
iface, err := task.ParseWorkflowInterface(draft)
|
||||
if err != nil {
|
||||
return task.WorkflowInterface{}, http.StatusBadRequest, err
|
||||
}
|
||||
return iface, 0, nil
|
||||
}
|
||||
|
||||
// workRequestRepository checks a requested "owner/name" against the
|
||||
// workflow's repository mode and the configured repositories, and returns it
|
||||
// split. A workflow that takes none refuses one; one that requires it refuses
|
||||
|
||||
Reference in new issue
Block a user