Files
mesh-catalog/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/owedacts.go
T

87 lines
3.1 KiB
Go

package main
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"strings"
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdelivery"
)
// What the gate owes the forge for a change of a delivery record (design 47 G A-39, A-35, §158, §161 to §167): every change of
// a record, a step's outcome applied or the record ended, says a line on the commit's note of each member pull request's head
// (`<at> record <key>: <what changed> (<why>)`) and replaces each member's delivery view with what `show` answers for the
// record; a comment is said only when the record ends failed or held, naming why. Each act goes through owe (owed.go): kept in
// the record's acts, said as the seat's event owed, and ended on owed.ended.
// RecordChange is one change of a record: what changed and why; Ended when it is the record's end.
type RecordChange struct {
What, Why string
Ended bool
}
// outcomeWords is what an outcome applied says changed.
func outcomeWords(o RecordOutcome) string {
where := o.Module
if o.Node != "" {
where += " on " + o.Node
}
return strings.TrimSpace(fmt.Sprintf("%s %s %s", where, o.Step, o.Outcome))
}
// recordChanged owes the forge what a change of the record asks, for each of its pull requests. An act that cannot be said is
// kept in the record all the same, said again at the gate's start (§165), and shows in the status as refused.
func (h *Holder) recordChanged(r *Record, c RecordChange) {
if h.Owed == nil {
return
}
now := h.Now().UTC()
line := fmt.Sprintf("%s record %s: %s (%s)", now.Format("2006-01-02T15:04:05Z"), r.Instance, c.What, c.Why)
view, err := json.MarshalIndent(map[string]any{"records": []recordView{viewOf(r)}}, "", " ")
if err != nil {
h.Refuse(fmt.Errorf("the record %s cannot be shown on its pull requests: %w", r.Instance, err))
return
}
var comment string
if c.Ended {
comment = endedFailedOrHeld(r)
}
for _, m := range membersOf(r) {
if m.Number <= 0 || m.Head == "" {
continue
}
pull := m.Repository + "#" + strconv.FormatInt(m.Number, 10)
acts := []seat.Owed{
{Act: seat.OwedActNote, Line: line, Commit: m.Head},
{Act: seat.OwedActView, Text: string(view)},
}
if comment != "" {
acts = append(acts, seat.Owed{Act: seat.OwedActComment, Text: comment})
}
for _, o := range acts {
o.Repository, o.Number, o.Head = m.Repository, m.Number, m.Head
o.ID = actID(o.Act+"."+r.Instance+"."+pull, now, o.Line+o.Text)
if err := h.owe(r, o); err != nil {
h.Refuse(fmt.Errorf("the %s owed on %s for the record %s is not said: %w", o.Act, pull, r.Instance, err))
}
}
}
}
// endedFailedOrHeld is the comment a record ended says when one of its outcomes is failed or held, naming why; "" otherwise.
func endedFailedOrHeld(r *Record) string {
var lines []string
for _, o := range r.Outcomes {
if o.Outcome == OutcomeFailed || o.Outcome == OutcomeHeld {
lines = append(lines, fmt.Sprintf("- %s: %s", outcomeWords(o), firstOf(o.Why, "no reason said")))
}
}
if len(lines) == 0 {
return ""
}
sort.Strings(lines)
return fmt.Sprintf("The delivery record %s ended (%s), with outcomes failed or held:\n%s", r.Instance, r.Ended.Why, strings.Join(lines, "\n"))
}