An approval, ask or park can be resolved by an operator, a rule, a classifier or automatically, so the vocabulary stops naming a person: - step type human.approve -> workflow.approve - park class needs_human -> needs_decision (ParkNeedsDecision) - event kinds human_* -> operator_* - alert kind task.needs_human -> task.needs_decision - status grouping "needs you" -> "needs attention" 0061 rewrites the stored values, including the approval step type in the workflow-definitions resource and each task's pinned definition, with the digest recomputed so resumed tasks still match.
61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package messaging
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
// ApprovalRequester asks an operator to approve or deny a named action.
|
|
type ApprovalRequester interface {
|
|
RequestApproval(ctx context.Context, action, description string) (ApprovalDecision, error)
|
|
}
|
|
|
|
// ApprovalDecision is the outcome of an operator consent request.
|
|
type ApprovalDecision int
|
|
|
|
const (
|
|
// ApprovalApproved means the operator approved this one execution.
|
|
ApprovalApproved ApprovalDecision = iota
|
|
|
|
// ApprovalPermanentlyApproved means the operator approved this action
|
|
// for the lifetime of the session.
|
|
ApprovalPermanentlyApproved
|
|
|
|
// ApprovalDenied means the operator explicitly refused.
|
|
ApprovalDenied
|
|
)
|
|
|
|
// ErrApprovalDenied is returned when the operator refuses the action.
|
|
var ErrApprovalDenied = errors.New("approval denied")
|
|
|
|
// ErrApprovalNotConfigured is returned when a tool requires approval but no
|
|
// ApprovalRequester is wired.
|
|
var ErrApprovalNotConfigured = errors.New("approval is not configured")
|
|
|
|
// ToolApprovalTimeout bounds how long dispatch waits for an operator to act.
|
|
const ToolApprovalTimeout = 2 * time.Minute
|
|
|
|
type approvalCtxKey struct{}
|
|
|
|
// WithApprovalRequester stores an ApprovalRequester on ctx.
|
|
func WithApprovalRequester(ctx context.Context, a ApprovalRequester) context.Context {
|
|
return context.WithValue(ctx, approvalCtxKey{}, a)
|
|
}
|
|
|
|
// ApprovalFromContext returns the ApprovalRequester stored on ctx.
|
|
func ApprovalFromContext(ctx context.Context) ApprovalRequester {
|
|
a, _ := ctx.Value(approvalCtxKey{}).(ApprovalRequester)
|
|
return a
|
|
}
|
|
|
|
// ApproverOf returns sender's ApprovalRequester when it reports the
|
|
// Approval capability and implements the interface.
|
|
func ApproverOf(sender any) (ApprovalRequester, bool) {
|
|
if !CapabilitiesOf(sender).Approval {
|
|
return nil, false
|
|
}
|
|
approver, ok := sender.(ApprovalRequester)
|
|
return approver, ok
|
|
}
|