mesh-delivery-forge-bridge: performed keeps the outcome and the pull request, not the act
The state performed filled to 77% of its template's 64 MiB (19,530 keys, 2.6 KB each): the bridge kept every owed event whole beside its outcome. performed's value is now the outcome said as owed.ended, with when, and the repository and number the act names (groups.Performed); bridge_owed_again reads the act from the gate's stream owed on its pull request's subject through an ordered consumer, the stream being the act's one copy; bridge_owed reads one act or one pull request's acts, never the whole state, whose answer had grown past what the bus carries. Builds: design 47 G §163, §162, §168; H-A-181, H-A-182
This commit is contained in:
@@ -141,7 +141,7 @@ func main() {
|
|||||||
tools = append(tools, OwedTools(askState(owedAsks, groups.OwedAsksBucket(served), 25*time.Second, func(v []byte) bool {
|
tools = append(tools, OwedTools(askState(owedAsks, groups.OwedAsksBucket(served), 25*time.Second, func(v []byte) bool {
|
||||||
a, err := groups.DecodeOwedAsk(v)
|
a, err := groups.DecodeOwedAsk(v)
|
||||||
return err == nil && a.Done()
|
return err == nil && a.Done()
|
||||||
}), func() ([]OwedRow, error) { return performedRows(performed) }, func(what, why string) error {
|
}), func(f OwedFilter) ([]OwedRow, error) { return performedRows(performed, f) }, func(what, why string) error {
|
||||||
_, err := recorder.Record(meshhandactsclient.Request{What: what, Why: why, Cause: "owed-dropped"})
|
_, err := recorder.Record(meshhandactsclient.Request{What: what, Why: why, Cause: "owed-dropped"})
|
||||||
return err
|
return err
|
||||||
}, time.Now)...)
|
}, time.Now)...)
|
||||||
|
|||||||
+43
-5
@@ -94,17 +94,55 @@ func TestTheBridgeTools(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return json.Marshal(a)
|
return json.Marshal(a)
|
||||||
}
|
}
|
||||||
rows := []OwedRow{{Of: seat.Seat, ID: "a", Outcome: forgemsg.OwedPerformed, At: at}}
|
rows := []OwedRow{
|
||||||
|
{OwedEnded: forgemsg.OwedEnded{Of: seat.Seat, ID: "a", Outcome: forgemsg.OwedPerformed, At: at}, Repository: "novox/mesh-sdk", Number: 178},
|
||||||
|
{OwedEnded: forgemsg.OwedEnded{Of: seat.Seat, ID: "b", Outcome: forgemsg.OwedPerformed, At: at}, Repository: "novox/mesh-sdk", Number: 179}}
|
||||||
|
var filters []OwedFilter
|
||||||
|
performed := func(f OwedFilter) ([]OwedRow, error) {
|
||||||
|
filters = append(filters, f)
|
||||||
|
var out []OwedRow
|
||||||
|
for _, r := range rows {
|
||||||
|
if f.holds(r) {
|
||||||
|
out = append(out, r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
var recorded []string
|
var recorded []string
|
||||||
recordErr := error(nil)
|
recordErr := error(nil)
|
||||||
record := func(what, why string) error { recorded = append(recorded, what+": "+why); return recordErr }
|
record := func(what, why string) error { recorded = append(recorded, what+": "+why); return recordErr }
|
||||||
tools := map[string]func(map[string]any) (any, error){}
|
tools := map[string]func(map[string]any) (any, error){}
|
||||||
for _, tl := range OwedTools(ask, func() ([]OwedRow, error) { return rows, nil }, record, func() time.Time { return at }) {
|
for _, tl := range OwedTools(ask, performed, record, func() time.Time { return at }) {
|
||||||
tools[tl.Name] = tl.Run
|
tools[tl.Name] = tl.Run
|
||||||
}
|
}
|
||||||
out, err := tools[OwedTool](map[string]any{"of": seat.Seat})
|
// bridge_owed reads one act or one pull request's acts, never the whole state (H-A-181).
|
||||||
if m, _ := out.(map[string]any); err != nil || len(m["performed"].([]OwedRow)) != 1 || len(m["backlog"].([]groups.Backlog)) != 1 {
|
for _, c := range []struct {
|
||||||
t.Errorf("bridge_owed: %v %v", out, err)
|
name string
|
||||||
|
args map[string]any
|
||||||
|
ids []string
|
||||||
|
}{
|
||||||
|
{"one pull request", map[string]any{"repository": "novox/mesh-sdk", "number": float64(179)}, []string{"b"}},
|
||||||
|
{"one act", map[string]any{"of": seat.Seat, "id": "a"}, []string{"a"}},
|
||||||
|
{"the whole state", map[string]any{"of": seat.Seat}, nil},
|
||||||
|
{"a repository with no number", map[string]any{"repository": "novox/mesh-sdk"}, nil},
|
||||||
|
} {
|
||||||
|
n := len(filters)
|
||||||
|
out, err := tools[OwedTool](c.args)
|
||||||
|
if c.ids == nil {
|
||||||
|
if err == nil || len(filters) != n {
|
||||||
|
t.Errorf("bridge_owed of %s: %v %v, read %d; want refused, nothing read", c.name, out, err, len(filters)-n)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
m, _ := out.(map[string]any)
|
||||||
|
got, _ := m["performed"].([]OwedRow)
|
||||||
|
var ids []string
|
||||||
|
for _, r := range got {
|
||||||
|
ids = append(ids, r.ID)
|
||||||
|
}
|
||||||
|
if err != nil || !reflect.DeepEqual(ids, c.ids) || len(m["backlog"].([]groups.Backlog)) != 1 {
|
||||||
|
t.Errorf("bridge_owed of %s: %v %v, want %v", c.name, out, err, c.ids)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if out, err := tools[OwedAgainTool](map[string]any{"of": seat.Seat, "id": "a"}); err != nil || out.(map[string]any)["outcome"] != forgemsg.OwedPerformed ||
|
if out, err := tools[OwedAgainTool](map[string]any{"of": seat.Seat, "id": "a"}); err != nil || out.(map[string]any)["outcome"] != forgemsg.OwedPerformed ||
|
||||||
asked[len(asked)-1].Kind != groups.OwedAgain {
|
asked[len(asked)-1].Kind != groups.OwedAgain {
|
||||||
|
|||||||
@@ -21,26 +21,50 @@ const (
|
|||||||
OwedDropTool = "bridge_owed_drop"
|
OwedDropTool = "bridge_owed_drop"
|
||||||
)
|
)
|
||||||
|
|
||||||
// OwedRow is one act the bridge performed, as the state performed keeps it: the outcome said as owed.ended.
|
// OwedRow is one act the bridge performed, as the state performed keeps it: the outcome said as owed.ended, and the pull
|
||||||
type OwedRow = forgemsg.OwedEnded
|
// request the act names (groups.Performed).
|
||||||
|
type OwedRow struct {
|
||||||
|
forgemsg.OwedEnded
|
||||||
|
Repository string `json:"repository,omitempty"`
|
||||||
|
Number int `json:"number,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// performedRows reads every act the state performed holds; a key that is not an act (a group end's bookkeeping) is left.
|
// OwedFilter is what bridge_owed reads of performed: one act (its id, of narrowing it to one key) or one pull request's
|
||||||
func performedRows(s stdio.KeptState) ([]OwedRow, error) {
|
// acts (repository and number), never the whole state (novox/hq design 47 H-A-181).
|
||||||
keys, err := s.Keys()
|
type OwedFilter struct {
|
||||||
if err != nil {
|
Of, ID, Repository string
|
||||||
return nil, fmt.Errorf("the bridge's performed state cannot be listed: %w", err)
|
Number int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f OwedFilter) holds(r OwedRow) bool {
|
||||||
|
return (f.Of == "" || r.Of == f.Of) && (f.ID == "" || r.ID == f.ID) &&
|
||||||
|
(f.Repository == "" || (r.Repository == f.Repository && r.Number == f.Number))
|
||||||
|
}
|
||||||
|
|
||||||
|
// performedRows reads the acts of performed f names, newest first: one key when f names the seat and the id, else each
|
||||||
|
// key's value held to f. A key that is not an act (a group end's bookkeeping) is left.
|
||||||
|
func performedRows(s stdio.KeptState, f OwedFilter) ([]OwedRow, error) {
|
||||||
|
var keys []string
|
||||||
|
if f.Of != "" && f.ID != "" {
|
||||||
|
keys = []string{groups.PerformedKey(f.Of, f.ID)}
|
||||||
|
} else {
|
||||||
|
all, err := s.Keys()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("the bridge's performed state cannot be listed: %w", err)
|
||||||
|
}
|
||||||
|
keys = all
|
||||||
}
|
}
|
||||||
var rows []OwedRow
|
rows := []OwedRow{}
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
e, err := s.Get(k)
|
e, err := s.Get(k)
|
||||||
if err != nil || e == nil {
|
if err != nil || e == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var p struct {
|
var p groups.Performed
|
||||||
Ended OwedRow `json:"ended"`
|
|
||||||
}
|
|
||||||
if json.Unmarshal(e.Value, &p) == nil && p.Ended.Of != "" {
|
if json.Unmarshal(e.Value, &p) == nil && p.Ended.Of != "" {
|
||||||
rows = append(rows, p.Ended)
|
if r := (OwedRow{OwedEnded: p.Ended, Repository: p.Repository, Number: p.Number}); f.holds(r) {
|
||||||
|
rows = append(rows, r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sort.Slice(rows, func(i, j int) bool { return rows[i].At.After(rows[j].At) })
|
sort.Slice(rows, func(i, j int) bool { return rows[i].At.After(rows[j].At) })
|
||||||
@@ -50,7 +74,7 @@ func performedRows(s stdio.KeptState) ([]OwedRow, error) {
|
|||||||
// OwedTools are bridge_owed, bridge_owed_again and bridge_owed_drop: ask writes an owed-asks value and waits for the
|
// OwedTools are bridge_owed, bridge_owed_again and bridge_owed_drop: ask writes an owed-asks value and waits for the
|
||||||
// bridge's answer; performed reads the state performed; record writes a hand act in the seat mesh-hand-acts' log before
|
// bridge's answer; performed reads the state performed; record writes a hand act in the seat mesh-hand-acts' log before
|
||||||
// a drop is asked, so a drop is never made without its record.
|
// a drop is asked, so a drop is never made without its record.
|
||||||
func OwedTools(ask func(key string, value []byte) ([]byte, error), performed func() ([]OwedRow, error), record func(what, why string) error, now func() time.Time) []stdio.Tool {
|
func OwedTools(ask func(key string, value []byte) ([]byte, error), performed func(OwedFilter) ([]OwedRow, error), record func(what, why string) error, now func() time.Time) []stdio.Tool {
|
||||||
str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
|
str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
|
||||||
act := map[string]any{
|
act := map[string]any{
|
||||||
"of": map[string]any{"type": "string", "description": "the seat that said the act: mesh-delivery (the gate)"},
|
"of": map[string]any{"type": "string", "description": "the seat that said the act: mesh-delivery (the gate)"},
|
||||||
@@ -66,6 +90,12 @@ func OwedTools(ask func(key string, value []byte) ([]byte, error), performed fun
|
|||||||
}
|
}
|
||||||
return groups.DecodeOwedAsk(v)
|
return groups.DecodeOwedAsk(v)
|
||||||
}
|
}
|
||||||
|
read := map[string]any{
|
||||||
|
"repository": map[string]any{"type": "string", "description": "the pull request's repository, as owner/name; with number"},
|
||||||
|
"number": map[string]any{"type": "integer", "description": "the pull request's number; with repository"}}
|
||||||
|
for k, v := range act {
|
||||||
|
read[k] = v
|
||||||
|
}
|
||||||
withWhy := map[string]any{"why": map[string]any{"type": "string", "description": "why it is dropped, in plain words"}}
|
withWhy := map[string]any{"why": map[string]any{"type": "string", "description": "why it is dropped, in plain words"}}
|
||||||
for k, v := range act {
|
for k, v := range act {
|
||||||
withWhy[k] = v
|
withWhy[k] = v
|
||||||
@@ -74,22 +104,24 @@ func OwedTools(ask func(key string, value []byte) ([]byte, error), performed fun
|
|||||||
{Name: OwedTool,
|
{Name: OwedTool,
|
||||||
Description: "What the forge bridge performed of what the gate and the checker owe the forge (design 47): each act it said as " +
|
Description: "What the forge bridge performed of what the gate and the checker owe the forge (design 47): each act it said as " +
|
||||||
"owed.ended, newest first, with its seat, id, pull request, outcome (performed, refused, could not be performed, " +
|
"owed.ended, newest first, with its seat, id, pull request, outcome (performed, refused, could not be performed, " +
|
||||||
"undecodable, dropped), the forge's words and when, from its state performed (kept 7 days); and the backlog of its " +
|
"undecodable, dropped), the forge's words and when, from its state performed (kept 7 days), of one act (id, of " +
|
||||||
|
"narrowing it) or of one pull request (repository and number), never the whole state; and the backlog of its " +
|
||||||
"durable on each seat's stream owed: the last event applied, the events waiting and held, the age of the oldest. " +
|
"durable on each seat's stream owed: the last event applied, the events waiting and held, the age of the oldest. " +
|
||||||
"of or id narrow it. Read-only.",
|
"Read-only.",
|
||||||
Input: map[string]any{"type": "object", "properties": act},
|
Input: map[string]any{"type": "object", "properties": read},
|
||||||
Run: func(args map[string]any) (any, error) {
|
Run: func(args map[string]any) (any, error) {
|
||||||
rows, err := performed()
|
f := OwedFilter{Of: str(args, "of"), ID: str(args, "id"), Repository: str(args, "repository")}
|
||||||
|
if n, ok := args["number"].(float64); ok {
|
||||||
|
f.Number = int(n)
|
||||||
|
}
|
||||||
|
if f.ID == "" && (f.Repository == "" || f.Number <= 0) {
|
||||||
|
return nil, fmt.Errorf("bridge_owed reads one act (id) or one pull request's acts (repository and number), never the whole state")
|
||||||
|
}
|
||||||
|
rows, err := performed(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
kept := []OwedRow{}
|
out := map[string]any{"performed": rows}
|
||||||
for _, r := range rows {
|
|
||||||
if (str(args, "of") == "" || r.Of == str(args, "of")) && (str(args, "id") == "" || r.ID == str(args, "id")) {
|
|
||||||
kept = append(kept, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out := map[string]any{"performed": kept}
|
|
||||||
if a, err := asked(groups.OwedBacklog, args); err != nil {
|
if a, err := asked(groups.OwedBacklog, args); err != nil {
|
||||||
out["backlog-error"] = err.Error()
|
out["backlog-error"] = err.Error()
|
||||||
} else {
|
} else {
|
||||||
@@ -100,8 +132,8 @@ func OwedTools(ask func(key string, value []byte) ([]byte, error), performed fun
|
|||||||
{Name: OwedAgainTool,
|
{Name: OwedAgainTool,
|
||||||
Description: "Perform one act owed to the forge again now, by hand, whatever its last outcome (a refusal the forge no longer " +
|
Description: "Perform one act owed to the forge again now, by hand, whatever its last outcome (a refusal the forge no longer " +
|
||||||
"makes, an act that could not be performed): the bridge calls the seat git's verb of the act under its id (safe to " +
|
"makes, an act that could not be performed): the bridge calls the seat git's verb of the act under its id (safe to " +
|
||||||
"repeat) and says its outcome as owed.ended. The act is the one the bridge kept, or, for the gate's, the seat " +
|
"repeat) and says its outcome as owed.ended. The act is read from the stream owed of the seat that said it, on the " +
|
||||||
"mesh-delivery's groups. Answers the outcome.",
|
"subject of the pull request the bridge's state performed names (kept 7 days there). Answers the outcome.",
|
||||||
Input: map[string]any{"type": "object", "required": []string{"of", "id"}, "properties": act},
|
Input: map[string]any{"type": "object", "required": []string{"of", "id"}, "properties": act},
|
||||||
Run: func(args map[string]any) (any, error) {
|
Run: func(args map[string]any) (any, error) {
|
||||||
a, err := asked(groups.OwedAgain, args)
|
a, err := asked(groups.OwedAgain, args)
|
||||||
|
|||||||
@@ -0,0 +1,84 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/nats-io/nats.go/jetstream"
|
||||||
|
|
||||||
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
||||||
|
|
||||||
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The act bridge_owed_again performs again is read from the stream owed of the seat that said it, on the subject of the
|
||||||
|
// pull request performed names, the stream being the act's one copy (novox/hq design 47 G §162, §163, H-A-181).
|
||||||
|
|
||||||
|
// actReader reads the act the seat of said under id from its stream owed, on the subject of the pull request
|
||||||
|
// (repository and number) the act names, for decode to say each message's id; found false when the stream no longer
|
||||||
|
// holds it.
|
||||||
|
type actReader interface {
|
||||||
|
act(ctx context.Context, of, repository string, number int, id string, idOf func([]byte) string) (data []byte, found bool, err error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// actOf is the act of to perform again, read from its seat's stream owed on the pull request p names.
|
||||||
|
func (o *owing) actOf(ctx context.Context, of string, p Performed, id string) ([]byte, error) {
|
||||||
|
read, err := readerOf(of)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if o.acts == nil {
|
||||||
|
return nil, fmt.Errorf("the bridge reads no stream owed, so the act %s of %s cannot be performed again", id, of)
|
||||||
|
}
|
||||||
|
data, found, err := o.acts.act(ctx, of, p.Repository, p.Number, id, func(m []byte) string { c, _ := read(m); return c.ID })
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("the act %s of %s cannot be read from %s's stream owed: %w", id, of, of, err)
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
return nil, fmt.Errorf("%s's stream owed no longer holds the act %s of %s#%d (kept 7 days)", of, id, p.Repository, p.Number)
|
||||||
|
}
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// streamActs reads an act from the gate's stream owed (seat mesh-delivery's StreamOwed) by an ordered consumer filtered
|
||||||
|
// on the pull request's subject, as the gate says it (SubjectOwed with the repository escaped): every message the
|
||||||
|
// stream keeps there, the newest of the id kept.
|
||||||
|
type streamActs struct{ js jetstream.JetStream }
|
||||||
|
|
||||||
|
func (s streamActs) act(ctx context.Context, of, repository string, number int, id string, idOf func([]byte) string) ([]byte, bool, error) {
|
||||||
|
if of != seat.Seat {
|
||||||
|
return nil, false, fmt.Errorf("the seat %q keeps no stream owed the bridge reads", of)
|
||||||
|
}
|
||||||
|
subject := seat.SubjectOwed(keys.EscapeName(repository), strconv.Itoa(number))
|
||||||
|
st, err := s.js.Stream(ctx, seat.StreamOwed)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
info, err := st.Info(ctx, jetstream.WithSubjectFilter(subject))
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
held := info.State.Subjects[subject]
|
||||||
|
if held == 0 {
|
||||||
|
return nil, false, nil
|
||||||
|
}
|
||||||
|
c, err := st.OrderedConsumer(ctx, jetstream.OrderedConsumerConfig{FilterSubjects: []string{subject}})
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
var found []byte
|
||||||
|
for range held {
|
||||||
|
m, err := c.Next(jetstream.FetchContext(ctx))
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, err
|
||||||
|
}
|
||||||
|
if idOf(m.Data()) == id {
|
||||||
|
found = m.Data()
|
||||||
|
}
|
||||||
|
if md, err := m.Metadata(); err == nil && md.NumPending == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found, found != nil, nil
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -38,6 +39,24 @@ func (m memPerformed) Get(_ context.Context, key string) ([]byte, bool, error) {
|
|||||||
}
|
}
|
||||||
func (m memPerformed) Put(_ context.Context, key string, v []byte) error { m[key] = v; return nil }
|
func (m memPerformed) Put(_ context.Context, key string, v []byte) error { m[key] = v; return nil }
|
||||||
|
|
||||||
|
// memActs is a seat's stream owed by pull request subject (`<repository>#<number>`); asked records what was read.
|
||||||
|
type memActs struct {
|
||||||
|
held map[string][][]byte
|
||||||
|
asked []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *memActs) act(_ context.Context, of, repository string, number int, id string, idOf func([]byte) string) ([]byte, bool, error) {
|
||||||
|
pr := fmt.Sprintf("%s %s#%d", of, repository, number)
|
||||||
|
m.asked = append(m.asked, pr)
|
||||||
|
var found []byte
|
||||||
|
for _, d := range m.held[pr] {
|
||||||
|
if idOf(d) == id {
|
||||||
|
found = d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found, found != nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
// seatFake answers each seat verb from answers, by its key; one absent is not answered (an error, as a bound passed).
|
// seatFake answers each seat verb from answers, by its key; one absent is not answered (an error, as a bound passed).
|
||||||
type seatFake struct {
|
type seatFake struct {
|
||||||
calls []string
|
calls []string
|
||||||
@@ -109,9 +128,12 @@ func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) {
|
|||||||
if v, ok := kept[performedKey(c.of, e.ID)]; ok != c.kept {
|
if v, ok := kept[performedKey(c.of, e.ID)]; ok != c.kept {
|
||||||
t.Errorf("%s: kept %v (%s), want %v", c.name, ok, v, c.kept)
|
t.Errorf("%s: kept %v (%s), want %v", c.name, ok, v, c.kept)
|
||||||
} else if ok {
|
} else if ok {
|
||||||
|
// The outcome and when, and the pull request the act names, nothing more (§163, H-A-181, H-A-182).
|
||||||
var p Performed
|
var p Performed
|
||||||
if err := json.Unmarshal(v, &p); err != nil || p.Ended.Outcome != c.outcome || !json.Valid(p.Owed) {
|
var fields map[string]json.RawMessage
|
||||||
t.Errorf("%s: kept %s, want the outcome and the owed event", c.name, v)
|
if err := json.Unmarshal(v, &p); err != nil || p.Ended.Outcome != c.outcome || p.Ended.At.IsZero() ||
|
||||||
|
p.Repository != "novox/mesh-sdk" || p.Number != 178 || json.Unmarshal(v, &fields) != nil || len(fields) != 3 {
|
||||||
|
t.Errorf("%s: kept %s, want the outcome, when, and the pull request novox/mesh-sdk#178, nothing more", c.name, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,6 +164,9 @@ func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) {
|
|||||||
t.Run("asked again, dropped, and its backlog read by hand", func(t *testing.T) {
|
t.Run("asked again, dropped, and its backlog read by hand", func(t *testing.T) {
|
||||||
f := &seatFake{answers: statusOK}
|
f := &seatFake{answers: statusOK}
|
||||||
o, pub, kept := newOwing(f)
|
o, pub, kept := newOwing(f)
|
||||||
|
// The act is read again from the gate's stream owed on its pull request's subject, never from performed (H-A-181).
|
||||||
|
acts := &memActs{held: map[string][][]byte{"mesh-delivery novox/mesh-sdk#178": {[]byte(`{`), gate("owed is said, a status")}}}
|
||||||
|
o.acts = acts
|
||||||
status := "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/merge-gate"
|
status := "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/merge-gate"
|
||||||
if _, err := o.heard(context.Background(), seat.Seat, gate("owed is said, a status")); err != nil {
|
if _, err := o.heard(context.Background(), seat.Seat, gate("owed is said, a status")); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -153,11 +178,15 @@ func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) {
|
|||||||
calls, said int
|
calls, said int
|
||||||
outcome, answers string
|
outcome, answers string
|
||||||
}{
|
}{
|
||||||
{"again, from performed", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: status}, 2, 2, forgemsg.OwedPerformed, "performed"},
|
{"again, from the gate's stream owed", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: status}, 2, 2, forgemsg.OwedPerformed, "performed"},
|
||||||
{"dropped", groups.OwedAsk{Kind: groups.OwedDrop, Of: seat.Seat, ID: status, Why: "set by hand", By: "lead@shanks"}, 2, 3, forgemsg.OwedDropped, "set by hand"},
|
{"dropped", groups.OwedAsk{Kind: groups.OwedDrop, Of: seat.Seat, ID: status, Why: "set by hand", By: "lead@shanks"}, 2, 3, forgemsg.OwedDropped, "set by hand"},
|
||||||
{"again, an act nobody holds", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: "x"}, 2, 3, "", "holds no owed act"},
|
{"again, an act nobody holds", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: "x"}, 2, 3, "", "holds no owed act"},
|
||||||
{"the backlog", groups.OwedAsk{Kind: groups.OwedBacklog}, 2, 3, "", "backlog"},
|
{"the backlog", groups.OwedAsk{Kind: groups.OwedBacklog}, 2, 3, "", "backlog"},
|
||||||
|
{"again, the stream no longer holding it", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: status}, 2, 3, "", "no longer holds"},
|
||||||
} {
|
} {
|
||||||
|
if c.name == "again, the stream no longer holding it" {
|
||||||
|
acts.held = nil
|
||||||
|
}
|
||||||
a := o.answer(context.Background(), c.ask, backlog)
|
a := o.answer(context.Background(), c.ask, backlog)
|
||||||
if !a.Done() || a.Outcome != c.outcome || !strings.Contains(a.Answer, c.answers) || len(f.calls) != c.calls || len(pub.data) != c.said {
|
if !a.Done() || a.Outcome != c.outcome || !strings.Contains(a.Answer, c.answers) || len(f.calls) != c.calls || len(pub.data) != c.said {
|
||||||
t.Errorf("%s: %+v, called %v, said %d; want %q naming %q, %d calls, %d said", c.name, a, f.calls, len(pub.data), c.outcome, c.answers, c.calls, c.said)
|
t.Errorf("%s: %+v, called %v, said %d; want %q naming %q, %d calls, %d said", c.name, a, f.calls, len(pub.data), c.outcome, c.answers, c.calls, c.said)
|
||||||
@@ -167,8 +196,12 @@ func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
var p Performed
|
var p Performed
|
||||||
if err := json.Unmarshal(kept[performedKey(seat.Seat, status)], &p); err != nil || p.Ended.Outcome != forgemsg.OwedDropped {
|
if err := json.Unmarshal(kept[performedKey(seat.Seat, status)], &p); err != nil || p.Ended.Outcome != forgemsg.OwedDropped ||
|
||||||
t.Errorf("performed after the drop: %+v %v", p, err)
|
p.Repository != "novox/mesh-sdk" || p.Number != 178 {
|
||||||
|
t.Errorf("performed after the drop: %+v %v; want dropped, its pull request kept", p, err)
|
||||||
|
}
|
||||||
|
if len(acts.asked) != 2 || acts.asked[0] != "mesh-delivery novox/mesh-sdk#178" {
|
||||||
|
t.Errorf("the stream owed was read on %v, want twice on the pull request performed names", acts.asked)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-3
@@ -22,6 +22,7 @@ import (
|
|||||||
git "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git"
|
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"
|
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"
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||||
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||||
"git.novox.be/novox/mesh-sdk/go/process"
|
"git.novox.be/novox/mesh-sdk/go/process"
|
||||||
|
|
||||||
"mesh-delivery-forge-bridge/internal/gitfixture"
|
"mesh-delivery-forge-bridge/internal/gitfixture"
|
||||||
@@ -33,7 +34,8 @@ import (
|
|||||||
// no others, so a permission the bridge lacks fails a test here and not on the mesh (issue 1279).
|
// no others, so a permission the bridge lacks fails a test here and not on the mesh (issue 1279).
|
||||||
//
|
//
|
||||||
// From module.json: the registrations and the gate's source-moved heard through its durable on EVENTS; branch-moved and
|
// From module.json: the registrations and the gate's source-moved heard through its durable on EVENTS; branch-moved and
|
||||||
// what the gate owes heard through its durable on each seat's own stream (SEAT_<seat>_<event>); the seat git's verbs, the
|
// what the gate owes heard through its durable on each seat's own stream (SEAT_<seat>_<event>), and each seat's kept
|
||||||
|
// event also read through an ordered consumer of its own (mesh-grants heardGrants, A-233; bridge_owed_again, H-A-181); the seat git's verbs, the
|
||||||
// seat mesh-catalog's registrations, the seat mesh-delivery's source, the seat mesh-broker's show and the seat
|
// seat mesh-catalog's registrations, the seat mesh-delivery's source, the seat mesh-broker's show and the seat
|
||||||
// mesh-hand-acts' hand-act called on mesh.seat.<seat>.call.<verb>.<token> (or its plain fallback); its own states heads
|
// mesh-hand-acts' hand-act called on mesh.seat.<seat>.call.<verb>.<token> (or its plain fallback); its own states heads
|
||||||
// and owed-asks, and performed under the name its binding gives; its stream forge. `**` is written `>` on the wire. The
|
// and owed-asks, and performed under the name its binding gives; its stream forge. `**` is written `>` on the wire. The
|
||||||
@@ -82,6 +84,11 @@ accounts {
|
|||||||
"$JS.ACK.SEAT_mesh-delivery_owed.n1_mesh-delivery-forge-bridge.>"
|
"$JS.ACK.SEAT_mesh-delivery_owed.n1_mesh-delivery-forge-bridge.>"
|
||||||
"$JS.API.CONSUMER.INFO.SEAT_mesh-delivery_owed.n1_mesh-delivery-forge-bridge"
|
"$JS.API.CONSUMER.INFO.SEAT_mesh-delivery_owed.n1_mesh-delivery-forge-bridge"
|
||||||
"$JS.API.CONSUMER.MSG.NEXT.SEAT_mesh-delivery_owed.n1_mesh-delivery-forge-bridge"
|
"$JS.API.CONSUMER.MSG.NEXT.SEAT_mesh-delivery_owed.n1_mesh-delivery-forge-bridge"
|
||||||
|
"$JS.API.CONSUMER.CREATE.SEAT_mesh-delivery_owed"
|
||||||
|
"$JS.API.CONSUMER.CREATE.SEAT_mesh-delivery_owed.>"
|
||||||
|
"$JS.API.CONSUMER.MSG.NEXT.SEAT_mesh-delivery_owed.*"
|
||||||
|
"$JS.API.CONSUMER.INFO.SEAT_mesh-delivery_owed.*"
|
||||||
|
"$JS.API.CONSUMER.DELETE.SEAT_mesh-delivery_owed.*"
|
||||||
"mesh.seat.mesh-broker.call.show.n1~mesh-delivery-forge-bridge"
|
"mesh.seat.mesh-broker.call.show.n1~mesh-delivery-forge-bridge"
|
||||||
"mesh.seat.mesh-broker.tool.show"
|
"mesh.seat.mesh-broker.tool.show"
|
||||||
"mesh.seat.mesh-catalog.call.registrations.n1~mesh-delivery-forge-bridge"
|
"mesh.seat.mesh-catalog.call.registrations.n1~mesh-delivery-forge-bridge"
|
||||||
@@ -518,7 +525,9 @@ func TestBridgePerformsWhatIsOwedOnce(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
gateOwed := gitfixture.Seat(t, seat.Seat, "events", "owed is said, a status", "event:owed")[0]
|
gateOwed := gitfixture.Seat(t, seat.Seat, "events", "owed is said, a status", "event:owed")[0]
|
||||||
subject := seat.SubjectOwed("novox~mesh-sdk", "178")
|
// On the pull request's subject as the gate says it (its repository escaped as a key part), where bridge_owed_again
|
||||||
|
// reads the act again (H-A-181).
|
||||||
|
subject := seat.SubjectOwed(keys.EscapeName("novox/mesh-sdk"), "178")
|
||||||
for range 2 {
|
for range 2 {
|
||||||
if _, err := b.js.Publish(b.ctx, subject, gateOwed); err != nil {
|
if _, err := b.js.Publish(b.ctx, subject, gateOwed); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -597,12 +606,22 @@ func TestBridgePerformsWhatIsOwedOnce(t *testing.T) {
|
|||||||
if a, err := groupkeys.DecodeOwedAsk(e.Value()); err == nil && a.Done() {
|
if a, err := groupkeys.DecodeOwedAsk(e.Value()); err == nil && a.Done() {
|
||||||
answered = true
|
answered = true
|
||||||
if a.Outcome != forgemsg.OwedPerformed || count("git.status") != 3 {
|
if a.Outcome != forgemsg.OwedPerformed || count("git.status") != 3 {
|
||||||
t.Errorf("bridge_owed_again: %+v, status asked %d times; want performed again", a, count("git.status"))
|
t.Errorf("bridge_owed_again: %+v, status asked %d times; want performed again, read from the gate's stream owed", a, count("git.status"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case <-bound:
|
case <-bound:
|
||||||
t.Fatal("bridge_owed_again was not answered within 20 s")
|
t.Fatal("bridge_owed_again was not answered within 20 s")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// performed keeps the outcome, when and the pull request, never the act (§163, H-A-181, H-A-182).
|
||||||
|
kept, err := b.js.KeyValue(b.ctx, boundPerformed)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if e, err := kept.Get(b.ctx, performedKey(seat.Seat, g.ID)); err != nil {
|
||||||
|
t.Errorf("performed of %s: %v", g.ID, err)
|
||||||
|
} else if fields := (map[string]json.RawMessage{}); json.Unmarshal(e.Value(), &fields) != nil || len(fields) != 3 || fields["owed"] != nil {
|
||||||
|
t.Errorf("performed of %s keeps %s, want the outcome, when and the pull request alone", g.ID, e.Value())
|
||||||
|
}
|
||||||
refusedRead(t, b, "mesh-delivery-gate_owed")
|
refusedRead(t, b, "mesh-delivery-gate_owed")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
|
|||||||
}
|
}
|
||||||
performedKV := &boundBucket{binding: binding, local: performedLocal, js: js}
|
performedKV := &boundBucket{binding: binding, local: performedLocal, js: js}
|
||||||
// What the gate owes the forge, performed through the seat git (design 47 *What the gate owes the forge*).
|
// What the gate owes the forge, performed through the seat git (design 47 *What the gate owes the forge*).
|
||||||
owe := &owing{call: call, pub: js, performed: kvPerformed{performedKV}, waits: binding.Waiting, now: time.Now, logger: logger}
|
owe := &owing{call: call, pub: js, performed: kvPerformed{performedKV}, acts: streamActs{js: js}, waits: binding.Waiting, now: time.Now, logger: logger}
|
||||||
keptCall := keptCallOf(ctx, call)
|
keptCall := keptCallOf(ctx, call)
|
||||||
reader := &views.ProcessReader{Module: forgeBridgeModule(), Call: keptCall, Logf: logger.Printf, Hub: hub}
|
reader := &views.ProcessReader{Module: forgeBridgeModule(), Call: keptCall, Logf: logger.Printf, Hub: hub}
|
||||||
src := sourcebranches.New(sourcebranches.Call(keptCall))
|
src := sourcebranches.New(sourcebranches.Call(keptCall))
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -22,22 +21,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// The state performed (manifest `requires`, the template state, opened by its binding: bound.go) keeps one key per owed act it
|
// The state performed (manifest `requires`, the template state, opened by its binding: bound.go) keeps one key per owed act it
|
||||||
// performed, the act's outcome and when, with the owed event as heard (novox/hq design 47 *What the gate owes the
|
// performed, in the form groups.Performed says (novox/hq design 47 G §163, H-A-181, H-A-182). It is a saving and a read,
|
||||||
// forge*). It is a saving and a read, never what keeps an act from happening twice: every act is safe to repeat at the
|
// never what keeps an act from happening twice: every act is safe to repeat at the forge.
|
||||||
// forge.
|
|
||||||
|
|
||||||
// performedKey is the key of one act in performed: the seat that said it and its id, the id in unpadded base64url, since
|
// performedKey is the key of one act in performed (groups.PerformedKey).
|
||||||
// an id may hold what a key may not (`#`, `/`).
|
func performedKey(of, id string) string { return groups.PerformedKey(of, id) }
|
||||||
func performedKey(of, id string) string {
|
|
||||||
return of + "." + base64.RawURLEncoding.EncodeToString([]byte(id))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Performed is one value of the state performed: the outcome said as owed.ended, and the owed event it answers, so
|
// Performed is one value of the state performed.
|
||||||
// bridge_owed_again performs it again from here.
|
type Performed = groups.Performed
|
||||||
type Performed struct {
|
|
||||||
Ended forgemsg.OwedEnded `json:"ended"`
|
|
||||||
Owed json.RawMessage `json:"owed,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// performedStore is the state performed as the owing reads and writes it.
|
// performedStore is the state performed as the owing reads and writes it.
|
||||||
type performedStore interface {
|
type performedStore interface {
|
||||||
@@ -70,6 +61,8 @@ type owing struct {
|
|||||||
call caller
|
call caller
|
||||||
pub publisher
|
pub publisher
|
||||||
performed performedStore
|
performed performedStore
|
||||||
|
// acts reads an act again from the stream owed of the seat that said it (H-A-181), for bridge_owed_again.
|
||||||
|
acts actReader
|
||||||
// waits, when set, says why the state performed cannot be used yet (its binding, design 47 G §577); empty when it can.
|
// waits, when set, says why the state performed cannot be used yet (its binding, design 47 G §577); empty when it can.
|
||||||
waits func() string
|
waits func() string
|
||||||
now func() time.Time
|
now func() time.Time
|
||||||
@@ -90,12 +83,12 @@ func readerOf(of string) (func([]byte) (bridge.OwedCall, error), error) {
|
|||||||
func (o *owing) heard(ctx context.Context, of string, data []byte) (forgemsg.OwedEnded, error) {
|
func (o *owing) heard(ctx context.Context, of string, data []byte) (forgemsg.OwedEnded, error) {
|
||||||
read, err := readerOf(of)
|
read, err := readerOf(of)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return o.end(ctx, forgemsg.OwedEnded{Of: of, Outcome: forgemsg.OwedUndecodable, Words: err.Error()}, data)
|
return o.end(ctx, forgemsg.OwedEnded{Of: of, Outcome: forgemsg.OwedUndecodable, Words: err.Error()}, "", 0)
|
||||||
}
|
}
|
||||||
c, err := read(data)
|
c, err := read(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return o.end(ctx, forgemsg.OwedEnded{Of: of, ID: c.ID,
|
return o.end(ctx, forgemsg.OwedEnded{Of: of, ID: c.ID,
|
||||||
Outcome: forgemsg.OwedUndecodable, Words: err.Error()}, data)
|
Outcome: forgemsg.OwedUndecodable, Words: err.Error()}, c.Repository, c.Number)
|
||||||
}
|
}
|
||||||
if w := o.waiting(); w != "" {
|
if w := o.waiting(); w != "" {
|
||||||
return forgemsg.OwedEnded{Of: c.Of, ID: c.ID}, fmt.Errorf("the bridge %s", w)
|
return forgemsg.OwedEnded{Of: c.Of, ID: c.ID}, fmt.Errorf("the bridge %s", w)
|
||||||
@@ -104,7 +97,7 @@ func (o *owing) heard(ctx context.Context, of string, data []byte) (forgemsg.Owe
|
|||||||
// said twice, or said again while its outcome was on its way: performed once, the outcome said once
|
// said twice, or said again while its outcome was on its way: performed once, the outcome said once
|
||||||
return forgemsg.OwedEnded{Of: c.Of, ID: c.ID}, nil
|
return forgemsg.OwedEnded{Of: c.Of, ID: c.ID}, nil
|
||||||
}
|
}
|
||||||
return o.perform(ctx, c, data)
|
return o.perform(ctx, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// hear is one message of a seat's stream owed: performed and acknowledged, or, when its outcome could not be said,
|
// hear is one message of a seat's stream owed: performed and acknowledged, or, when its outcome could not be said,
|
||||||
@@ -127,19 +120,20 @@ func (o *owing) waiting() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// perform calls the seat git's verb of one act and says what it came to.
|
// perform calls the seat git's verb of one act and says what it came to.
|
||||||
func (o *owing) perform(ctx context.Context, c bridge.OwedCall, data []byte) (forgemsg.OwedEnded, error) {
|
func (o *owing) perform(ctx context.Context, c bridge.OwedCall) (forgemsg.OwedEnded, error) {
|
||||||
answer, callErr := o.call(ctx, "seat:"+git.Seat+"."+c.Verb, c.Input)
|
answer, callErr := o.call(ctx, "seat:"+git.Seat+"."+c.Verb, c.Input)
|
||||||
outcome, words := bridge.Outcome(answer, callErr)
|
outcome, words := bridge.Outcome(answer, callErr)
|
||||||
if outcome != forgemsg.OwedPerformed {
|
if outcome != forgemsg.OwedPerformed {
|
||||||
o.logger.Printf("the owed %s %s of %s#%d (said by %s) is %s: %s", c.Act, c.ID, c.Repository, c.Number, c.Of, outcome, words)
|
o.logger.Printf("the owed %s %s of %s#%d (said by %s) is %s: %s", c.Act, c.ID, c.Repository, c.Number, c.Of, outcome, words)
|
||||||
}
|
}
|
||||||
return o.end(ctx, forgemsg.OwedEnded{Of: c.Of, ID: c.ID,
|
return o.end(ctx, forgemsg.OwedEnded{Of: c.Of, ID: c.ID,
|
||||||
Outcome: outcome, Words: words}, data)
|
Outcome: outcome, Words: words}, c.Repository, c.Number)
|
||||||
}
|
}
|
||||||
|
|
||||||
// end says an act's outcome as owed.ended on the stream forge, then keeps it in performed. An outcome not kept is
|
// end says an act's outcome as owed.ended on the stream forge, then keeps it in performed with the pull request the act
|
||||||
// logged: the act may be performed again, which changes nothing at the forge.
|
// names (repository and number). An outcome not kept is logged: the act may be performed again, which changes nothing at
|
||||||
func (o *owing) end(ctx context.Context, e forgemsg.OwedEnded, owed []byte) (forgemsg.OwedEnded, error) {
|
// the forge.
|
||||||
|
func (o *owing) end(ctx context.Context, e forgemsg.OwedEnded, repository string, number int) (forgemsg.OwedEnded, error) {
|
||||||
e.At = o.now().UTC()
|
e.At = o.now().UTC()
|
||||||
data, err := forgemsg.EncodeOwedEnded(e)
|
data, err := forgemsg.EncodeOwedEnded(e)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -151,10 +145,7 @@ func (o *owing) end(ctx context.Context, e forgemsg.OwedEnded, owed []byte) (for
|
|||||||
if e.ID == "" {
|
if e.ID == "" {
|
||||||
return e, nil // an undecodable act with no id is said, and has no key to keep
|
return e, nil // an undecodable act with no id is said, and has no key to keep
|
||||||
}
|
}
|
||||||
if !json.Valid(owed) {
|
v, err := json.Marshal(Performed{Ended: e, Repository: repository, Number: number})
|
||||||
owed = nil
|
|
||||||
}
|
|
||||||
v, err := json.Marshal(Performed{Ended: e, Owed: owed})
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = o.performed.Put(ctx, performedKey(e.Of, e.ID), v)
|
err = o.performed.Put(ctx, performedKey(e.Of, e.ID), v)
|
||||||
}
|
}
|
||||||
@@ -175,19 +166,17 @@ func (o *owing) held(ctx context.Context, of, id string) bool {
|
|||||||
return found
|
return found
|
||||||
}
|
}
|
||||||
|
|
||||||
// owedOf is the owed event of an act to perform again, as performed keeps it.
|
// kept is what performed holds of an act: its outcome and its pull request.
|
||||||
func (o *owing) owedOf(ctx context.Context, of, id string) ([]byte, error) {
|
func (o *owing) kept(ctx context.Context, of, id string) (Performed, error) {
|
||||||
v, found, err := o.performed.Get(ctx, performedKey(of, id))
|
v, found, err := o.performed.Get(ctx, performedKey(of, id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("performed cannot be read: %w", err)
|
return Performed{}, fmt.Errorf("performed cannot be read: %w", err)
|
||||||
}
|
}
|
||||||
if found {
|
var p Performed
|
||||||
var p Performed
|
if !found || json.Unmarshal(v, &p) != nil {
|
||||||
if err := json.Unmarshal(v, &p); err == nil && len(p.Owed) > 0 {
|
return Performed{}, fmt.Errorf("the bridge holds no owed act %s of %s in its state performed", id, of)
|
||||||
return p.Owed, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("the bridge holds no owed act %s of %s in its state performed", id, of)
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// answer answers one owed ask: an act performed again now, whatever performed holds of it; an act dropped, said as
|
// answer answers one owed ask: an act performed again now, whatever performed holds of it; an act dropped, said as
|
||||||
@@ -199,20 +188,22 @@ func (o *owing) answer(ctx context.Context, a groups.OwedAsk, backlog func(conte
|
|||||||
a.Backlog, a.Answer = backlog(ctx), "the backlog of each seat's stream owed"
|
a.Backlog, a.Answer = backlog(ctx), "the backlog of each seat's stream owed"
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
data, err := o.owedOf(ctx, a.Of, a.ID)
|
p, err := o.kept(ctx, a.Of, a.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.Answer = err.Error()
|
a.Answer = err.Error()
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
var e forgemsg.OwedEnded
|
var e forgemsg.OwedEnded
|
||||||
if a.Kind == groups.OwedDrop {
|
if a.Kind == groups.OwedDrop {
|
||||||
e, err = o.end(ctx, forgemsg.OwedEnded{Of: a.Of, ID: a.ID, Outcome: forgemsg.OwedDropped, Words: a.Why + " (dropped by " + a.By + ")"}, data)
|
e, err = o.end(ctx, forgemsg.OwedEnded{Of: a.Of, ID: a.ID, Outcome: forgemsg.OwedDropped, Words: a.Why + " (dropped by " + a.By + ")"}, p.Repository, p.Number)
|
||||||
} else if read, rerr := readerOf(a.Of); rerr != nil {
|
} else if read, rerr := readerOf(a.Of); rerr != nil {
|
||||||
err = rerr
|
err = rerr
|
||||||
|
} else if data, rerr := o.actOf(ctx, a.Of, p, a.ID); rerr != nil {
|
||||||
|
err = rerr
|
||||||
} else if c, rerr := read(data); rerr != nil {
|
} else if c, rerr := read(data); rerr != nil {
|
||||||
e, err = o.end(ctx, forgemsg.OwedEnded{Of: a.Of, ID: a.ID, Outcome: forgemsg.OwedUndecodable, Words: rerr.Error()}, data)
|
e, err = o.end(ctx, forgemsg.OwedEnded{Of: a.Of, ID: a.ID, Outcome: forgemsg.OwedUndecodable, Words: rerr.Error()}, p.Repository, p.Number)
|
||||||
} else {
|
} else {
|
||||||
e, err = o.perform(ctx, c, data)
|
e, err = o.perform(ctx, c)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.Answer = err.Error()
|
a.Answer = err.Error()
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package groups
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
|
||||||
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The state performed keeps one key per owed act the bridge performed, its outcome and when, and the pull request the
|
||||||
|
// act names, nothing more (novox/hq design 47 G §163, H-A-181, H-A-182): the act itself stays in the stream owed of the
|
||||||
|
// seat that said it, its one copy, where bridge_owed_again reads it again. The bridge writes it; its tools read it.
|
||||||
|
|
||||||
|
// PerformedKey is the key of one act in performed: the seat that said it and its id, the id in unpadded base64url, since
|
||||||
|
// an id may hold what a key may not (`#`, `/`).
|
||||||
|
func PerformedKey(of, id string) string {
|
||||||
|
return of + "." + base64.RawURLEncoding.EncodeToString([]byte(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Performed is one value of the state performed (§163, H-A-182): the outcome said as owed.ended, with when, and the
|
||||||
|
// repository and number of the pull request the act names, so bridge_owed reads one pull request's acts and
|
||||||
|
// bridge_owed_again finds the act on its pull request's subject of the stream owed (H-A-181).
|
||||||
|
type Performed struct {
|
||||||
|
Ended forgemsg.OwedEnded `json:"ended"`
|
||||||
|
Repository string `json:"repository,omitempty"`
|
||||||
|
Number int `json:"number,omitempty"`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user