feat(runs,events,ui): identify workflows by immutable UUID (#2125)

This commit is contained in:
sam committed 2026-11-24 05:20:18 +11:00
1 parent 21d1d2e8bf
commit 67ce2901a5
46 files changed
+1284 -512

No files matched your search

+204 -53
View File
@@ -19,6 +19,7 @@ import (
// WorkflowView is one stored version of a workflow.
type WorkflowView struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Version int `json:"version"`
Body string `json:"body"`
@@ -31,7 +32,7 @@ type WorkflowView struct {
}
func workflowView(w store.Workflow) WorkflowView {
v := WorkflowView{Name: w.Name, Version: w.Version, Body: w.Body, CreatedBy: w.CreatedBy, CreatedAt: w.CreatedAt, Steps: []string{}, Enabled: !w.Disabled}
v := WorkflowView{ID: w.ID, Name: w.Name, Version: w.Version, Body: w.Body, CreatedBy: w.CreatedBy, CreatedAt: w.CreatedAt, Steps: []string{}, Enabled: !w.Disabled}
if doc, err := ParseDocument(w.Body); err == nil {
v.Steps = doc.Describe()
}
@@ -40,6 +41,8 @@ func workflowView(w store.Workflow) WorkflowView {
// DraftView is the unpublished edit of a workflow.
type DraftView struct {
WorkflowID uuid.UUID `json:"workflow_id"`
// Name is the workflow's name.
Name string `json:"name"`
Body string `json:"body"`
// BaseVersion is the published version the draft began from, 0 for a
@@ -52,12 +55,14 @@ type DraftView struct {
}
func draftView(d store.Draft) DraftView {
return DraftView{Name: d.Name, Body: d.Body, BaseVersion: d.BaseVersion, Revision: d.Revision, UpdatedBy: d.UpdatedBy, UpdatedAt: d.UpdatedAt}
return DraftView{WorkflowID: d.WorkflowID, Name: d.Name, Body: d.Body, BaseVersion: d.BaseVersion, Revision: d.Revision, UpdatedBy: d.UpdatedBy, UpdatedAt: d.UpdatedAt}
}
// WorkflowStats is what one workflow's runs add up to. Test runs are not
// counted.
type WorkflowStats struct {
WorkflowID uuid.UUID `json:"workflow_id"`
// Workflow is the workflow's name.
Workflow string `json:"workflow"`
Total int `json:"total"`
Succeeded int `json:"succeeded"`
@@ -80,7 +85,7 @@ type LastRun struct {
func workflowStats(s store.Stats) WorkflowStats {
out := WorkflowStats{
Workflow: s.Workflow, Total: s.Total, Succeeded: s.Succeeded, Failed: s.Failed, Active: s.Active,
WorkflowID: s.WorkflowID, Workflow: s.Workflow, Total: s.Total, Succeeded: s.Succeeded, Failed: s.Failed, Active: s.Active,
LastRun: LastRun{ID: s.LastRunID, State: s.LastState, At: s.LastRunAt},
}
if ended := s.Succeeded + s.Failed; ended > 0 {
@@ -102,24 +107,32 @@ func (e *Engine) registerWorkflows(api huma.API, orgID string) {
OperationID: "list-workflows", Method: http.MethodGet, Path: "/api/workflows",
Summary: "List every workflow at its latest version", Tags: tags,
}, a.list)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionCreate, governance.KindWorkflow), huma.Operation{
OperationID: "create-workflow", Method: http.MethodPost, Path: "/api/workflows",
Summary: "Create a workflow, and publish its first version when a document is given", Tags: tags, DefaultStatus: http.StatusCreated,
}, a.create)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionUpdate, governance.KindWorkflow), huma.Operation{
OperationID: "rename-workflow", Method: http.MethodPut, Path: "/api/workflows/{id}/name",
Summary: "Rename a workflow; its runs, triggers and callers refer to it by id and are unchanged", Tags: tags,
}, a.rename)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindWorkflow), huma.Operation{
OperationID: "get-workflow", Method: http.MethodGet, Path: "/api/workflows/{name}",
OperationID: "get-workflow", Method: http.MethodGet, Path: "/api/workflows/{id}",
Summary: "Get a workflow", Tags: tags,
}, a.get)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionUpdate, governance.KindWorkflow), huma.Operation{
OperationID: "put-workflow", Method: http.MethodPut, Path: "/api/workflows/{name}",
OperationID: "put-workflow", Method: http.MethodPut, Path: "/api/workflows/{id}",
Summary: "Publish a workflow document as its next version", Tags: tags,
}, a.put)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionDelete, governance.KindWorkflow), huma.Operation{
OperationID: "delete-workflow", Method: http.MethodDelete, Path: "/api/workflows/{name}",
Summary: "Delete a workflow, unless a trigger or another workflow still names it", Tags: tags,
OperationID: "delete-workflow", Method: http.MethodDelete, Path: "/api/workflows/{id}",
Summary: "Delete a workflow, unless a trigger or another workflow still refers to it", Tags: tags,
}, a.remove)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionUpdate, governance.KindWorkflow), huma.Operation{
OperationID: "set-workflow-enabled", Method: http.MethodPut, Path: "/api/workflows/{name}/enabled",
OperationID: "set-workflow-enabled", Method: http.MethodPut, Path: "/api/workflows/{id}/enabled",
Summary: "Enable or disable a workflow", Tags: tags,
}, a.setEnabled)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRun, governance.KindWorkflow), huma.Operation{
OperationID: "run-workflow", Method: http.MethodPost, Path: "/api/workflows/{name}/runs",
OperationID: "run-workflow", Method: http.MethodPost, Path: "/api/workflows/{id}/runs",
Summary: "Run a workflow now with an input", Tags: tags, DefaultStatus: http.StatusAccepted,
}, a.run)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindWorkflow), huma.Operation{
@@ -127,19 +140,19 @@ func (e *Engine) registerWorkflows(api huma.API, orgID string) {
Summary: "List every workflow's unpublished draft", Tags: tags,
}, a.drafts)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindWorkflow), huma.Operation{
OperationID: "get-workflow-draft", Method: http.MethodGet, Path: "/api/workflows/{name}/draft",
OperationID: "get-workflow-draft", Method: http.MethodGet, Path: "/api/workflows/{id}/draft",
Summary: "Get a workflow's unpublished draft", Tags: tags,
}, a.draft)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionUpdate, governance.KindWorkflow), huma.Operation{
OperationID: "put-workflow-draft", Method: http.MethodPut, Path: "/api/workflows/{name}/draft",
OperationID: "put-workflow-draft", Method: http.MethodPut, Path: "/api/workflows/{id}/draft",
Summary: "Save a workflow's draft, which no run uses until it is published", Tags: tags,
}, a.putDraft)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionUpdate, governance.KindWorkflow), huma.Operation{
OperationID: "discard-workflow-draft", Method: http.MethodDelete, Path: "/api/workflows/{name}/draft",
OperationID: "discard-workflow-draft", Method: http.MethodDelete, Path: "/api/workflows/{id}/draft",
Summary: "Discard a workflow's draft", Tags: tags, DefaultStatus: http.StatusNoContent,
}, a.discardDraft)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRun, governance.KindWorkflow), huma.Operation{
OperationID: "test-workflow-draft", Method: http.MethodPost, Path: "/api/workflows/{name}/draft/test-run",
OperationID: "test-workflow-draft", Method: http.MethodPost, Path: "/api/workflows/{id}/draft/test-run",
Summary: "Test-run a workflow's draft: every action is judged and none that changes something runs", Tags: tags, DefaultStatus: http.StatusAccepted,
}, a.testDraft)
httpapi.Register(api, httpapi.AdminAuthorized(governance.ActionRead, governance.KindWorkflow), huma.Operation{
@@ -171,15 +184,37 @@ func (a *workflowAPI) list(ctx context.Context, _ *emptyInput) (*httpapi.Reply[[
return httpapi.OK(out), nil
}
// idInput is a workflow's id in the path.
type idInput struct {
ID string `path:"id" format:"uuid"`
}
// workflowID parses a workflow id from the path.
func workflowID(raw string) (uuid.UUID, error) {
id, err := uuid.Parse(raw)
if err != nil {
return uuid.Nil, huma.Error400BadRequest("a workflow id is a UUID")
}
return id, nil
}
type workflowInput struct {
Name string `path:"name"`
ID string `path:"id" format:"uuid"`
Version int `query:"version" minimum:"1" doc:"A past version; the latest by default"`
}
func (a *workflowAPI) get(ctx context.Context, in *workflowInput) (*httpapi.Reply[WorkflowView], error) {
w, err := a.e.db.Workflow(ctx, a.orgID, in.Name, in.Version)
id, err := workflowID(in.ID)
if err != nil {
return nil, err
}
return a.view(ctx, id, in.Version)
}
func (a *workflowAPI) view(ctx context.Context, id uuid.UUID, version int) (*httpapi.Reply[WorkflowView], error) {
w, err := a.e.db.Workflow(ctx, a.orgID, id, version)
switch {
case errors.Is(err, store.ErrNoWorkflow):
case errors.Is(err, store.ErrNoWorkflow), err == nil && w.Deleted:
return nil, huma.Error404NotFound("no such workflow")
case err != nil:
return nil, a.fail(ctx, "read the workflow", err)
@@ -187,43 +222,136 @@ func (a *workflowAPI) get(ctx context.Context, in *workflowInput) (*httpapi.Repl
return httpapi.OK(workflowView(w)), nil
}
// WorkflowIdentity is a workflow apart from its versions.
type WorkflowIdentity struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
// Version is the version published with the workflow, 0 when none was.
Version int `json:"version"`
}
type createWorkflowInput struct {
Body struct {
Name string `json:"name" doc:"Lowercase letters and digits, joined by dots or hyphens"`
Body string `json:"body,omitempty" doc:"The first version's workflow document, as YAML; the workflow is created unpublished without one"`
}
}
func (a *workflowAPI) create(ctx context.Context, in *createWorkflowInput) (*httpapi.Reply[WorkflowIdentity], error) {
if !stableid.Valid(in.Body.Name) {
return nil, huma.Error400BadRequest("a workflow name is lowercase letters and digits, joined by dots or hyphens")
}
if in.Body.Body != "" {
// Check the document before creating anything, so a bad one leaves no
// unpublished workflow behind.
if err := a.e.checkDocument(ctx, a.orgID, in.Body.Body); err != nil {
return nil, a.publishError(ctx, err)
}
}
id, err := a.e.Create(ctx, a.orgID, in.Body.Name)
switch {
case errors.Is(err, ErrNameTaken):
return nil, huma.Error409Conflict("another workflow has that name")
case err != nil:
return nil, a.fail(ctx, "create the workflow", err)
}
out := WorkflowIdentity{ID: id, Name: in.Body.Name, Enabled: true}
if in.Body.Body != "" {
zero := 0
version, err := a.e.Publish(ctx, a.orgID, id, in.Body.Body, governance.CallerID(ctx), &zero)
if err != nil {
if derr := a.e.defs.DeleteWorkflow(ctx, a.orgID, id); derr != nil {
a.e.log.WarnContext(ctx, "remove workflow whose first version was refused", "workflow", id, "error", derr)
}
return nil, a.publishError(ctx, err)
}
out.Version = version
}
return httpapi.OK(out), nil
}
type renameInput struct {
ID string `path:"id" format:"uuid"`
Body struct {
Name string `json:"name" doc:"Lowercase letters and digits, joined by dots or hyphens"`
}
}
func (a *workflowAPI) rename(ctx context.Context, in *renameInput) (*httpapi.Reply[WorkflowIdentity], error) {
id, err := workflowID(in.ID)
if err != nil {
return nil, err
}
if !stableid.Valid(in.Body.Name) {
return nil, huma.Error400BadRequest("a workflow name is lowercase letters and digits, joined by dots or hyphens")
}
switch err := a.e.Rename(ctx, a.orgID, id, in.Body.Name); {
case errors.Is(err, ErrNameTaken):
return nil, huma.Error409Conflict("another workflow has that name")
case errors.Is(err, store.ErrNoWorkflow):
return nil, huma.Error404NotFound("no such workflow")
case err != nil:
return nil, a.fail(ctx, "rename the workflow", err)
}
return a.identity(ctx, id)
}
func (a *workflowAPI) identity(ctx context.Context, id uuid.UUID) (*httpapi.Reply[WorkflowIdentity], error) {
w, err := a.e.db.Identity(ctx, a.orgID, id)
if err != nil {
return nil, a.fail(ctx, "read the workflow", err)
}
out := WorkflowIdentity{ID: w.ID, Name: w.Name, Enabled: !w.Disabled}
if latest, err := a.e.db.Workflow(ctx, a.orgID, id, 0); err == nil {
out.Version = latest.Version
}
return httpapi.OK(out), nil
}
type putWorkflowInput struct {
Name string `path:"name" doc:"Lowercase letters and digits, joined by dots or hyphens"`
ID string `path:"id" format:"uuid"`
Body struct {
Body string `json:"body" doc:"The workflow document, as YAML"`
// ExpectedVersion makes the save refuse unless the latest version is
// this one, so an editor can't publish over a version it never saw.
ExpectedVersion *int `json:"expected_version,omitempty" minimum:"0" doc:"The latest version the document was edited from, 0 for a new workflow; the save is refused with 409 when another is stored"`
ExpectedVersion *int `json:"expected_version,omitempty" minimum:"0" doc:"The latest version the document was edited from, 0 for a workflow not yet published; the save is refused with 409 when another is stored"`
}
}
func (a *workflowAPI) put(ctx context.Context, in *putWorkflowInput) (*httpapi.Reply[WorkflowView], error) {
if !stableid.Valid(in.Name) {
return nil, huma.Error400BadRequest("a workflow name is lowercase letters and digits, joined by dots or hyphens")
}
version, err := a.e.Publish(ctx, a.orgID, in.Name, in.Body.Body, governance.CallerID(ctx), in.Body.ExpectedVersion)
id, err := workflowID(in.ID)
if err != nil {
if errors.Is(err, ErrVersionConflict) {
return nil, huma.Error409Conflict("the workflow has another version than the one this was edited from; reload it before publishing")
}
if invalid, ok := errors.AsType[*InvalidError](err); ok {
return nil, Problems(invalid)
}
return nil, a.fail(ctx, "save the workflow", err)
return nil, err
}
w, err := a.e.db.Workflow(ctx, a.orgID, in.Name, version)
version, err := a.e.Publish(ctx, a.orgID, id, in.Body.Body, governance.CallerID(ctx), in.Body.ExpectedVersion)
if err != nil {
return nil, a.fail(ctx, "read the workflow", err)
return nil, a.publishError(ctx, err)
}
return httpapi.OK(workflowView(w)), nil
return a.view(ctx, id, version)
}
type nameInput struct {
Name string `path:"name"`
// publishError turns why a document wasn't published into the response that
// says what to do about it.
func (a *workflowAPI) publishError(ctx context.Context, err error) error {
if invalid, ok := errors.AsType[*InvalidError](err); ok {
return Problems(invalid)
}
switch {
case errors.Is(err, ErrVersionConflict):
return huma.Error409Conflict("the workflow has another version than the one this was edited from; reload it before publishing")
case errors.Is(err, store.ErrNoWorkflow):
return huma.Error404NotFound("no such workflow")
}
return a.fail(ctx, "save the workflow", err)
}
func (a *workflowAPI) remove(ctx context.Context, in *nameInput) (*struct{}, error) {
switch err := a.e.Delete(ctx, a.orgID, in.Name); {
func (a *workflowAPI) remove(ctx context.Context, in *idInput) (*struct{}, error) {
id, err := workflowID(in.ID)
if err != nil {
return nil, err
}
switch err := a.e.Delete(ctx, a.orgID, id); {
case errors.Is(err, store.ErrNoWorkflow):
return nil, huma.Error404NotFound("no such workflow")
case err != nil:
@@ -236,39 +364,47 @@ func (a *workflowAPI) remove(ctx context.Context, in *nameInput) (*struct{}, err
}
type setEnabledInput struct {
Name string `path:"name"`
ID string `path:"id" format:"uuid"`
Body struct {
Enabled bool `json:"enabled"`
}
}
func (a *workflowAPI) setEnabled(ctx context.Context, in *setEnabledInput) (*httpapi.Reply[WorkflowView], error) {
switch err := a.e.SetEnabled(ctx, a.orgID, in.Name, in.Body.Enabled); {
id, err := workflowID(in.ID)
if err != nil {
return nil, err
}
switch err := a.e.SetEnabled(ctx, a.orgID, id, in.Body.Enabled); {
case errors.Is(err, store.ErrNoWorkflow):
return nil, huma.Error404NotFound("no such workflow")
case err != nil:
return nil, a.fail(ctx, "change whether the workflow is enabled", err)
}
return a.get(ctx, &workflowInput{Name: in.Name})
return a.view(ctx, id, 0)
}
type runWorkflowInput struct {
Name string `path:"name"`
ID string `path:"id" format:"uuid"`
Body struct {
Input map[string]any `json:"input,omitempty" doc:"The workflow's inputs by name"`
}
}
func (a *workflowAPI) run(ctx context.Context, in *runWorkflowInput) (*httpapi.Reply[RunView], error) {
id, err := workflowID(in.ID)
if err != nil {
return nil, err
}
input, err := inputOf(in.Body.Input)
if err != nil {
return nil, huma.Error400BadRequest("the input isn't valid JSON")
}
id, err := a.e.Start(ctx, StartRequest{OrgID: a.orgID, Workflow: in.Name, Input: input})
run, err := a.e.Start(ctx, StartRequest{OrgID: a.orgID, WorkflowID: id, Input: input})
if err != nil {
return nil, a.startError(ctx, err)
}
return a.started(ctx, id)
return a.started(ctx, run)
}
func (a *workflowAPI) started(ctx context.Context, id uuid.UUID) (*httpapi.Reply[RunView], error) {
@@ -312,8 +448,12 @@ func (a *workflowAPI) drafts(ctx context.Context, _ *emptyInput) (*httpapi.Reply
return httpapi.OK(out), nil
}
func (a *workflowAPI) draft(ctx context.Context, in *nameInput) (*httpapi.Reply[DraftView], error) {
d, err := a.e.db.Draft(ctx, a.orgID, in.Name)
func (a *workflowAPI) draft(ctx context.Context, in *idInput) (*httpapi.Reply[DraftView], error) {
id, err := workflowID(in.ID)
if err != nil {
return nil, err
}
d, err := a.e.db.Draft(ctx, a.orgID, id)
switch {
case errors.Is(err, store.ErrNoDraft):
return nil, huma.Error404NotFound("the workflow has no draft")
@@ -324,7 +464,7 @@ func (a *workflowAPI) draft(ctx context.Context, in *nameInput) (*httpapi.Reply[
}
type putDraftInput struct {
Name string `path:"name" doc:"Lowercase letters and digits, joined by dots or hyphens"`
ID string `path:"id" format:"uuid"`
Body struct {
Body string `json:"body" doc:"The draft document, as YAML. It may be unfinished or invalid; publishing checks it."`
// Revision is the draft's revision as last read, so a write can't
@@ -334,11 +474,14 @@ type putDraftInput struct {
}
func (a *workflowAPI) putDraft(ctx context.Context, in *putDraftInput) (*httpapi.Reply[DraftView], error) {
if !stableid.Valid(in.Name) {
return nil, huma.Error400BadRequest("a workflow name is lowercase letters and digits, joined by dots or hyphens")
id, err := workflowID(in.ID)
if err != nil {
return nil, err
}
d, err := a.e.db.PutDraft(ctx, a.orgID, in.Name, in.Body.Body, governance.CallerID(ctx), in.Body.Revision)
d, err := a.e.db.PutDraft(ctx, a.orgID, id, in.Body.Body, governance.CallerID(ctx), in.Body.Revision)
switch {
case errors.Is(err, store.ErrNoWorkflow):
return nil, huma.Error404NotFound("no such workflow")
case errors.Is(err, store.ErrDraftConflict):
return nil, huma.Error409Conflict("the draft was changed elsewhere; load it before saving")
case err != nil:
@@ -347,8 +490,12 @@ func (a *workflowAPI) putDraft(ctx context.Context, in *putDraftInput) (*httpapi
return httpapi.OK(draftView(d)), nil
}
func (a *workflowAPI) discardDraft(ctx context.Context, in *nameInput) (*struct{}, error) {
had, err := a.e.db.DeleteDraft(ctx, a.orgID, in.Name)
func (a *workflowAPI) discardDraft(ctx context.Context, in *idInput) (*struct{}, error) {
id, err := workflowID(in.ID)
if err != nil {
return nil, err
}
had, err := a.e.db.DeleteDraft(ctx, a.orgID, id)
switch {
case err != nil:
return nil, a.fail(ctx, "discard the draft", err)
@@ -359,7 +506,11 @@ func (a *workflowAPI) discardDraft(ctx context.Context, in *nameInput) (*struct{
}
func (a *workflowAPI) testDraft(ctx context.Context, in *runWorkflowInput) (*httpapi.Reply[RunView], error) {
d, err := a.e.db.Draft(ctx, a.orgID, in.Name)
id, err := workflowID(in.ID)
if err != nil {
return nil, err
}
d, err := a.e.db.Draft(ctx, a.orgID, id)
switch {
case errors.Is(err, store.ErrNoDraft):
return nil, huma.Error404NotFound("the workflow has no draft to test")
@@ -370,11 +521,11 @@ func (a *workflowAPI) testDraft(ctx context.Context, in *runWorkflowInput) (*htt
if err != nil {
return nil, huma.Error400BadRequest("the input isn't valid JSON")
}
id, err := a.e.StartDry(ctx, DryRequest{OrgID: a.orgID, Workflow: in.Name, Document: d.Body, Input: input})
run, err := a.e.StartDry(ctx, DryRequest{OrgID: a.orgID, WorkflowID: id, Document: d.Body, Input: input})
if err != nil {
return nil, a.startError(ctx, err)
}
return a.started(ctx, id)
return a.started(ctx, run)
}
func (a *workflowAPI) stats(ctx context.Context, _ *emptyInput) (*httpapi.Reply[[]WorkflowStats], error) {