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

+23 -16
View File
@@ -241,12 +241,12 @@ func (m *Module) setHMAC(src *store.Source, set SourceSettings, stored *store.So
type putTriggerInput struct {
Name string `path:"name" doc:"Lowercase letters, digits and hyphens"`
Body struct {
SourceID string `json:"source_id,omitzero" format:"uuid" doc:"The source whose events fire it; none when every is set"`
EventType string `json:"event_type,omitzero" doc:"The event type that fires it; fixed when every is set"`
Filter map[string]string `json:"filter,omitzero" doc:"Event fields that must match exactly"`
Workflow string `json:"workflow" doc:"The workflow each match runs"`
Disabled bool `json:"disabled,omitzero"`
Every string `json:"every,omitzero" doc:"Fire on an interval instead of on events, such as 1h" example:"1h"`
SourceID string `json:"source_id,omitzero" format:"uuid" doc:"The source whose events fire it; none when every is set"`
EventType string `json:"event_type,omitzero" doc:"The event type that fires it; fixed when every is set"`
Filter map[string]string `json:"filter,omitzero" doc:"Event fields that must match exactly"`
WorkflowID string `json:"workflow_id" format:"uuid" doc:"The workflow each match runs"`
Disabled bool `json:"disabled,omitzero"`
Every string `json:"every,omitzero" doc:"Fire on an interval instead of on events, such as 1h" example:"1h"`
// EventTypeID restricts the trigger to events identified as that event
// type, and MappingID reads their payload for the run.
EventTypeID string `json:"event_type_id,omitzero" format:"uuid" doc:"Fire only for events identified as this event type of the source"`
@@ -283,14 +283,18 @@ func (m *Module) putTrigger(ctx context.Context, in *putTriggerInput) (*httpapi.
// this trigger to its own.
req.Filter = map[string]string{"trigger": in.Name}
}
if !validName.MatchString(in.Name) || (req.SourceID == "" && !every.Valid) || (req.EventType == "" && req.EventTypeID == "") || req.Workflow == "" {
if !validName.MatchString(in.Name) || (req.SourceID == "" && !every.Valid) || (req.EventType == "" && req.EventTypeID == "") || req.WorkflowID == "" {
return nil, huma.Error400BadRequest("a trigger needs a lowercase name, a workflow, and either a source and event type or an every interval")
}
sourceID, err := m.triggerSource(ctx, req.SourceID)
if err != nil {
return nil, err
}
typeID, mappingID, err := m.checkTargets(ctx, sourceID, req.EventType, req.Workflow, req.EventTypeID, req.MappingID)
workflow, err := uuid.Parse(req.WorkflowID)
if err != nil {
return nil, huma.Error400BadRequest("a workflow id is a UUID", &huma.ErrorDetail{Location: "body.workflow_id", Message: "a workflow id is a UUID"})
}
typeID, mappingID, err := m.checkTargets(ctx, sourceID, req.EventType, workflow, req.EventTypeID, req.MappingID)
if err != nil {
return nil, err
}
@@ -304,7 +308,7 @@ func (m *Module) putTrigger(ctx context.Context, in *putTriggerInput) (*httpapi.
}
if err := m.store.UpsertTrigger(ctx, store.Trigger{
OrgID: m.orgID, Name: in.Name, SourceID: sourceID, EventType: req.EventType,
Filter: filter, Workflow: req.Workflow, Enabled: !req.Disabled, Approved: approved,
Filter: filter, WorkflowID: workflow, Enabled: !req.Disabled, Approved: approved,
EventTypeID: typeID, MappingID: mappingID, ScheduleEverySeconds: every,
}); err != nil {
return nil, huma.Error500InternalServerError("couldn't save the trigger", err)
@@ -337,13 +341,13 @@ func (m *Module) triggerSource(ctx context.Context, raw string) (uuid.NullUUID,
// checkTargets refuses a trigger whose workflow can't be started or whose
// event type, mapping or connector event type doesn't exist.
func (m *Module) checkTargets(ctx context.Context, sourceID uuid.NullUUID, eventType, workflow, eventTypeID, mappingID string) (typeID, mapID uuid.NullUUID, err error) {
func (m *Module) checkTargets(ctx context.Context, sourceID uuid.NullUUID, eventType string, workflow uuid.UUID, eventTypeID, mappingID string) (typeID, mapID uuid.NullUUID, err error) {
if typeID, mapID, err = m.checkBinding(ctx, sourceID, eventTypeID, mappingID); err != nil {
return typeID, mapID, err
}
if err := m.runs.CheckTrigger(ctx, m.orgID, workflow); err != nil {
if refused, ok := errors.AsType[*runs.InvalidError](err); ok {
return typeID, mapID, huma.Error400BadRequest(refused.Problems[0].Message, &huma.ErrorDetail{Location: "body.workflow", Message: refused.Problems[0].Message})
return typeID, mapID, huma.Error400BadRequest(refused.Problems[0].Message, &huma.ErrorDetail{Location: "body.workflow_id", Message: refused.Problems[0].Message})
}
return typeID, mapID, huma.Error500InternalServerError("couldn't check the workflow", err)
}
@@ -489,7 +493,6 @@ type proposedTrigger struct {
Source string `json:"source" doc:"The source's label"`
EventType string `json:"event_type"`
Filter jsontext.Value `json:"filter"`
Workflow string `json:"workflow"`
}
func (m *Module) groups(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]groupView], error) {
@@ -583,9 +586,11 @@ type triggerView struct {
Source string `json:"source,omitempty"`
EventType string `json:"event_type"`
Filter map[string]string `json:"filter"`
Workflow string `json:"workflow"`
Enabled bool `json:"enabled"`
Every string `json:"every,omitempty"`
// WorkflowID is the workflow the trigger starts, and Workflow its name.
WorkflowID uuid.UUID `json:"workflow_id"`
Workflow string `json:"workflow"`
Enabled bool `json:"enabled"`
Every string `json:"every,omitempty"`
// Status is pending_approval, armed or paused.
Status string `json:"status" enum:"pending_approval,armed,paused"`
EventTypeID string `json:"event_type_id,omitempty"`
@@ -608,7 +613,7 @@ func statusOf(t store.Trigger) string {
// viewOfTrigger is the API's form of a stored trigger. Unsigned is set by the
// caller, which knows the sources.
func viewOfTrigger(t store.Trigger) (triggerView, error) {
view := triggerView{Name: t.Name, EventType: t.EventType, Workflow: t.Workflow, Enabled: t.Enabled, Filter: map[string]string{}, Status: statusOf(t)}
view := triggerView{Name: t.Name, EventType: t.EventType, WorkflowID: t.WorkflowID, Enabled: t.Enabled, Filter: map[string]string{}, Status: statusOf(t)}
if len(t.Filter) > 0 {
if err := json.Unmarshal(t.Filter, &view.Filter); err != nil {
return view, fmt.Errorf("filter of %s: %w", t.Name, err)
@@ -635,6 +640,7 @@ func (m *Module) viewOfTrigger(ctx context.Context, t store.Trigger) (triggerVie
if err != nil {
return view, err
}
view.Workflow = m.workflowName(ctx, t.WorkflowID)
if t.SourceID.Valid {
if src, err := m.store.Source(ctx, m.orgID, t.SourceID.UUID); err == nil {
view.Source, view.Unsigned = src.Label, src.Verification == store.None
@@ -670,6 +676,7 @@ func (m *Module) triggers(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]tr
return nil, huma.Error500InternalServerError("couldn't read the filter of " + t.Name)
}
view.Source, view.Unsigned = src.Label, src.Verification == store.None
view.Workflow = m.workflowName(ctx, t.WorkflowID)
out = append(out, view)
}
return httpapi.OK(out), nil