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 is a change-request connector. Judgement's change-request // rules decide which held actions need one; this opens and withdraws them. type ChangeRequests interface { // Open raises the change request for r. It may be called again for the same // request and must not open a second one. 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 makes the module open the change requests judgement asks // for through c. Without one, those approvals go to the approvers' channels, // so a rule never strands a request where nobody can answer it. func (m *Module) UseChangeRequests(c ChangeRequests) { m.changes = c } // Escalate passes a pending request to step n of its path, telling every // channel who it has reached and until when. The step's deadline runs from // now. A request that has since been answered is left alone. 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 } // The notice goes first and is idempotent by key, so an escalation retried // after the move failed still tells people once. if err := m.tell.Notify(ctx, deliveries.Notification{ RunID: row.RunID, Key: row.Key + ":escalated:" + strconv.Itoa(n), Title: "Approval escalated: " + row.Connector + " " + row.Action, Body: "Nobody answered in time, so it now goes to " + who + " and waits until " + until.Format("2006-01-02 15:04 UTC") + ". " + row.Summary, }); err != nil { return err } if _, _, err := m.store.Escalate(ctx, m.orgID, id, n, to.Person, until); err != nil { return err } m.log.InfoContext(ctx, "approval request escalated", "request", id, "step", n) return nil } // Accept counts an approval toward raising its action class, once, when the // action it covers is about to run. An approval that is discarded first never // counts. 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, Via: row.AnsweredVia.String, At: row.AnsweredAt.Time, }) } // Discard withdraws an approval whose action changed before it could run, and // says why. A discarded approval doesn't count toward a raise, and a request // for the changed action is a new one. 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 means a request was asked without anyone to put it to. var ErrNoPath = errors.New("an approval request needs an escalation path")