gunk-dev/the-valley

changes.go

source88a5a486b52fmain's published tip history
10.0 KiB
1package valley
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "strings"
8)
9
10// SchemaV1 is the schema of the machine view `valley changes --json` prints.
11const SchemaV1 = "valley-changes/v1"
12
13// Landing values: what a valley knows about whether a version landed.
14const (
15 Landed = "landed" // the version's head is an ancestor of main
16 LandedRecorded = "landed-recorded" // proven by the integrator's signed landing statement
17 Uncertain = "uncertain" // a request was consumed or failed, with no landing evidence
18 NotLanded = "not-landed"
19)
20
21// Change states.
22const (
23 StateReady = "ready"
24 StateWithdrawn = "withdrawn"
25 // StateDraft is a change its submitter returned from ready (`valley
26 // unready`), as launch does when a follow-up run continues it. approve
27 // refuses it until a head is marked ready again.
28 StateDraft = "draft"
29)
30
31// Changes is a valley's view of every change record it serves, in every
32// state (valley-changes/v1).
33type Changes struct {
34 Schema string `json:"schema"`
35 Host string `json:"host"` // the serving host, as reached
36 Repos []Repo `json:"repos"`
37}
38
39// Repo is one repository the valley serves.
40type Repo struct {
41 Repo string `json:"repo"`
42 URL string `json:"url"`
43 // Error is set (non-null) when the valley couldn't read the repo.
44 Error json.RawMessage `json:"error"`
45 Changes []Change `json:"changes"`
46 Branches []Branch `json:"branches"`
47}
48
49// Err returns why the valley couldn't read the repo, or "" when it could.
50func (r Repo) Err() string {
51 raw := bytes.TrimSpace(r.Error)
52 if len(raw) == 0 || string(raw) == "null" {
53 return ""
54 }
55 var s string
56 if json.Unmarshal(raw, &s) == nil {
57 return s
58 }
59 return string(raw)
60}
61
62// Change is a change record. Changes are keyed by (repo, target, change ID).
63type Change struct {
64 ChangeID string `json:"change_id"`
65 Handle string `json:"handle"` // shortest unique prefix of the ID, at least 4 characters
66 Target string `json:"target"`
67 Owner string `json:"owner"`
68 State string `json:"state"` // StateReady, StateDraft or StateWithdrawn
69 Record string `json:"record"`
70 Seq int `json:"seq"`
71 Active int `json:"active"` // n of the active version
72 Versions []Version `json:"versions"`
73 // Includes names the changes whose active head is a strict ancestor of
74 // this one's active head, nearest only.
75 Includes []string `json:"includes"`
76 // Knots are the knots in the repo's plan graph that link the change
77 // (valley:<repo>/<change ID>), active first; a valley from before plan
78 // graphs lists none.
79 Knots []ChangeKnot `json:"knots"`
80 // Restack is the latest report the host's restacker wrote about the
81 // change, or nil for none; a valley from before restack reports lists
82 // none.
83 Restack *Restack `json:"restack"`
84 // LandedVersion and LandedCommit are the valley's verdict on whether the
85 // change landed (its LandedAs): the version it landed as and the commit
86 // on main, or null while it hasn't, as for a next round made on a main
87 // that already holds a landing.
88 LandedVersion LandedVersion `json:"landed_version"`
89 LandedCommit *string `json:"landed_commit"`
90}
91
92// LandedVersion is a change's landed_version: N is the version the valley
93// says the change landed as, or nil for none. Listed is false when the
94// listing has no such field, as from a valley older than it.
95type LandedVersion struct {
96 Listed bool
97 N *int
98}
99
100// UnmarshalJSON reads a landed_version the listing carries, null included.
101func (l *LandedVersion) UnmarshalJSON(data []byte) error {
102 l.Listed = true
103 return json.Unmarshal(data, &l.N)
104}
105
106// Restack is a change's latest restack report as the valley lists it: its
107// ref and commit on origin, whether it is current (every ref it observed
108// still holds what it observed), and what it says.
109type Restack struct {
110 Ref string `json:"ref"`
111 Commit string `json:"commit"`
112 Current bool `json:"current"`
113 Report *RestackReport `json:"report"`
114}
115
116// RestackSchema is the schema of a restack report.
117const RestackSchema = "valley-restack-report/v1"
118
119// What came of a restack attempt.
120const (
121 RestackConflict = "conflict" // a layer's rebase conflicted; nothing was published
122 RestackBlocked = "blocked" // integration requests held it back
123 RestackPartial = "partial" // branches moved, and not every version was added
124 RestackComplete = "complete" // every layer was rebased and versioned
125 RestackSuperseded = "superseded" // its inputs moved while it ran
126)
127
128// RestackReport is what the restacker observed when it tried to rebase a
129// ready change onto its target (valley-restack-report/v1). It is an
130// observation, not evidence: klaus compares its inputs with what origin
131// holds before acting on it, and never runs or relays its free text.
132// Heads and commits carry the "git-sha1:" prefix.
133type RestackReport struct {
134 Schema string `json:"schema"`
135 ChangeID string `json:"change_id"`
136 Target string `json:"target"`
137 TargetTip string `json:"target_tip"`
138 Operation string `json:"operation"`
139 Result string `json:"result"`
140 Layers []RestackLayer `json:"layers"` // the changes rebased together, bottom first
141 Failing *string `json:"failing_layer"`
142 Conflicts []string `json:"conflict_paths"`
143 Refusal *string `json:"refusal"`
144}
145
146// RestackLayer is one change a restack attempt rebased, as it read it.
147// BranchTip is nil when origin had no such branch.
148type RestackLayer struct {
149 ChangeID string `json:"change_id"`
150 Record string `json:"record"`
151 Version int `json:"version"`
152 Head string `json:"head"`
153 Branch string `json:"branch"`
154 BranchTip *string `json:"branch_tip"`
155 Published *RestackPublished `json:"published"`
156}
157
158// RestackPublished is what an attempt published for a layer.
159type RestackPublished struct {
160 Head string `json:"head"`
161 Version *int `json:"version"`
162 Record *string `json:"record"`
163}
164
165// Layer returns the report's layer for the change id.
166func (r *RestackReport) Layer(id string) (RestackLayer, bool) {
167 for _, l := range r.Layers {
168 if l.ChangeID == id {
169 return l, true
170 }
171 }
172 return RestackLayer{}, false
173}
174
175// headPrefix opens every head and commit a restack report names.
176const headPrefix = "git-sha1:"
177
178// ReportOID returns the object ID a report names, without its "git-sha1:"
179// prefix, or "" when s isn't one: the prefix and 40 lowercase hex digits.
180func ReportOID(s string) string {
181 oid, ok := strings.CutPrefix(s, headPrefix)
182 if !ok || !IsOID(oid) {
183 return ""
184 }
185 return oid
186}
187
188// IsOID reports whether s is a full SHA-1 object ID in lowercase hex.
189func IsOID(s string) bool {
190 if len(s) != 40 {
191 return false
192 }
193 for _, c := range s {
194 if !('0' <= c && c <= '9' || 'a' <= c && c <= 'f') {
195 return false
196 }
197 }
198 return true
199}
200
201// ChangeKnot is a knot that links a change.
202type ChangeKnot struct {
203 ID string `json:"id"`
204 Type string `json:"type"`
205 Status string `json:"status"`
206 Title string `json:"title"`
207}
208
209// Version is one head marked ready as a version of a change.
210type Version struct {
211 N int `json:"n"`
212 Head string `json:"head"`
213 Title string `json:"title"`
214 Branch string `json:"branch"`
215 Uploader string `json:"uploader"`
216 Landing string `json:"landing"`
217 LandedCommit *string `json:"landed_commit"`
218 Request *Request `json:"request"` // the integration request filed for it, if any
219 Outcome *Outcome `json:"outcome"` // the integrator's decision on that request, if any
220 // Checks is where the host's check worker stands on the version's
221 // required checks. The valley lists it on a ready change's active
222 // version only; nil means unknown: the change isn't ready, or the
223 // valley is from before checks were listed.
224 Checks *Checks `json:"checks"`
225}
226
227// The states of a version's checks, and of each required check.
228const (
229 ChecksPassed = "passed"
230 ChecksFailed = "failed"
231 ChecksPending = "pending"
232 // ChecksOther is a required check on a runner the host's worker
233 // doesn't run. It never decides the overall state.
234 ChecksOther = "other"
235)
236
237// Checks is the host's check worker's view of a version's required checks.
238type Checks struct {
239 State string `json:"state"` // ChecksPassed, ChecksFailed or ChecksPending
240 Tree string `json:"tree"` // the digest of the head's tree the checks ran on
241 Required []Check `json:"required"`
242}
243
244// Check is one required check.
245type Check struct {
246 Name string `json:"name"`
247 State string `json:"state"` // ChecksPassed, ChecksFailed, ChecksPending or ChecksOther
248 Signer string `json:"signer,omitempty"`
249 Why string `json:"why,omitempty"` // why it failed, as the worker says
250}
251
252// Failed returns the required checks that failed, in listed order.
253func (c *Checks) Failed() []Check {
254 if c == nil {
255 return nil
256 }
257 var out []Check
258 for _, r := range c.Required {
259 if r.State == ChecksFailed {
260 out = append(out, r)
261 }
262 }
263 return out
264}
265
266// FailedNames returns the names of the required checks that failed.
267func (c *Checks) FailedNames() []string {
268 var out []string
269 for _, r := range c.Failed() {
270 out = append(out, r.Name)
271 }
272 return out
273}
274
275// Request is an integration request.
276type Request struct {
277 Ref string `json:"ref"`
278 Commit string `json:"commit"`
279}
280
281// Outcome is the integrator's decision on a request.
282type Outcome struct {
283 Decision string `json:"decision"`
284 Head string `json:"head"`
285 Tip string `json:"tip"`
286}
287
288// Branch is a branch on the repo's origin.
289type Branch struct {
290 Branch string `json:"branch"`
291 Head string `json:"head"`
292}
293
294// Decode parses the output of `valley changes --json`. It refuses any schema
295// but SchemaV1, since a later schema may change what the fields mean, and
296// ignores fields it doesn't know.
297func Decode(data []byte) (*Changes, error) {
298 var c Changes
299 if err := json.Unmarshal(data, &c); err != nil {
300 return nil, fmt.Errorf("decoding valley changes: %w", err)
301 }
302 if c.Schema != SchemaV1 {
303 return nil, fmt.Errorf("valley changes: schema %q, want %q", c.Schema, SchemaV1)
304 }
305 return &c, nil
306}