package runs import ( "context" "errors" "fmt" "slices" "strings" "github.com/samcharles93/archie-core/modules/runs/internal/store" ) // definitions is the stored workflows and drafts the catalogue operations read // and change. type definitions interface { Workflow(ctx context.Context, orgID, name string, version int) (store.Workflow, error) Workflows(ctx context.Context, orgID string) ([]store.Workflow, error) AddWorkflow(ctx context.Context, orgID, name, body, by string, expected *int) (store.Workflow, error) SetEnabled(ctx context.Context, orgID, name string, enabled bool) error DeleteWorkflow(ctx context.Context, orgID, name string) error DeleteDraft(ctx context.Context, orgID, name string) (bool, error) ActiveRuns(ctx context.Context, orgID, name string) (int, error) } // Referrers finds what outside the runs module names a workflow. type Referrers interface { // WorkflowTriggers names the triggers that start the workflow. WorkflowTriggers(ctx context.Context, workflow string) ([]string, error) } // UseReferrers tells the engine where to look for the triggers that start a // workflow. It must be called before the API is served. func (e *Engine) UseReferrers(r Referrers) { e.mu.Lock() defer e.mu.Unlock() e.referrers = r } var ( // ErrVersionConflict is returned by Publish when the workflow's latest // version is not the one the caller expected. ErrVersionConflict = store.ErrVersionConflict // ErrDisabled is returned when a start names a disabled workflow. ErrDisabled = errors.New("the workflow is disabled") // ErrNoWorkflow is returned when no workflow has the name. ErrNoWorkflow = store.ErrNoWorkflow ) // refusal is a start the workflow can't take as asked: its inputs are wrong or // it can't run yet. It is the caller's to fix, unlike a failure to reach storage. type refusal struct{ error } func (r refusal) Unwrap() error { return r.error } // IsRefusal reports whether err says a run can't start as asked. func IsRefusal(err error) bool { _, ok := errors.AsType[refusal](err) return ok } // ReferencedError says what keeps a workflow from being deleted. type ReferencedError struct { Workflow string // By names each trigger, workflow or run that still depends on it. By []string } func (e *ReferencedError) Error() string { return fmt.Sprintf("workflow %q is still used by %s", e.Workflow, strings.Join(e.By, ", ")) } // Publish stores body as the next version of the workflow name and discards // its draft. When expected is set, the latest version must be that one (zero // for a workflow not yet published), or the body is not stored. A body that // doesn't validate is stored as nothing. func (e *Engine) Publish(ctx context.Context, orgID, name, body, by string, expected *int) (int, error) { if _, err := e.validateDocument(ctx, body); err != nil { return 0, err } return e.publish(ctx, orgID, name, body, by, expected) } func (e *Engine) publish(ctx context.Context, orgID, name, body, by string, expected *int) (int, error) { latest, err := e.defs.Workflow(ctx, orgID, name, 0) if err != nil && !errors.Is(err, store.ErrNoWorkflow) { return 0, err } if expected != nil && latest.Version != *expected { return 0, ErrVersionConflict } version := latest.Version if version == 0 || normalise(latest.Body) != normalise(body) { w, err := e.defs.AddWorkflow(ctx, orgID, name, body, by, expected) if err != nil { return 0, err } version = w.Version } if _, err := e.defs.DeleteDraft(ctx, orgID, name); err != nil { e.log.WarnContext(ctx, "discard published draft", "workflow", name, "error", err) } return version, nil } // SetEnabled enables or disables a workflow. A disabled workflow is not // started by a trigger, another workflow or a person. func (e *Engine) SetEnabled(ctx context.Context, orgID, name string, enabled bool) error { return e.defs.SetEnabled(ctx, orgID, name, enabled) } // Delete removes a workflow with all its versions and its draft. It is refused, // naming what depends on the workflow, while a trigger or another workflow // names it or one of its runs is still going. func (e *Engine) Delete(ctx context.Context, orgID, name string) error { if _, err := e.defs.Workflow(ctx, orgID, name, 0); err != nil { return err } by, err := e.referencesTo(ctx, orgID, name) if err != nil { return err } if len(by) > 0 { return &ReferencedError{Workflow: name, By: by} } return e.defs.DeleteWorkflow(ctx, orgID, name) } func (e *Engine) referencesTo(ctx context.Context, orgID, name string) ([]string, error) { var by []string e.mu.RLock() referrers := e.referrers e.mu.RUnlock() if referrers != nil { triggers, err := referrers.WorkflowTriggers(ctx, name) if err != nil { return nil, fmt.Errorf("find triggers of %q: %w", name, err) } for _, t := range triggers { by = append(by, "trigger "+t) } } all, err := e.defs.Workflows(ctx, orgID) if err != nil { return nil, err } for _, w := range all { if w.Name == name { continue } doc, err := ParseDocument(w.Body) if err != nil { continue } if slices.Contains(workflowsNamedIn(doc.Steps), name) { by = append(by, "workflow "+w.Name) } } active, err := e.defs.ActiveRuns(ctx, orgID, name) if err != nil { return nil, err } if active > 0 { by = append(by, fmt.Sprintf("%d runs in progress", active)) } return by, nil } // workflowsNamedIn lists the workflows steps call or hand off to, however // deeply the steps are nested. func workflowsNamedIn(steps []Step) []string { var out []string for _, s := range steps { if s.Type == typeCall || s.Type == typeHandoff { var named struct { Workflow string `yaml:"workflow"` } if err := s.Settings.Decode(&named); err == nil && named.Workflow != "" { out = append(out, named.Workflow) } } for _, branch := range s.Parallel { out = append(out, workflowsNamedIn(branch)...) } if s.Switch != nil { for _, branch := range s.Switch.Cases { out = append(out, workflowsNamedIn(branch)...) } } } return out }