An owed act met by the forge away (401 while the store was replaced) ended could not be performed and nothing performed it again. Each act is now a kept call at the module's pace (kept-call-pace-first, kept-call-pace-max) until owed-away-bound (60 minutes), then ends could not be performed naming every try. Before each try the bridge reads the pull request through the seat git's pull: one closed unmerged ends owed.ended not-needed, never performed. The gate's owed event gains record (the delivery record's source revision), kept with the act in performed, and bridge_owed_again by record and why performs again every act of that record that ended could not be performed, written in the hand-act log first. Builds: design 47 H2 §807, §775, §819, H-A-432
122 lines
4.8 KiB
Go
122 lines
4.8 KiB
Go
package bridge
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
git "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git"
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
|
)
|
|
|
|
// OwedCall is one act owed to the forge as the seat git's verb that performs it: the seat that said it (Of), the act's
|
|
// id, and the verb with its input (novox/hq design 47 *What the gate owes the forge*). Every verb is safe to repeat at
|
|
// the forge: a status and the view replace what stands, and a comment and a note line carry the id, which the seat's holder skips when it already holds it.
|
|
type OwedCall struct {
|
|
Of string
|
|
ID string
|
|
Act string
|
|
Repository string
|
|
Number int
|
|
// Record is the delivery record the act is for, its source revision (design 47 H2 §807, H-A-432 (1)); empty when the
|
|
// owed named none.
|
|
Record string
|
|
Verb string
|
|
Input any
|
|
}
|
|
|
|
// ErrUndecodable is an owed event the bridge cannot read: it is said as owed.ended `undecodable`, never only logged.
|
|
var ErrUndecodable = errors.New("an owed act the bridge cannot read")
|
|
|
|
func undecodable(why string, args ...any) error {
|
|
return fmt.Errorf("%w: %s", ErrUndecodable, fmt.Sprintf(why, args...))
|
|
}
|
|
|
|
// GateOwed reads the seat mesh-delivery's event owed into the seat git's verb that performs it.
|
|
func GateOwed(data []byte) (OwedCall, error) {
|
|
var o seat.OwedEvent
|
|
if err := json.Unmarshal(data, &o); err != nil {
|
|
return OwedCall{}, undecodable("the gate's owed is not json: %v", err)
|
|
}
|
|
c := OwedCall{Of: seat.Seat, ID: o.ID, Act: o.Act, Repository: o.Repository, Number: int(o.Number), Record: o.Record}
|
|
if err := c.common(o.Head); err != nil {
|
|
return c, err
|
|
}
|
|
commit := o.Commit
|
|
if commit == "" {
|
|
commit = o.Head
|
|
}
|
|
needsPull := func() error {
|
|
if o.Number <= 0 {
|
|
return undecodable("the owed %s %s names no pull request", o.Act, o.ID)
|
|
}
|
|
return nil
|
|
}
|
|
switch o.Act {
|
|
case seat.OwedActStatus:
|
|
if o.Context == "" || o.State == "" {
|
|
return c, undecodable("the owed status %s names no context or state", o.ID)
|
|
}
|
|
c.Verb, c.Input = git.VerbStatus, git.StatusInput{ID: o.ID, Repository: o.Repository, Commit: commit, Context: o.Context,
|
|
State: o.State, Description: o.Description, Target: o.Target}
|
|
case seat.OwedActView:
|
|
if err := needsPull(); err != nil {
|
|
return c, err
|
|
}
|
|
c.Verb, c.Input = git.VerbView, git.ViewInput{ID: o.ID, Repository: o.Repository, Number: o.Number, Text: o.Text}
|
|
case seat.OwedActComment:
|
|
if err := needsPull(); err != nil {
|
|
return c, err
|
|
}
|
|
c.Verb, c.Input = git.VerbComment, git.CommentInput{ID: o.ID, Repository: o.Repository, Number: o.Number, Text: o.Text}
|
|
case seat.OwedActNote:
|
|
if o.Line == "" {
|
|
return c, undecodable("the owed note line %s says no line", o.ID)
|
|
}
|
|
c.Verb, c.Input = git.VerbNote, git.NoteInput{ID: o.ID, Repository: o.Repository, Commit: commit, Line: o.Line}
|
|
default:
|
|
return c, undecodable("the owed %s is the act %q, none the seat git performs", o.ID, o.Act)
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func (c OwedCall) common(head string) error {
|
|
switch {
|
|
case c.ID == "":
|
|
return undecodable("an owed %s of %s names no id", c.Act, c.Repository)
|
|
case c.Repository == "":
|
|
return undecodable("the owed %s names no repository", c.ID)
|
|
case !sha40.MatchString(head):
|
|
return undecodable("the owed %s names the head %q, not 40 hex digits", c.ID, head)
|
|
}
|
|
if _, _, err := splitRepository(c.Repository); err != nil {
|
|
return undecodable("the owed %s: %v", c.ID, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Outcome is what an act's call of the seat git came to, as owed.ended says it: performed when the forge holds the act
|
|
// (done now, or its id already held); refused with the forge's own words when the seat answers an error of the kind
|
|
// refused; could not be performed with the error when the seat did not answer, answered an error of the
|
|
// kind failed, or answered what the contract cannot read.
|
|
func Outcome(raw json.RawMessage, callErr error) (outcome, words string) {
|
|
if callErr != nil {
|
|
return forgemsg.OwedCouldNotBePerformed, callErr.Error()
|
|
}
|
|
var a git.StatusAnswer // every act verb's answer (status, comment, view, note) has these fields
|
|
if err := json.Unmarshal(raw, &a); err != nil {
|
|
return forgemsg.OwedCouldNotBePerformed, "the seat git's answer is not its contract's: " + err.Error()
|
|
}
|
|
switch {
|
|
case a.Error != "" && a.ErrorKind == git.ErrorKindRefused:
|
|
return forgemsg.OwedRefused, a.Error
|
|
case a.Error != "":
|
|
// failed, or a kind the contract does not name: the forge did not do it, and did not say no
|
|
return forgemsg.OwedCouldNotBePerformed, a.Error
|
|
case a.Performed || a.Held:
|
|
return forgemsg.OwedPerformed, ""
|
|
}
|
|
return forgemsg.OwedCouldNotBePerformed, "the seat git's answer says neither performed, held nor an error: " + string(raw)
|
|
}
|