package workflow import ( "fmt" "strings" "github.com/samcharles93/archie-core/internal/domain/workintake" ) // IntakeRule sends an issue carrying Label to Workflow. type IntakeRule struct { Label string `json:"label" title:"Label" doc:"Matched without regard to case."` Workflow string `json:"workflow" title:"Workflow"` } // IntakeRoutes is where an issue goes by its labels: the first rule, in list // order, that matches one of them, else Default. type IntakeRoutes struct { Rules []IntakeRule `json:"rules" title:"Rules"` Default string `json:"default" title:"Default workflow" doc:"Runs an issue no rule matches."` } // ShippedIntakeRoutes is the routing a fresh install starts with. An // unlabelled issue goes to triage to be classified. func ShippedIntakeRoutes() IntakeRoutes { return IntakeRoutes{ Rules: []IntakeRule{{Label: "bug", Workflow: "tdd"}, {Label: "feature", Workflow: "feasibility"}}, Default: "triage", } } // WorkflowFor returns the workflow an issue with these labels runs. func (r IntakeRoutes) WorkflowFor(labels []string) string { for _, rule := range r.Rules { for _, label := range labels { if strings.EqualFold(workintake.LabelValue(label), strings.TrimSpace(rule.Label)) { return rule.Workflow } } } return r.Default } // Validate rejects an empty label or workflow name and a missing default. func (r IntakeRoutes) Validate() error { for i, rule := range r.Rules { if strings.TrimSpace(rule.Label) == "" { return fmt.Errorf("intake routes: rule %d has no label", i+1) } if strings.TrimSpace(rule.Workflow) == "" { return fmt.Errorf("intake routes: rule %d has no workflow", i+1) } } if strings.TrimSpace(r.Default) == "" { return fmt.Errorf("intake routes: a default workflow is required") } return nil } // CheckDefined rejects a route naming a workflow that is not defined. func (r IntakeRoutes) CheckDefined(available map[string]struct{}) error { for _, rule := range r.Rules { if _, ok := available[rule.Workflow]; !ok { return fmt.Errorf("intake routes: label %q names workflow %q, which is not defined", rule.Label, rule.Workflow) } } if _, ok := available[r.Default]; !ok { return fmt.Errorf("intake routes: the default names workflow %q, which is not defined", r.Default) } return nil } // ResolveWorkflowID deterministically selects a definition without compiling // or executing it. It is used before worker dispatch to pin the exact YAML. func ResolveWorkflowID(t *Task, routes IntakeRoutes, available map[string]struct{}) (string, error) { id := t.Workflow if id == "" { id = routes.WorkflowFor(workintake.SplitLabels(t.Labels)) } if _, ok := available[id]; !ok { return "", fmt.Errorf("workflow %q is not defined", id) } return id, nil }