Builds: design 47 G §73, §74, §332 (every subject built and parsed back; group-ended under branch and group with no module part). The rule Passed of the dev branch is not built: no line of the contracts part names it. Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
103 lines
4.2 KiB
Go
103 lines
4.2 KiB
Go
// Package meshpipelinerunnerclient is the logic the callers of the seat mesh-pipeline-runner share (ADR 0634 §1): the
|
|
// subjects of the runner's events, built and parsed, and their bodies decoded (design 47 G §73, §332). It names no
|
|
// module, repository or branch: every name is the caller's.
|
|
package meshpipelinerunnerclient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshpipelinerunner"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
)
|
|
|
|
// eventPrefix starts every event subject of the seat.
|
|
const eventPrefix = "mesh.seat." + seat.Seat + ".event."
|
|
|
|
// Subject is where the runner says event of a pipeline: mesh.seat.mesh-pipeline-runner.event.<event>.<module>.<branch>.
|
|
// <commit or group>, each name escaped as a key's name is (design 47 *Health and going back*), so a branch or a group
|
|
// holding a dot is one token.
|
|
func Subject(event, module, branch, commitOrGroup string) string {
|
|
return eventPrefix + event + "." + keys.EscapeName(module) + "." + keys.EscapeName(branch) + "." + keys.EscapeName(commitOrGroup)
|
|
}
|
|
|
|
// GroupEndedSubject is where the runner says a group's end: mesh.seat.mesh-pipeline-runner.event.group-ended.<branch>.
|
|
// <group>, with no module part, since a group spans modules (design 47 G §332).
|
|
func GroupEndedSubject(branch, group string) string {
|
|
return eventPrefix + seat.EventGroupEnded + "." + keys.EscapeName(branch) + "." + keys.EscapeName(group)
|
|
}
|
|
|
|
// Of is what an event's subject names. A group-ended names no module.
|
|
type Of struct {
|
|
Event, Module, Branch, CommitOrGroup string
|
|
}
|
|
|
|
// Parse reads a subject Subject or GroupEndedSubject built: an event of the seat's, and its names unescaped.
|
|
func Parse(subject string) (Of, error) {
|
|
rest, ok := strings.CutPrefix(subject, eventPrefix)
|
|
toks := strings.Split(rest, ".")
|
|
want := 4
|
|
if len(toks) > 0 && toks[0] == seat.EventGroupEnded {
|
|
want = 3
|
|
}
|
|
if !ok || len(toks) != want || !slices.Contains(seat.Events(), toks[0]) {
|
|
return Of{}, fmt.Errorf("meshpipelinerunnerclient: %q is no event subject of the seat %s", subject, seat.Seat)
|
|
}
|
|
names := make([]string, 0, 3)
|
|
for i, t := range toks[1:] {
|
|
n, err := keys.UnescapeName(t)
|
|
if err != nil || n == "" {
|
|
return Of{}, fmt.Errorf("meshpipelinerunnerclient: %q: part %d: %v", subject, i+1, err)
|
|
}
|
|
names = append(names, n)
|
|
}
|
|
if want == 3 {
|
|
return Of{Event: toks[0], Branch: names[0], CommitOrGroup: names[1]}, nil
|
|
}
|
|
return Of{Event: toks[0], Module: names[0], Branch: names[1], CommitOrGroup: names[2]}, nil
|
|
}
|
|
|
|
// PipelineEnded decodes a pipeline-ended body, refusing one that names no pipeline, module, branch or commit.
|
|
func PipelineEnded(data []byte) (seat.PipelineEndedEvent, error) {
|
|
var e seat.PipelineEndedEvent
|
|
if err := json.Unmarshal(data, &e); err != nil {
|
|
return e, fmt.Errorf("meshpipelinerunnerclient: pipeline-ended: %w", err)
|
|
}
|
|
if e.Pipeline == "" || e.Module == "" || e.Branch == "" || e.Commit == "" {
|
|
return e, fmt.Errorf("meshpipelinerunnerclient: pipeline-ended names no pipeline, module, branch or commit")
|
|
}
|
|
return e, nil
|
|
}
|
|
|
|
// GroupEnded decodes a group-ended body, refusing one that names no group or branch, and each of its pipelines as
|
|
// PipelineEnded does.
|
|
func GroupEnded(data []byte) (seat.GroupEndedEvent, error) {
|
|
var g seat.GroupEndedEvent
|
|
if err := json.Unmarshal(data, &g); err != nil {
|
|
return g, fmt.Errorf("meshpipelinerunnerclient: group-ended: %w", err)
|
|
}
|
|
if g.Group == "" || g.Branch == "" {
|
|
return g, fmt.Errorf("meshpipelinerunnerclient: group-ended names no group or branch")
|
|
}
|
|
for _, p := range g.Pipelines {
|
|
if p.Pipeline == "" || p.Module == "" || p.Branch == "" || p.Commit == "" {
|
|
return g, fmt.Errorf("meshpipelinerunnerclient: group-ended %s: a pipeline names no pipeline, module, branch or commit", g.Group)
|
|
}
|
|
}
|
|
return g, nil
|
|
}
|
|
|
|
// StepEnded decodes a step-ended (or step-started) body, refusing one that names no pipeline, module or step.
|
|
func StepEnded(data []byte) (seat.StepEndedEvent, error) {
|
|
var e seat.StepEndedEvent
|
|
if err := json.Unmarshal(data, &e); err != nil {
|
|
return e, fmt.Errorf("meshpipelinerunnerclient: step event: %w", err)
|
|
}
|
|
if e.Pipeline == "" || e.Module == "" || e.Result.Step == "" {
|
|
return e, fmt.Errorf("meshpipelinerunnerclient: step event names no pipeline, module or step")
|
|
}
|
|
return e, nil
|
|
}
|