gunk-dev/the-valley

bus.go

source28ba29740930main's published tip history
3.3 KiB
1package main
2
3// Publishing outcomes. The mechanism is the post-receive hook's, exactly:
4// one `nats pub` per event, on valley.git.<repo>.<event>, with a payload
5// satisfying schema/events.cue. Failure to publish is logged and never
6// fatal — git is the source of truth and the bus is the replaceable
7// component, so a bus problem costs a log line and one `valley replay`.
8//
9// Nothing here consumes. Bus authentication (bd-d853d9c) gates automated
10// consumers, and the integrator level-triggers over the request refs
11// instead, so it needs no subscription.
12
13import (
14 "the-valley/integrator/verdict"
15
16 "encoding/json"
17 "fmt"
18 "os/exec"
19)
20
21func (in *integrator) publishLanded(ch verdict.Change, v verdict.Verdict, old, new string) {
22 transferred := []string{}
23 for _, cv := range v.Checks {
24 if cv.Transferred {
25 transferred = append(transferred, cv.Name)
26 }
27 }
28 in.publish("integration-succeeded", map[string]any{
29 "event": "integration-succeeded",
30 "repo": in.project,
31 "change": ch.ID,
32 "target": ch.Target,
33 "old": old,
34 "new": new,
35 "transferred": transferred,
36 })
37}
38
39func (in *integrator) publishStale(ch verdict.Change, v verdict.Verdict, tip string) {
40 checks := v.Invalidated
41 if checks == nil {
42 checks = []string{}
43 }
44 in.publish("request-stale", map[string]any{
45 "event": "request-stale",
46 "repo": in.project,
47 "change": ch.ID,
48 "target": ch.Target,
49 "tip": tip,
50 "reason": v.StaleReason,
51 "checks": checks,
52 })
53}
54
55// publish writes one event. The payload is validated against the event
56// vocabulary before it is sent when a schema is configured: an event no
57// consumer could read is not a thing to publish, and the discipline that
58// keeps the vocabulary one schema'd event at a time is worth nothing if the
59// publisher can sidestep it.
60func (in *integrator) publish(kind string, payload map[string]any) {
61 body, err := json.Marshal(payload)
62 if err != nil {
63 fmt.Fprintf(in.out, " bus %s not published: %v\n", kind, err)
64 return
65 }
66 if err := in.vetEvent(kind, body); err != nil {
67 fmt.Fprintf(in.out, " bus %s not published: %v\n", kind, err)
68 return
69 }
70 fmt.Fprintf(in.out, " event %s %s\n", kind, body)
71 if in.bus == "" {
72 return
73 }
74 subject := fmt.Sprintf("valley.git.%s.%s", in.project, kind)
75 cmd := exec.Command(in.nats, "--server", in.bus, "pub", subject, string(body))
76 if out, err := cmd.CombinedOutput(); err != nil {
77 fmt.Fprintf(in.out, " bus publish of %s FAILED: %v: %s\n", subject, err, firstLine(string(out)))
78 return
79 }
80 fmt.Fprintf(in.out, " bus %s\n", subject)
81}
82
83// eventDefinition names the schema definition a payload is vetted against.
84var eventDefinition = map[string]string{
85 "integration-succeeded": "#IntegrationSucceeded",
86 "request-stale": "#RequestStale",
87}
88
89func (in *integrator) vetEvent(kind string, body []byte) error {
90 if in.eventSchema == "" {
91 return nil
92 }
93 def, ok := eventDefinition[kind]
94 if !ok {
95 return fmt.Errorf("%s is not in the event vocabulary", kind)
96 }
97 file, err := writeTemp("valley-integrator-event", body)
98 if err != nil {
99 return err
100 }
101 defer removeTemp(file)
102 cmd := exec.Command(in.cue, "vet", "-d", def, in.eventSchema, file)
103 if out, err := cmd.CombinedOutput(); err != nil {
104 return fmt.Errorf("the payload does not satisfy %s: %s", def, firstLine(string(out)))
105 }
106 return nil
107}