gunk-dev/the-valley

bus.go

source467cc727ded8main's published tip history
3.2 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 "encoding/json"
15 "fmt"
16 "os/exec"
17)
18
19func (in *integrator) publishLanded(ch Change, v Verdict, old, new string) {
20 transferred := []string{}
21 for _, cv := range v.Checks {
22 if cv.Transferred {
23 transferred = append(transferred, cv.Name)
24 }
25 }
26 in.publish("integration-succeeded", map[string]any{
27 "event": "integration-succeeded",
28 "repo": in.project,
29 "change": ch.ID,
30 "target": ch.Target,
31 "old": old,
32 "new": new,
33 "transferred": transferred,
34 })
35}
36
37func (in *integrator) publishStale(ch Change, v Verdict, tip string) {
38 checks := v.Invalidated
39 if checks == nil {
40 checks = []string{}
41 }
42 in.publish("request-stale", map[string]any{
43 "event": "request-stale",
44 "repo": in.project,
45 "change": ch.ID,
46 "target": ch.Target,
47 "tip": tip,
48 "reason": v.StaleReason,
49 "checks": checks,
50 })
51}
52
53// publish writes one event. The payload is validated against the event
54// vocabulary before it is sent when a schema is configured: an event no
55// consumer could read is not a thing to publish, and the discipline that
56// keeps the vocabulary one schema'd event at a time is worth nothing if the
57// publisher can sidestep it.
58func (in *integrator) publish(kind string, payload map[string]any) {
59 body, err := json.Marshal(payload)
60 if err != nil {
61 fmt.Fprintf(in.out, " bus %s not published: %v\n", kind, err)
62 return
63 }
64 if err := in.vetEvent(kind, body); err != nil {
65 fmt.Fprintf(in.out, " bus %s not published: %v\n", kind, err)
66 return
67 }
68 fmt.Fprintf(in.out, " event %s %s\n", kind, body)
69 if in.bus == "" {
70 return
71 }
72 subject := fmt.Sprintf("valley.git.%s.%s", in.project, kind)
73 cmd := exec.Command(in.nats, "--server", in.bus, "pub", subject, string(body))
74 if out, err := cmd.CombinedOutput(); err != nil {
75 fmt.Fprintf(in.out, " bus publish of %s FAILED: %v: %s\n", subject, err, firstLine(string(out)))
76 return
77 }
78 fmt.Fprintf(in.out, " bus %s\n", subject)
79}
80
81// eventDefinition names the schema definition a payload is vetted against.
82var eventDefinition = map[string]string{
83 "integration-succeeded": "#IntegrationSucceeded",
84 "request-stale": "#RequestStale",
85}
86
87func (in *integrator) vetEvent(kind string, body []byte) error {
88 if in.eventSchema == "" {
89 return nil
90 }
91 def, ok := eventDefinition[kind]
92 if !ok {
93 return fmt.Errorf("%s is not in the event vocabulary", kind)
94 }
95 file, err := writeTemp("valley-integrator-event", body)
96 if err != nil {
97 return err
98 }
99 defer removeTemp(file)
100 cmd := exec.Command(in.cue, "vet", "-d", def, in.eventSchema, file)
101 if out, err := cmd.CombinedOutput(); err != nil {
102 return fmt.Errorf("the payload does not satisfy %s: %s", def, firstLine(string(out)))
103 }
104 return nil
105}