gunk-dev/the-valley

export.go

source19d534c7c066main's published tip history
10.0 KiB
1package export
2
3import (
4 "bytes"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "strings"
9 "unicode"
10 "unicode/utf8"
11
12 "the-valley/knot"
13)
14
15// Schema is the export.json document's schema. A change to any field's
16// name or meaning is a new schema version.
17const Schema = "valley-web-export/v1"
18
19// Visibility is who may read an exported project. Public is the only
20// visibility v1 carries: the export holds what an anonymous reader may see.
21type Visibility string
22
23const Public Visibility = "public"
24
25// Export is export.json: every public project of one valley, as an
26// anonymous reader may see it.
27type Export struct {
28 Schema string `json:"schema"`
29 // Collected is when the plan was read (valley-plan/v1's now), in
30 // RFC 3339. It is display-time: the content digest leaves it out.
31 Collected string `json:"collected"`
32 Projects []Project `json:"projects"`
33}
34
35// Project is one exported project. Its two tips are different refs: the
36// graph tip is refs/the-valley/knots, which its knots were read at, and
37// the source tip is main, which its bundle carries.
38type Project struct {
39 Name string `json:"project"`
40 Visibility Visibility `json:"visibility"`
41 // GraphTip is "" for a project with no plan graph.
42 GraphTip string `json:"graph_tip"`
43 SourceTip string `json:"source_tip"`
44 // Roots are the outcomes with no parent, in tree order.
45 Roots []string `json:"roots"`
46 // Knots are every knot, in tree order, each with its body and comments.
47 Knots []Knot `json:"knots"`
48 // InFlight are the active knots that are claimed, the oldest claim
49 // first, then the active knots whose status is in-progress and that
50 // nobody claims, in tree order.
51 InFlight []string `json:"in_flight"`
52 OpenResiduals []Residuals `json:"open_residuals"`
53 // Changes are the changes ready for approval: ready, with an active
54 // version that has not landed.
55 Changes []Change `json:"changes"`
56}
57
58// Knot is one knot: its fields, what the graph derives of it, its edges to
59// other projects' knots, and its body and comments. Every principal name
60// in it has passed through the publisher's display mapping.
61type Knot struct {
62 ID string `json:"id"`
63 Type string `json:"type"`
64 Status string `json:"status"`
65 Active bool `json:"active"`
66 Title string `json:"title"`
67 Created string `json:"created"`
68 CreatedBy string `json:"created_by"`
69 Source string `json:"source,omitempty"`
70 Parent string `json:"parent,omitempty"`
71 Owner string `json:"owner,omitempty"`
72 BlockedBy []string `json:"blocked_by"`
73 Changes []string `json:"changes"`
74 Target string `json:"target,omitempty"`
75 GraduatedInto string `json:"graduated_into,omitempty"`
76
77 // State is valley-plan/v1's: ready, in-progress, blocked, active,
78 // done or abandoned.
79 State string `json:"state"`
80 Ready bool `json:"ready"`
81 WaitingOn []string `json:"waiting_on"`
82 ClearedByEnding []string `json:"cleared_by_ending"`
83 Dangling []string `json:"dangling"`
84 Unresolved []string `json:"unresolved"`
85 Children []string `json:"children"`
86 Blocks []string `json:"blocks"`
87 Progress *Progress `json:"progress,omitempty"`
88 Claim *Claim `json:"claim,omitempty"`
89 Edges []Edge `json:"edges"`
90
91 Body string `json:"body"`
92 Comments []Comment `json:"comments"`
93 CommentCount int `json:"comment_count"`
94}
95
96// Progress is a parent's finished descendants, out of those not abandoned.
97type Progress struct {
98 Done int `json:"done"`
99 Of int `json:"of"`
100}
101
102// Claim is a knot's claim. AgeSeconds and Stale are as of Collected; they
103// are display-time, and a reader derives a claim's age from At.
104type Claim struct {
105 By string `json:"by"`
106 At string `json:"at"`
107 AgeSeconds int64 `json:"age_seconds"`
108 Stale bool `json:"stale"`
109}
110
111// Edge is a reference to another project's knot, from blocked_by or
112// target. State is resolved or dangling, as valley-plan/v1 says, or hidden
113// when the project is not exported. A hidden edge carries the reference
114// alone: no status, no title and no why.
115type Edge struct {
116 Field string `json:"field"`
117 Ref string `json:"ref"`
118 Project string `json:"project"`
119 ID string `json:"id"`
120 State string `json:"state"`
121 Why *string `json:"why"`
122 Status *string `json:"status"`
123 Title *string `json:"title"`
124}
125
126// Edge states.
127const (
128 EdgeResolved = "resolved"
129 EdgeDangling = "dangling"
130 EdgeHidden = "hidden"
131)
132
133// Comment is one comment on a knot, oldest first. Op is the operation that
134// wrote it, which with the knot names the comment.
135type Comment struct {
136 Author string `json:"author"`
137 At string `json:"at"`
138 Op string `json:"op"`
139 Body string `json:"body"`
140}
141
142// Residuals are the open bugs under one parent, or under none.
143type Residuals struct {
144 Parent *string `json:"parent"`
145 Bugs []string `json:"bugs"`
146}
147
148// Change is a change ready for approval, with its active version alone.
149type Change struct {
150 ChangeID string `json:"change_id"`
151 Handle string `json:"handle"`
152 Target string `json:"target"`
153 Owner string `json:"owner"`
154 Version Version `json:"version"`
155 // Includes are the changes this one includes that the export carries.
156 Includes []string `json:"includes"`
157 Knots []KnotRef `json:"knots"`
158}
159
160// Version is a change's active version.
161type Version struct {
162 N int `json:"n"`
163 Head string `json:"head"`
164 Title string `json:"title"`
165 Uploader string `json:"uploader"`
166 // Landing is not-landed or uncertain: a ready change's active version
167 // has not landed.
168 Landing string `json:"landing"`
169 Request *Request `json:"request"`
170 Outcome *Outcome `json:"outcome"`
171}
172
173// Request is the integration request that approves the version's head.
174type Request struct {
175 Commit string `json:"commit"`
176}
177
178// Outcome is the integrator's last decision about the version.
179type Outcome struct {
180 Decision string `json:"decision"`
181 Head string `json:"head"`
182 Tip *string `json:"tip"`
183}
184
185// KnotRef is a knot of the same project that links a change.
186type KnotRef struct {
187 ID string `json:"id"`
188 Type string `json:"type"`
189 Status string `json:"status"`
190 Title string `json:"title"`
191}
192
193// Encode writes the export as export.json's bytes: compact JSON and a
194// closing newline.
195func (e *Export) Encode() ([]byte, error) {
196 if err := e.Validate(); err != nil {
197 return nil, err
198 }
199 b, err := json.Marshal(e)
200 if err != nil {
201 return nil, err
202 }
203 return append(b, '\n'), nil
204}
205
206// Decode reads export.json strictly: a field the schema does not name, a
207// second document, or an export Validate refuses, fails the read.
208func Decode(data []byte) (*Export, error) {
209 dec := json.NewDecoder(bytes.NewReader(data))
210 dec.DisallowUnknownFields()
211 var e Export
212 if err := dec.Decode(&e); err != nil {
213 return nil, fmt.Errorf("export.json: %w", err)
214 }
215 if dec.More() {
216 return nil, errors.New("export.json holds more than one document")
217 }
218 if err := e.Validate(); err != nil {
219 return nil, err
220 }
221 return &e, nil
222}
223
224// Validate holds an export to what a server may route by and show without
225// further checks: its schema, project names that are safe as file names,
226// one entry per project in name order, object IDs for tips, and knot IDs
227// that are unique within their project. Text fields are valid UTF-8 with
228// no control character other than a newline or a tab.
229func (e *Export) Validate() error {
230 if e.Schema != Schema {
231 return fmt.Errorf("export.json has schema %q, not %q", e.Schema, Schema)
232 }
233 for i, p := range e.Projects {
234 if !ValidName(p.Name) {
235 return fmt.Errorf("export.json names a project %q, which is not a project name", p.Name)
236 }
237 if i > 0 && e.Projects[i-1].Name >= p.Name {
238 return fmt.Errorf("export.json lists project %s out of order or twice", p.Name)
239 }
240 if p.Visibility != Public {
241 return fmt.Errorf("project %s has visibility %q; v1 exports public projects alone", p.Name, p.Visibility)
242 }
243 if p.GraphTip != "" && !ValidObjectID(p.GraphTip) {
244 return fmt.Errorf("project %s has graph tip %q, which is not an object ID", p.Name, p.GraphTip)
245 }
246 if !ValidObjectID(p.SourceTip) {
247 return fmt.Errorf("project %s has source tip %q, which is not an object ID", p.Name, p.SourceTip)
248 }
249 seen := map[string]bool{}
250 for _, k := range p.Knots {
251 if !knot.IsLocalID(k.ID) {
252 return fmt.Errorf("project %s holds a knot %q, which is not a knot ID", p.Name, k.ID)
253 }
254 if seen[k.ID] {
255 return fmt.Errorf("project %s holds knot %s twice", p.Name, k.ID)
256 }
257 seen[k.ID] = true
258 for _, s := range []string{k.Title, k.Body, k.Source, k.CreatedBy, k.Owner} {
259 if !validText(s) {
260 return fmt.Errorf("knot %s:%s holds text that is not valid UTF-8 or holds a control character", p.Name, k.ID)
261 }
262 }
263 for _, c := range k.Comments {
264 if !validText(c.Body) || !validText(c.Author) {
265 return fmt.Errorf("a comment on knot %s:%s holds text that is not valid UTF-8 or holds a control character", p.Name, k.ID)
266 }
267 }
268 }
269 for _, c := range p.Changes {
270 if !ValidObjectID(c.Version.Head) {
271 return fmt.Errorf("change %s of project %s has head %q, which is not an object ID", c.ChangeID, p.Name, c.Version.Head)
272 }
273 }
274 }
275 return nil
276}
277
278// ValidName reports whether s is a project's name: the plan graph's rule
279// for a project's or a principal's name. Such a name holds no slash and
280// cannot be a dot segment, so it is safe as a file name.
281func ValidName(s string) bool { return knot.IsName(s) }
282
283// ValidObjectID reports whether s is a full git object ID, SHA-1 or
284// SHA-256, in lowercase hex.
285func ValidObjectID(s string) bool {
286 return (len(s) == 40 || len(s) == 64) && isHex(s)
287}
288
289func isHex(s string) bool {
290 for _, c := range []byte(s) {
291 if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
292 return false
293 }
294 }
295 return true
296}
297
298// validText is valid UTF-8 holding no control character but a newline or
299// a tab.
300func validText(s string) bool {
301 return utf8.ValidString(s) && !strings.ContainsFunc(s, func(r rune) bool {
302 return unicode.IsControl(r) && r != '\n' && r != '\t'
303 })
304}