Cancelling a run moved it and the runs it called to cancelled and wrote nothing to either trail, so a stopped run could not say when it stopped or who asked. The stop now returns the runs it stopped and records one cancel entry on each, in the same transaction, naming the principal that asked. The entry is a step named cancel with the caller's identity, so the run detail shows it with the rest of the trail.
89 lines
3.2 KiB
Go
89 lines
3.2 KiB
Go
package approvals
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/deliveries"
|
|
"github.com/samcharles93/archie-core/modules/governance"
|
|
)
|
|
|
|
// ChangeRequests manages external change requests for actions that require them.
|
|
type ChangeRequests interface {
|
|
// Open must be idempotent for the same approval request.
|
|
Open(ctx context.Context, r Request) error
|
|
// Withdraw withdraws r's change request if it is still open.
|
|
Withdraw(ctx context.Context, r Request) error
|
|
}
|
|
|
|
// UseChangeRequests configures the change-request connector. Without one,
|
|
// approval requests still go to the approvers' channels.
|
|
func (m *Module) UseChangeRequests(c ChangeRequests) { m.changes = c }
|
|
|
|
// Escalate advances a pending request to step n. It sets the step's deadline
|
|
// relative to the time of escalation.
|
|
func (m *Module) Escalate(ctx context.Context, id uuid.UUID, n int, to governance.PathStep) error {
|
|
row, err := m.store.ByID(ctx, m.orgID, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if row.State != StatePending {
|
|
return nil
|
|
}
|
|
until := m.now().UTC().Add(to.Deadline())
|
|
who := "the next person on the escalation path"
|
|
if to.Name != "" {
|
|
who = to.Name
|
|
}
|
|
// Send the notice before updating the request. If the update fails,
|
|
// retrying with the same key won't duplicate the notice.
|
|
if err := m.tell.Notify(ctx, deliveries.Notification{
|
|
RunID: row.RunID, Key: row.Key + ":escalated:" + strconv.Itoa(n),
|
|
Title: "Approval escalated: " + named(row),
|
|
Body: "No one responded before the previous deadline. Next approver: " + who + ". Response deadline: " + until.Format("2006-01-02 15:04 UTC") + ". " + row.Summary,
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
if _, _, err := m.store.Escalate(ctx, m.orgID, id, n, until); err != nil {
|
|
return err
|
|
}
|
|
m.log.InfoContext(ctx, "approval request escalated", "request", id, "step", n)
|
|
return nil
|
|
}
|
|
|
|
// Accept counts an approval towards an increase in autonomy just before the
|
|
// action runs. Each approval counts at most once; discarded approvals do not count.
|
|
func (m *Module) Accept(ctx context.Context, id uuid.UUID) error {
|
|
row, counted, err := m.store.Count(ctx, m.orgID, id)
|
|
if err != nil || !counted {
|
|
return err
|
|
}
|
|
return m.learn(ctx, row, Answer{
|
|
RequestID: row.ID, Approved: true, By: row.AnsweredBy.String, ByID: row.AnsweredByID.String, Via: row.AnsweredVia.String, At: row.AnsweredAt.Time,
|
|
})
|
|
}
|
|
|
|
// Discard records why an approval is no longer valid and notifies the approval
|
|
// channels. A discarded approval cannot count towards an autonomy proposal.
|
|
func (m *Module) Discard(ctx context.Context, id uuid.UUID, because string) error {
|
|
row, discarded, err := m.store.Discard(ctx, m.orgID, id, because)
|
|
if err != nil || !discarded {
|
|
return err
|
|
}
|
|
m.log.InfoContext(ctx, "approval discarded", "request", id, "because", because)
|
|
if err := m.tell.Told(ctx, deliveries.ApprovalRequest{
|
|
RunID: row.RunID, RequestID: row.ID, Key: row.Key + ":ask",
|
|
Title: "Approval discarded", Body: because,
|
|
}, "Discarded: "+because); err != nil {
|
|
return fmt.Errorf("tell discard: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ErrNoPath indicates that an approval request has no escalation path.
|
|
var ErrNoPath = errors.New("an approval request needs an escalation path")
|