Files
archie/internal/daemon/binding_target.go
T
sam 804d306f75
Quality / check (pull_request) Canceled after 0s
ci/woodpecker/pr/quality Pipeline was successful
fix(daemon): skip issue intake when the routed workflow is disabled (#1913)
pollNATS queued every discovered issue and let the pin path park it with
'workflow triage is disabled'. Now, after the TaskByIssue existence check
and before PublishTask, the label-routed workflow is resolved against the
active definitions and checked against the identity org's enablement, the
same filter dispatchOrgBindings applies to armed bindings. A disabled
route logs one Info line and returns false without acknowledging, so the
issue is rediscovered on a later poll and picked up if the workflow is
re-enabled. Read failures of org, definitions or enablement log a Warn
and fall through to publishing; no routable definition keeps the current
publish behaviour. processNATSTask's park path is unchanged.
2026-11-13 00:42:40 +11:00

126 lines
4.8 KiB
Go

package daemon
import (
"context"
"fmt"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/workflow"
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
// bindingTarget is what one binding dispatch starts: the repository the task
// works on (empty for none) and the workflow inputs it carries.
type bindingTarget struct {
owner, repo string
inputs map[string]any
// declared is whether the workflow declares inputs; such a task's body is
// provenance only, since its data travels as inputs.
declared bool
}
// activeWorkflows returns the definitions a dispatch cycle checks bindings
// against, the same set a task is later pinned from.
func (d *Daemon) activeWorkflows(ctx context.Context) (workflow.WorkflowDefinitionCollection, error) {
if d.WorkflowDefinitions == nil {
return workflow.ShippedDefinitions(), nil
}
collection, _, err := d.WorkflowDefinitions.WorkflowDefinitions(ctx)
return collection, err
}
// workflowEnablement returns which workflows each org has disabled; with no
// source wired every workflow is enabled.
func (d *Daemon) workflowEnablement(ctx context.Context) (workflowtask.WorkflowEnablement, error) {
if d.WorkflowEnablement == nil {
return workflowtask.WorkflowEnablement{}, nil
}
return d.WorkflowEnablement.WorkflowEnablement(ctx)
}
// disabledRoutedWorkflow reports the workflow an issue's labels route to and
// whether the identity's org has that workflow disabled, matching the
// enablement filter binding dispatch applies. A read failure (org,
// definitions, enablement) is an error so the caller falls through to
// publishing rather than dropping the issue; no routable definition is not an
// error, it is reported as not disabled and the pin path parks the task.
func (d *Daemon) disabledRoutedWorkflow(ctx context.Context, labels, identityID string) (routed string, disabled bool, err error) {
orgID, err := d.identityOrg(ctx, identity.IdentityID(identityID))
if err != nil {
return "", false, err
}
definitions, err := d.activeWorkflows(ctx)
if err != nil {
return "", false, err
}
available := make(map[string]struct{}, len(definitions.Definitions))
for _, definition := range definitions.Definitions {
available[definition.ID] = struct{}{}
}
routed, resolveErr := workflow.ResolveWorkflowID(&workflow.Task{Labels: labels}, available)
if resolveErr == nil {
enablement, err := d.workflowEnablement(ctx)
if err != nil {
return "", false, err
}
return routed, !enablement.Enabled(orgID, routed), nil
}
// No routable definition is a normal outcome here, not a read failure:
// keep the publish behaviour and let the pin path park the task.
return "", false, nil
}
// resolveBindingTarget checks a matched binding against the workflow it
// targets and resolves the task's repository and inputs. A non-empty reason
// means the binding does not dispatch and the reason is recorded on it.
func (d *Daemon) resolveBindingTarget(b binding.Binding, values map[string]any, workflows workflow.WorkflowDefinitionCollection) (target bindingTarget, reason string, ok bool) {
entry, found := workflows.DefinitionByID(b.Workflow)
if !found {
return bindingTarget{}, fmt.Sprintf("workflow %q is not defined", b.Workflow), false
}
iface, err := workflowtask.ParseWorkflowInterface(entry.YAML)
if err != nil {
return bindingTarget{}, fmt.Sprintf("workflow %q: %v", b.Workflow, err), false
}
target.inputs = b.ResolveInputs(values)
target.declared = len(iface.Inputs) > 0
if err := iface.CheckInputs(target.inputs); err != nil {
return bindingTarget{}, "inputs do not match the workflow: " + err.Error(), false
}
switch mode := iface.RepositoryMode(); {
case mode == workflowtask.RepositoryNone:
return target, "", true
case b.RepoParam != "":
owner, repo, _, err := b.RepositoryFrom(values)
if err != nil {
return bindingTarget{}, err.Error(), false
}
if !d.repoConfigured(owner, repo) {
return bindingTarget{}, fmt.Sprintf("repository %s/%s from the event is not a configured repository", owner, repo), false
}
target.owner, target.repo = owner, repo
return target, "", true
case mode == workflowtask.RepositoryOptional && b.Owner == "":
return target, "", true
}
owner, repo, reason := d.resolveBindingRepo(b)
if reason != "" {
return bindingTarget{}, reason, false
}
target.owner, target.repo = owner, repo
return target, "", true
}
// repoConfigured reports whether owner/repo is one of the configured repos. A
// repository named by an event must be one, so a payload cannot point an
// agent at an arbitrary repository.
func (d *Daemon) repoConfigured(owner, repo string) bool {
for _, r := range d.Cfg.Get().Repos {
if r.Owner == owner && r.Name == repo {
return true
}
}
return false
}