Label to workflow rules and a default workflow are the intake-routes resource: ordered, case-insensitive, first rule wins, seeded with bug to tdd, feature to feasibility, default triage. A write naming an undefined workflow or an empty label is refused. The daemon reads it through the workflow definitions client when pinning a task and at intake. The kind indirection only carried this routing, so it is removed: the per-kind NATS subjects (every consumer already bound archie.task.>) are one subject, the envelope and playbook triggers lose their kind, and x-intake-routes is dropped. The dashboard gets an Issue routing page under Automation; the trigger card reads the same setting.
168 lines
7.4 KiB
Go
168 lines
7.4 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/access"
|
|
"github.com/samcharles93/archie-core/internal/domain/binding"
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
"github.com/samcharles93/archie-core/internal/taskstate"
|
|
)
|
|
|
|
type workflowPrincipals map[identity.IdentityID]org.OrgID
|
|
|
|
func (p workflowPrincipals) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
|
|
owner, ok := p[id]
|
|
if !ok {
|
|
return access.Principal{}, fmt.Errorf("principal %q unavailable", id)
|
|
}
|
|
return access.Principal{IdentityID: id, Org: owner}, nil
|
|
}
|
|
|
|
// memberOf is a person who serves one org and holds a membership in another.
|
|
type memberOf struct{ home, other org.OrgID }
|
|
|
|
func (m memberOf) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) {
|
|
return access.Principal{IdentityID: id, Org: m.home, Memberships: []org.Membership{{IdentityID: id, OrgID: m.home}, {IdentityID: id, OrgID: m.other}}}, nil
|
|
}
|
|
|
|
// listsWorkspaces lets a principal select an org, as the State Store client does.
|
|
type listsWorkspaces struct{ *identityStore }
|
|
|
|
func (listsWorkspaces) ListWorkspaces(context.Context, org.OrgID) ([]org.Workspace, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
type orgBindingStore struct {
|
|
storecontract.TaskStore
|
|
storecontract.BindingStore
|
|
storecontract.MappingStore
|
|
bindings []binding.Binding
|
|
started map[org.OrgID]*workflow.Task
|
|
}
|
|
|
|
func (s *orgBindingStore) ListBindings(context.Context) ([]binding.Binding, error) {
|
|
return s.bindings, nil
|
|
}
|
|
|
|
func (s *orgBindingStore) ListUndispatchedCaptures(_ context.Context, bindings []string, _ int) ([]storecontract.CapturedEvent, error) {
|
|
var captures []storecontract.CapturedEvent
|
|
for _, b := range s.bindings {
|
|
if slices.Contains(bindings, b.ID) {
|
|
captures = append(captures, storecontract.CapturedEvent{ID: b.Matcher.Source, Source: b.Matcher.Source, EventType: b.Matcher.Source, Body: "{}", Authenticated: true})
|
|
}
|
|
}
|
|
return captures, nil
|
|
}
|
|
|
|
func (*orgBindingStore) RecordDispatch(context.Context, string, int64, string, int64, string) error {
|
|
return nil
|
|
}
|
|
func (*orgBindingStore) InsertEvent(context.Context, events.Event) (int64, error) { return 0, nil }
|
|
|
|
func (*orgBindingStore) GetMapping(_ context.Context, id string) (*mapping.Mapping, error) {
|
|
return &mapping.Mapping{EventTypeID: id}, nil
|
|
}
|
|
|
|
func (s *orgBindingStore) EnqueueBindingTask(ctx context.Context, owner, repo, _, _, wf, identity, bindingID string, _ int, _ string, _ map[string]any) (*workflow.Task, error) {
|
|
run := &workflow.Task{ID: 1, Owner: owner, Repo: repo, Workflow: wf, Identity: identity, Org: org.OrgFromContext(ctx), BindingID: bindingID}
|
|
s.started[run.Org] = run
|
|
return run, nil
|
|
}
|
|
|
|
func TestDispatchOrgWorkflows(t *testing.T) {
|
|
for _, disabled := range []bool{false, true} {
|
|
t.Run(fmt.Sprintf("acme disabled %v", disabled), func(t *testing.T) {
|
|
store := &orgBindingStore{started: map[org.OrgID]*workflow.Task{}, bindings: []binding.Binding{
|
|
{ID: "root", MappingID: "root", Matcher: binding.Matcher{Source: "root"}, OrgID: org.DefaultOrgID, Workflow: "deploy", Status: binding.StatusArmed, Owner: "owner", Repo: "repo"},
|
|
{ID: "acme", MappingID: "acme", Matcher: binding.Matcher{Source: "acme"}, OrgID: "acme", Workflow: "deploy", Status: binding.StatusArmed},
|
|
}}
|
|
d := wired(&Daemon{Cfg: config.NewHolder(config.Config{}), Store: store, Log: slog.New(slog.DiscardHandler), Identity: NewIdentityGate(neutralIdentities{}, neutralAssignments{}, allowAll{}, workflowPrincipals{sys: org.DefaultOrgID, "acme-bot": "acme"}, neutralDenials{}, nil, nil, slog.New(slog.DiscardHandler)), WorkflowDefinitions: orgDefinitions{
|
|
org.DefaultOrgID: "id: deploy\nrepository: required\nsteps:\n - type: workflow.finish\n",
|
|
"acme": "id: deploy\nrepository: none\nsteps:\n - type: workflow.finish\n",
|
|
}})
|
|
d.BindingDispatch.bindings, d.BindingDispatch.dispatcher = store, store
|
|
d.BindingDispatch.creator, d.BindingDispatch.mappings = store, store
|
|
d.BindingDispatch.orgPrincipals = func(context.Context) []access.Principal {
|
|
return []access.Principal{{IdentityID: identity.SystemID, Org: org.DefaultOrgID}, {IdentityID: "acme-bot", Org: "acme"}}
|
|
}
|
|
if disabled {
|
|
d.BindingDispatch.enablement = enablementOf(workflowtask.WorkflowEnablement{}.SetEnabled("acme", "deploy", false))
|
|
}
|
|
d.BindingDispatch.dispatchBindings(t.Context())
|
|
root := store.started[org.DefaultOrgID]
|
|
if root == nil || root.Repo != "repo" || root.Identity != string(sys) {
|
|
t.Fatalf("root dispatch %#v", root)
|
|
}
|
|
acme := store.started["acme"]
|
|
if disabled {
|
|
if acme != nil {
|
|
t.Fatalf("disabled org dispatched %#v", acme)
|
|
}
|
|
return
|
|
}
|
|
if acme == nil || acme.Repo != "" || acme.Identity != "acme-bot" {
|
|
t.Fatalf("org's repo-free workflow did not dispatch as its identity: %#v", acme)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type orgDefinitions map[org.OrgID]string
|
|
|
|
func (d orgDefinitions) WorkflowDefinitions(ctx context.Context) (workflow.WorkflowDefinitionCollection, int64, error) {
|
|
owner := org.DefaultOrgID
|
|
if p, ok := access.PrincipalFromContext(ctx); ok && p.Org != "" {
|
|
owner = p.Org
|
|
}
|
|
return workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "deploy", YAML: d[owner]}}}, 3, nil
|
|
}
|
|
|
|
func (orgDefinitions) IntakeRoutes(context.Context) (workflow.IntakeRoutes, error) {
|
|
return workflow.ShippedIntakeRoutes(), nil
|
|
}
|
|
|
|
func TestPinWorkflowInTaskOrg(t *testing.T) {
|
|
const shared = "id: deploy\nsteps:\n - type: workflow.finish\n"
|
|
const custom = "id: deploy\nrepository: none\nsteps:\n - type: workflow.finish\n"
|
|
for _, tc := range []struct {
|
|
name string
|
|
owner org.OrgID
|
|
principals access.PrincipalSource
|
|
want string
|
|
refused bool
|
|
}{
|
|
{"non-default org definition", "acme", workflowPrincipals{"bot": "acme"}, custom, false},
|
|
{"another org cannot supply the definition", "acme", workflowPrincipals{"bot": org.DefaultOrgID}, "", true},
|
|
{"a member of the task's org may start it from another", "acme", memberOf{home: org.DefaultOrgID, other: "acme"}, custom, false},
|
|
{"a non-member cannot start a task in another org", "acme", memberOf{home: org.DefaultOrgID, other: "beta"}, "", true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
store := listsWorkspaces{&identityStore{}}
|
|
d := wired(&Daemon{Store: store, Identity: NewIdentityGate(neutralIdentities{}, neutralAssignments{}, allowAll{}, tc.principals, neutralDenials{}, nil, store, slog.New(slog.DiscardHandler)), WorkflowDefinitions: orgDefinitions{org.DefaultOrgID: shared, "acme": custom}})
|
|
task := &workflow.Task{ID: 1, Identity: "bot", Org: tc.owner, Workflow: "deploy"}
|
|
err := d.pinWorkflowDefinition(t.Context(), task)
|
|
if tc.refused {
|
|
if err == nil || pinParkClass(err) != taskstate.ParkNeedsDecision || task.WorkflowDefinitionYAML != "" {
|
|
t.Fatalf("cross-org pin accepted: task %#v err %v", task, err)
|
|
}
|
|
return
|
|
}
|
|
if err != nil || task.WorkflowDefinitionYAML != tc.want {
|
|
t.Fatalf("pinned %q, want %q; err %v", task.WorkflowDefinitionYAML, tc.want, err)
|
|
}
|
|
})
|
|
}
|
|
}
|