feat(scheduling): resolve the issue discovery rule per organisation
This commit is contained in:
1 parent
b8e080301c
commit
8150df8d81
12 files changed
+168
-35
No files matched your search
@@ -126,12 +126,18 @@ func (b *boot) issueSources(ctx context.Context) []daemon.IssueSource {
|
||||
}
|
||||
byConnection[repo.Connection] = append(byConnection[repo.Connection], repo)
|
||||
}
|
||||
rule, err := b.discoveryRule(octx, o.owner)
|
||||
if err != nil {
|
||||
log.WarnContext(ctx, "issue discovery: scheduling policy unavailable", "err", err)
|
||||
continue
|
||||
}
|
||||
for _, connection := range connections {
|
||||
bound := byConnection[connection.ID]
|
||||
if len(bound) == 0 {
|
||||
continue
|
||||
}
|
||||
if source, ok := b.connectionSource(octx, o, connection, bound); ok {
|
||||
source.Dispatch, source.Label = rule.Dispatch, rule.Label
|
||||
out = append(out, source)
|
||||
}
|
||||
}
|
||||
@@ -139,6 +145,21 @@ func (b *boot) issueSources(ctx context.Context) []daemon.IssueSource {
|
||||
return out
|
||||
}
|
||||
|
||||
// discoveryRule is the dispatch rule an organisation's repositories are
|
||||
// polled with: its own scheduling policy over the daemon's, which already
|
||||
// carries the system organisation's.
|
||||
func (b *boot) discoveryRule(ctx context.Context, owner org.OrgID) (config.Config, error) {
|
||||
rule := b.cfgHolder.Get()
|
||||
if owner == org.DefaultOrgID {
|
||||
return rule, nil
|
||||
}
|
||||
_, _, err := b.controlPlane.Query(ctx, controlplanestore.SchedulingPolicyKind, func(value []byte) (err error) {
|
||||
rule, err = controlplanestore.ApplyOrgScheduling(rule, value)
|
||||
return err
|
||||
})
|
||||
return rule, err
|
||||
}
|
||||
|
||||
func (b *boot) connectionSource(ctx context.Context, o orgAgents, connection config.ForgeConnection, repos []config.Repo) (daemon.IssueSource, bool) {
|
||||
log := b.log.With("org", o.owner, "connection", connection.ID)
|
||||
for _, principal := range o.principals {
|
||||
|
||||
@@ -253,7 +253,11 @@ type IssueSource struct {
|
||||
Forge forge.Forge
|
||||
// BotUser is the connection's external account, the assignee polled for.
|
||||
BotUser string
|
||||
Repos []config.Repo
|
||||
// Dispatch and Label are the discovery rule the connection's org polls
|
||||
// with. Empty means the daemon's own.
|
||||
Dispatch config.Dispatch
|
||||
Label string
|
||||
Repos []config.Repo
|
||||
}
|
||||
|
||||
func (d *Daemon) issueSources(ctx context.Context) []IssueSource {
|
||||
@@ -1203,6 +1207,9 @@ func (d *Daemon) poll(ctx context.Context) error {
|
||||
}
|
||||
cfg := d.Cfg.Get()
|
||||
cfg.BotUser = source.BotUser
|
||||
if source.Dispatch.Trigger != "" {
|
||||
cfg.Dispatch, cfg.Label = source.Dispatch, source.Label
|
||||
}
|
||||
tag := d.sourceTag(source)
|
||||
for _, repo := range source.Repos {
|
||||
issues, complete := d.pollIssuesWithConfig(ctx, source.Forge, cfg, repo)
|
||||
|
||||
@@ -49,7 +49,7 @@ var dashboardPages = []DashboardPage{
|
||||
{Path: "/settings/task-execution", Label: "Task execution", Description: "Statuses, operator actions, budgets."},
|
||||
{Path: "/settings/models", Label: "Models", Description: "Instance model aliases and providers."},
|
||||
{Path: "/org/repositories", Label: "Repositories", Description: "Watched repositories and gate overrides."},
|
||||
{Path: "/settings/scheduling-policy", Label: "Scheduling policy", Description: "Work discovery and forge labels."},
|
||||
{Path: "/org/scheduling-policy", Label: "Scheduling policy", Description: "Work discovery and forge labels."},
|
||||
{Path: "/settings/tools", Label: "Tools & MCP", Description: "Agent tools and MCP servers."},
|
||||
{Path: "/settings/audit", Label: "Audit", Description: "Settings, access, identity and runtime events."},
|
||||
{Path: "/settings/plugins", Label: "Plugins", Description: "Extension directories loaded at startup."},
|
||||
|
||||
@@ -197,10 +197,21 @@ var orgSeededKinds = map[string]bool{
|
||||
ForgeConnectionsKind: true, RepositoryPoliciesKind: true,
|
||||
}
|
||||
|
||||
// orgEmptyDocuments are the kinds an org other than the system org reads as
|
||||
// an unset document until it writes one: its unset fields fall back to the
|
||||
// instance's, so nothing is stored for it and the instance's values are not
|
||||
// copied in.
|
||||
var orgEmptyDocuments = map[string]string{
|
||||
SchedulingPolicyKind: `{"poll_interval":"","max_retries":0,"dispatch":{"trigger":"","ack_reaction":""}}`,
|
||||
}
|
||||
|
||||
// resource seeds an org's workflow documents on first use, including orgs
|
||||
// created after this process started. File-derived settings are never copied.
|
||||
func (s *Server) resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) {
|
||||
resource, err := s.store.Resource(ctx, orgID, kind)
|
||||
if empty, ok := orgEmptyDocuments[kind]; ok && orgID != storecontract.DefaultOrgID && errors.Is(err, storecontract.ErrResourceNotFound) {
|
||||
return storecontract.Resource{OrgID: orgID, Kind: kind, Value: []byte(empty)}, nil
|
||||
}
|
||||
if !errors.Is(err, storecontract.ErrResourceNotFound) || !orgSeededKinds[kind] {
|
||||
// Serve the current channel schema so stored settings remain editable.
|
||||
if err == nil && kind == ChannelSettingsKind {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package controlplanestore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
@@ -104,22 +106,21 @@ func validateRepositories(input []byte) error {
|
||||
return validateAs(input, configuration.ValidateRepositories)
|
||||
}
|
||||
|
||||
// validateScheduling applies the rules every org's document shares. The
|
||||
// instance-wide fields are required of the system org and refused of every
|
||||
// other (checkSchedulingWrite).
|
||||
func validateScheduling(input []byte) error {
|
||||
return validateAs(input, func(policy schedulingPolicy) error {
|
||||
if policy.MaxRetries < 0 {
|
||||
return fmt.Errorf("max_retries must not be negative")
|
||||
}
|
||||
// A positive interval, not a non-negative one: the file layer's rule is
|
||||
// the same, and there is no defaulting behind a stored value -- a stored
|
||||
// "0s" reaches cfg.PollInterval as zero and stops archied starting.
|
||||
interval, err := time.ParseDuration(policy.PollInterval)
|
||||
if err != nil || interval <= 0 {
|
||||
return fmt.Errorf("poll_interval must be a positive duration")
|
||||
if policy.PollInterval != "" {
|
||||
interval, err := time.ParseDuration(policy.PollInterval)
|
||||
if err != nil || interval <= 0 {
|
||||
return fmt.Errorf("poll_interval must be a positive duration")
|
||||
}
|
||||
}
|
||||
// Same reason as the repository rules above: RuntimeConfig replaces
|
||||
// cfg.Dispatch from this resource, and the daemon refuses to start with a
|
||||
// trigger it does not know.
|
||||
if !configuration.DispatchTriggerValid(policy.Dispatch.Trigger) {
|
||||
if policy.Dispatch.Trigger != "" && !configuration.DispatchTriggerValid(policy.Dispatch.Trigger) {
|
||||
return fmt.Errorf("dispatch.trigger %q is not a discovery rule the daemon can poll with", policy.Dispatch.Trigger)
|
||||
}
|
||||
// A label-requiring trigger needs a label.
|
||||
@@ -130,6 +131,48 @@ func validateScheduling(input []byte) error {
|
||||
})
|
||||
}
|
||||
|
||||
// checkSchedulingWrite separates the instance's polling from an org's
|
||||
// discovery rule. The poll interval and retry cap belong to the one daemon
|
||||
// (RuntimeConfig layers them from the system org), so another org's copy
|
||||
// would parse and never apply; the system org's must still be complete,
|
||||
// because the daemon refuses to start without them.
|
||||
func checkSchedulingWrite(orgID string, value []byte) error {
|
||||
var policy schedulingPolicy
|
||||
if err := json.Unmarshal(value, &policy); err != nil {
|
||||
return err
|
||||
}
|
||||
if orgID == storecontract.DefaultOrgID {
|
||||
if policy.PollInterval == "" || policy.Dispatch.Trigger == "" {
|
||||
return fmt.Errorf("%w: poll_interval and dispatch.trigger are required", ErrValidation)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if policy.PollInterval != "" || policy.MaxRetries != 0 {
|
||||
return fmt.Errorf("%w: poll_interval and max_retries are instance settings; an org sets the dispatch rule and label only", ErrValidation)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ApplyOrgScheduling returns cfg with an org's discovery rule over the
|
||||
// instance's: a field the org leaves unset keeps the instance value, so an
|
||||
// org that has written nothing polls as it always has.
|
||||
func ApplyOrgScheduling(cfg config.Config, stored []byte) (config.Config, error) {
|
||||
var policy schedulingPolicy
|
||||
if err := json.Unmarshal(stored, &policy); err != nil {
|
||||
return config.Config{}, fmt.Errorf("decode %s: %w", SchedulingPolicyKind, err)
|
||||
}
|
||||
if policy.Dispatch.Trigger != "" {
|
||||
cfg.Dispatch.Trigger = policy.Dispatch.Trigger
|
||||
}
|
||||
if policy.Dispatch.AckReaction != "" {
|
||||
cfg.Dispatch.AckReaction = policy.Dispatch.AckReaction
|
||||
}
|
||||
if policy.Label != nil {
|
||||
cfg.Label = *policy.Label
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// validateUpdateSettings refuses a channel the update scripts cannot select.
|
||||
func validateUpdateSettings(input []byte) error {
|
||||
return validateAs(input, func(doc controlplanerpc.UpdateSettings) error {
|
||||
|
||||
@@ -183,11 +183,10 @@ func (s *Server) readResource(ctx context.Context, orgID, kind string, into any)
|
||||
// AgentProfile, WorkflowExecutionSettings, ExtensionSettings, UpdateSettings,
|
||||
// AuditRetention, ScheduleSettings (the zone every cron schedule reads in).
|
||||
// Pending an org owner of the channels they configure:
|
||||
// ChannelSettings, SchedulingPolicy, TaskPolicy (outbound notification
|
||||
// webhook).
|
||||
// ChannelSettings, TaskPolicy (outbound notification webhook).
|
||||
var instanceOnlyKinds = map[string]bool{
|
||||
ChannelSettingsKind: true, SchedulingPolicyKind: true,
|
||||
TaskPolicyKind: true, ToolSettingsKind: true,
|
||||
ChannelSettingsKind: true,
|
||||
TaskPolicyKind: true, ToolSettingsKind: true,
|
||||
PluginSettingsKind: true, ContainerRuntimePoliciesKind: true, AgentProfileKind: true,
|
||||
ExtensionSettingsKind: true, UpdateSettingsKind: true, WorkflowExecutionSettingsKind: true,
|
||||
AuditRetentionKind: true, ScheduleSettingsKind: true,
|
||||
@@ -210,6 +209,8 @@ func (s *Server) checkOrgWrite(ctx context.Context, orgID, kind string, value []
|
||||
if orgID != storecontract.DefaultOrgID {
|
||||
return s.checkOrgProviders(ctx, orgID, value)
|
||||
}
|
||||
case SchedulingPolicyKind:
|
||||
return checkSchedulingWrite(orgID, value)
|
||||
case ModelAliasesKind:
|
||||
return s.checkAliasWrite(ctx, orgID, value)
|
||||
case WorkflowDefinitionsKind:
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package controlplanestore
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
func TestSchedulingPolicyWritesStayWithinTheirOrg(t *testing.T) {
|
||||
tests := []struct {
|
||||
name, org, value string
|
||||
refused bool
|
||||
}{
|
||||
{"system org writes the full policy", storecontract.DefaultOrgID, `{"poll_interval":"1m","dispatch":{"trigger":"assignee"}}`, false},
|
||||
{"system org omits the poll interval", storecontract.DefaultOrgID, `{"dispatch":{"trigger":"assignee"}}`, true},
|
||||
{"another org sets its dispatch rule", "acme", `{"label":"go","dispatch":{"trigger":"label"}}`, false},
|
||||
{"another org sets the instance poll interval", "acme", `{"poll_interval":"1s","dispatch":{"trigger":"label"},"label":"go"}`, true},
|
||||
{"another org sets the instance retry cap", "acme", `{"max_retries":99}`, true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := (&Server{}).checkOrgWrite(t.Context(), tt.org, SchedulingPolicyKind, []byte(tt.value))
|
||||
if refused := errors.Is(err, ErrValidation); refused != tt.refused {
|
||||
t.Fatalf("refused = %v, want %v (err %v)", refused, tt.refused, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyOrgSchedulingKeepsTheInstanceRuleForUnsetFields(t *testing.T) {
|
||||
instance := config.Config{Label: "archie", Dispatch: config.Dispatch{Trigger: "assignee", AckReaction: "eyes"}}
|
||||
tests := []struct {
|
||||
name, stored string
|
||||
want config.Config
|
||||
}{
|
||||
{"org overrides trigger and label", `{"label":"go","dispatch":{"trigger":"label"}}`, config.Config{Label: "go", Dispatch: config.Dispatch{Trigger: "label", AckReaction: "eyes"}}},
|
||||
{"org leaves everything unset", `{}`, instance},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := ApplyOrgScheduling(instance, []byte(tt.stored))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Label != tt.want.Label || got.Dispatch != tt.want.Dispatch {
|
||||
t.Fatalf("got %+v/%q, want %+v/%q", got.Dispatch, got.Label, tt.want.Dispatch, tt.want.Label)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user