| 1 | package export |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "reflect" |
| 11 | "unicode" |
| 12 | "unicode/utf8" |
| 13 | |
| 14 | "the-valley/exactjson" |
| 15 | "the-valley/knot" |
| 16 | ) |
| 17 | |
| 18 | // Schema is the export.json document's schema. A change to any field's |
| 19 | // name or meaning is a new schema version. |
| 20 | const Schema = "valley-web-export/v1" |
| 21 | |
| 22 | // Visibility is who may read an exported project. Public is the only |
| 23 | // visibility v1 carries: the export holds what an anonymous reader may see. |
| 24 | type Visibility string |
| 25 | |
| 26 | const Public Visibility = "public" |
| 27 | |
| 28 | // Export is export.json: every public project of one valley, as an |
| 29 | // anonymous reader may see it. |
| 30 | type Export struct { |
| 31 | Schema string `json:"schema"` |
| 32 | // Collected is when the plan was read (valley-plan/v1's now), in |
| 33 | // RFC 3339. It is display-time: the content digest leaves it out. |
| 34 | Collected string `json:"collected"` |
| 35 | // Groups are the groups the projects belong to, by identifier, and |
| 36 | // no other. An export with none omits it, as v1 exports before |
| 37 | // groups did. |
| 38 | Groups []Group `json:"groups,omitempty"` |
| 39 | Projects []Project `json:"projects"` |
| 40 | } |
| 41 | |
| 42 | // Project is one exported project. Its two tips are different refs: the |
| 43 | // graph tip is refs/the-valley/knots, which its knots were read at, and |
| 44 | // the source tip is main, which its bundle carries. |
| 45 | type Project struct { |
| 46 | Name string `json:"project"` |
| 47 | Visibility Visibility `json:"visibility"` |
| 48 | // Group is the identifier of the project's group. WebName is its name |
| 49 | // within the group, the second segment of its web URLs, which Name, |
| 50 | // its identifier, need not be. FormerNames are its former web names |
| 51 | // within the group, and Aliases its short aliases. A project with no |
| 52 | // group, as in a v1 export from before groups, has no web URL, and a |
| 53 | // server serves it as one the export does not hold. |
| 54 | Group string `json:"group,omitempty"` |
| 55 | WebName string `json:"name,omitempty"` |
| 56 | FormerNames []string `json:"former_names,omitempty"` |
| 57 | Aliases []string `json:"aliases,omitempty"` |
| 58 | // GraphTip is "" for a project with no plan graph. |
| 59 | GraphTip string `json:"graph_tip"` |
| 60 | SourceTip string `json:"source_tip"` |
| 61 | // Roots are the outcomes with no parent, in tree order. |
| 62 | Roots []string `json:"roots"` |
| 63 | // Knots are every knot, in tree order, each with its body and comments. |
| 64 | Knots []Knot `json:"knots"` |
| 65 | // InFlight are the active knots that are claimed, the oldest claim |
| 66 | // first, then the active knots whose status is in-progress and that |
| 67 | // nobody claims, in tree order. |
| 68 | InFlight []string `json:"in_flight"` |
| 69 | OpenResiduals []Residuals `json:"open_residuals"` |
| 70 | // Changes are the changes ready for approval: ready, with an active |
| 71 | // version that has not landed. |
| 72 | Changes []Change `json:"changes"` |
| 73 | } |
| 74 | |
| 75 | // Knot is one knot: its fields, what the graph derives of it, its edges to |
| 76 | // other projects' knots, and its body and comments. Every principal name |
| 77 | // in it has passed through the publisher's display mapping. |
| 78 | type Knot struct { |
| 79 | ID string `json:"id"` |
| 80 | Type string `json:"type"` |
| 81 | Status string `json:"status"` |
| 82 | Active bool `json:"active"` |
| 83 | Title string `json:"title"` |
| 84 | Created string `json:"created"` |
| 85 | CreatedBy string `json:"created_by"` |
| 86 | Source string `json:"source,omitempty"` |
| 87 | Parent string `json:"parent,omitempty"` |
| 88 | Owner string `json:"owner,omitempty"` |
| 89 | BlockedBy []string `json:"blocked_by"` |
| 90 | Changes []string `json:"changes"` |
| 91 | Target string `json:"target,omitempty"` |
| 92 | GraduatedInto string `json:"graduated_into,omitempty"` |
| 93 | |
| 94 | // State is valley-plan/v1's: ready, in-progress, blocked, active, |
| 95 | // done or abandoned. |
| 96 | State string `json:"state"` |
| 97 | Ready bool `json:"ready"` |
| 98 | WaitingOn []string `json:"waiting_on"` |
| 99 | // WaitingOnHidden is set when the knot also waits on one or more |
| 100 | // knots of projects the export does not hold, which WaitingOn leaves |
| 101 | // out. It says neither which nor how many. An export with no such |
| 102 | // knot omits it everywhere, as v1 exports before it did. |
| 103 | WaitingOnHidden bool `json:"waiting_on_hidden,omitempty"` |
| 104 | ClearedByEnding []string `json:"cleared_by_ending"` |
| 105 | Dangling []string `json:"dangling"` |
| 106 | Unresolved []string `json:"unresolved"` |
| 107 | Children []string `json:"children"` |
| 108 | Blocks []string `json:"blocks"` |
| 109 | Progress *Progress `json:"progress,omitempty"` |
| 110 | Claim *Claim `json:"claim,omitempty"` |
| 111 | Edges []Edge `json:"edges"` |
| 112 | |
| 113 | Body string `json:"body"` |
| 114 | Comments []Comment `json:"comments"` |
| 115 | CommentCount int `json:"comment_count"` |
| 116 | } |
| 117 | |
| 118 | // Progress is a parent's finished descendants, out of those not abandoned. |
| 119 | type Progress struct { |
| 120 | Done int `json:"done"` |
| 121 | Of int `json:"of"` |
| 122 | } |
| 123 | |
| 124 | // Claim is a knot's claim. AgeSeconds and Stale are as of Collected; they |
| 125 | // are display-time, and a reader derives a claim's age from At. |
| 126 | type Claim struct { |
| 127 | By string `json:"by"` |
| 128 | At string `json:"at"` |
| 129 | AgeSeconds int64 `json:"age_seconds"` |
| 130 | Stale bool `json:"stale"` |
| 131 | } |
| 132 | |
| 133 | // Edge is a reference to another project's knot, from blocked_by or |
| 134 | // target. State is resolved or dangling, as valley-plan/v1 says, or hidden |
| 135 | // when the project is not exported. A hidden edge carries the reference |
| 136 | // alone: no status, no title and no why. |
| 137 | type Edge struct { |
| 138 | Field string `json:"field"` |
| 139 | Ref string `json:"ref"` |
| 140 | Project string `json:"project"` |
| 141 | ID string `json:"id"` |
| 142 | State string `json:"state"` |
| 143 | Why *string `json:"why"` |
| 144 | Status *string `json:"status"` |
| 145 | Title *string `json:"title"` |
| 146 | } |
| 147 | |
| 148 | // Edge states. |
| 149 | const ( |
| 150 | EdgeResolved = "resolved" |
| 151 | EdgeDangling = "dangling" |
| 152 | EdgeHidden = "hidden" |
| 153 | ) |
| 154 | |
| 155 | // Comment is one comment on a knot, oldest first. Op is the operation that |
| 156 | // wrote it, which with the knot names the comment. A comment that has been |
| 157 | // edited says when it was last edited, in Edited, and by whom, in |
| 158 | // EditedBy; one never edited has neither. |
| 159 | type Comment struct { |
| 160 | Author string `json:"author"` |
| 161 | At string `json:"at"` |
| 162 | Op string `json:"op"` |
| 163 | Edited string `json:"edited,omitempty"` |
| 164 | EditedBy string `json:"edited_by,omitempty"` |
| 165 | Body string `json:"body"` |
| 166 | } |
| 167 | |
| 168 | // Residuals are the open bugs under one parent, or under none. |
| 169 | type Residuals struct { |
| 170 | Parent *string `json:"parent"` |
| 171 | Bugs []string `json:"bugs"` |
| 172 | } |
| 173 | |
| 174 | // Change is a change ready for approval, with its active version alone. |
| 175 | type Change struct { |
| 176 | ChangeID string `json:"change_id"` |
| 177 | Handle string `json:"handle"` |
| 178 | Target string `json:"target"` |
| 179 | Owner string `json:"owner"` |
| 180 | Version Version `json:"version"` |
| 181 | // Includes are the changes this one includes that the export carries. |
| 182 | Includes []string `json:"includes"` |
| 183 | Knots []KnotRef `json:"knots"` |
| 184 | } |
| 185 | |
| 186 | // Version is a change's active version. |
| 187 | type Version struct { |
| 188 | N int `json:"n"` |
| 189 | Head string `json:"head"` |
| 190 | Title string `json:"title"` |
| 191 | Uploader string `json:"uploader"` |
| 192 | // Landing is not-landed or uncertain: a ready change's active version |
| 193 | // has not landed. |
| 194 | Landing string `json:"landing"` |
| 195 | Request *Request `json:"request"` |
| 196 | Outcome *Outcome `json:"outcome"` |
| 197 | } |
| 198 | |
| 199 | // Request is the integration request that approves the version's head. |
| 200 | type Request struct { |
| 201 | Commit string `json:"commit"` |
| 202 | } |
| 203 | |
| 204 | // Outcome is the integrator's last decision about the version. |
| 205 | type Outcome struct { |
| 206 | Decision string `json:"decision"` |
| 207 | Head string `json:"head"` |
| 208 | Tip *string `json:"tip"` |
| 209 | } |
| 210 | |
| 211 | // KnotRef is a knot of the same project that links a change. |
| 212 | type KnotRef struct { |
| 213 | ID string `json:"id"` |
| 214 | Type string `json:"type"` |
| 215 | Status string `json:"status"` |
| 216 | Title string `json:"title"` |
| 217 | } |
| 218 | |
| 219 | // Encode writes the export as export.json's bytes: compact JSON and a |
| 220 | // closing newline. |
| 221 | func (e *Export) Encode() ([]byte, error) { |
| 222 | if err := e.Validate(); err != nil { |
| 223 | return nil, err |
| 224 | } |
| 225 | b, err := json.Marshal(e) |
| 226 | if err != nil { |
| 227 | return nil, err |
| 228 | } |
| 229 | return append(b, '\n'), nil |
| 230 | } |
| 231 | |
| 232 | // Decode reads export.json strictly: a key that is not the name of a |
| 233 | // field the schema names, spelled exactly, a key twice in one object, |
| 234 | // anything but white space after the document, or an export Validate |
| 235 | // refuses, fails the read. |
| 236 | func Decode(data []byte) (*Export, error) { |
| 237 | return DecodeContext(context.Background(), bytes.NewReader(data)) |
| 238 | } |
| 239 | |
| 240 | // DecodeContext reads and validates an export while ctx remains active. |
| 241 | // It reads the document twice, as exactjson.Decode does: first through |
| 242 | // exactjson.Check, which refuses a key not spelled exactly as a field's |
| 243 | // name, a key twice in one object, an array or object where the field |
| 244 | // holds none, and nesting past exactjson.MaxDepth, and then into the |
| 245 | // export. Both reads check ctx between tokens and within long strings. |
| 246 | func DecodeContext(ctx context.Context, r io.Reader) (*Export, error) { |
| 247 | var data bytes.Buffer |
| 248 | dec := newTokenDecoder(ctx, io.TeeReader(r, &data)) |
| 249 | if err := exactjson.Check(dec, reflect.TypeFor[Export]()); err != nil { |
| 250 | if stopped := ctx.Err(); stopped != nil { |
| 251 | return nil, stopped |
| 252 | } |
| 253 | return nil, fmt.Errorf("export.json: %w", err) |
| 254 | } |
| 255 | if _, err := dec.Token(); err != io.EOF { |
| 256 | if ctx.Err() != nil { |
| 257 | return nil, ctx.Err() |
| 258 | } |
| 259 | return nil, errors.New("export.json holds more than its one document") |
| 260 | } |
| 261 | var e Export |
| 262 | if err := decodeValue(ctx, newTokenDecoder(ctx, &data), reflect.ValueOf(&e).Elem()); err != nil { |
| 263 | return nil, fmt.Errorf("export.json: %w", err) |
| 264 | } |
| 265 | if err := e.ValidateContext(ctx); err != nil { |
| 266 | return nil, err |
| 267 | } |
| 268 | return &e, nil |
| 269 | } |
| 270 | |
| 271 | // Validate holds an export to what a server may route by and show without |
| 272 | // further checks: its schema, project names that are safe as file names, |
| 273 | // one entry per project in name order, object IDs for tips, and knot IDs |
| 274 | // that are unique within their project. Its groups and the places of its |
| 275 | // projects pass CheckNames, and each group is some project's. Text fields |
| 276 | // are valid UTF-8 with no control character other than a newline or a |
| 277 | // tab. |
| 278 | func (e *Export) Validate() error { |
| 279 | return e.ValidateContext(context.Background()) |
| 280 | } |
| 281 | |
| 282 | // ValidateContext checks the export while ctx remains active. |
| 283 | func (e *Export) ValidateContext(ctx context.Context) error { |
| 284 | if err := ctx.Err(); err != nil { |
| 285 | return err |
| 286 | } |
| 287 | if e.Schema != Schema { |
| 288 | return fmt.Errorf("export.json has schema %q, not %q", e.Schema, Schema) |
| 289 | } |
| 290 | for i, p := range e.Projects { |
| 291 | if err := ctx.Err(); err != nil { |
| 292 | return err |
| 293 | } |
| 294 | if !ValidName(p.Name) { |
| 295 | return fmt.Errorf("export.json names a project %q, which is not a project name", p.Name) |
| 296 | } |
| 297 | if i > 0 && e.Projects[i-1].Name >= p.Name { |
| 298 | return fmt.Errorf("export.json lists project %s out of order or twice", p.Name) |
| 299 | } |
| 300 | if p.Visibility != Public { |
| 301 | return fmt.Errorf("project %s has visibility %q; v1 exports public projects alone", p.Name, p.Visibility) |
| 302 | } |
| 303 | if p.GraphTip != "" && !ValidObjectID(p.GraphTip) { |
| 304 | return fmt.Errorf("project %s has graph tip %q, which is not an object ID", p.Name, p.GraphTip) |
| 305 | } |
| 306 | if !ValidObjectID(p.SourceTip) { |
| 307 | return fmt.Errorf("project %s has source tip %q, which is not an object ID", p.Name, p.SourceTip) |
| 308 | } |
| 309 | seen := map[string]bool{} |
| 310 | for _, k := range p.Knots { |
| 311 | if err := ctx.Err(); err != nil { |
| 312 | return err |
| 313 | } |
| 314 | if !knot.IsLocalID(k.ID) { |
| 315 | return fmt.Errorf("project %s holds a knot %q, which is not a knot ID", p.Name, k.ID) |
| 316 | } |
| 317 | if seen[k.ID] { |
| 318 | return fmt.Errorf("project %s holds knot %s twice", p.Name, k.ID) |
| 319 | } |
| 320 | seen[k.ID] = true |
| 321 | for _, s := range []string{k.Title, k.Body, k.Source, k.CreatedBy, k.Owner} { |
| 322 | if err := ctx.Err(); err != nil { |
| 323 | return err |
| 324 | } |
| 325 | if !validTextContext(ctx, s) { |
| 326 | if err := ctx.Err(); err != nil { |
| 327 | return err |
| 328 | } |
| 329 | return fmt.Errorf("knot %s:%s holds text that is not valid UTF-8 or holds a control character", p.Name, k.ID) |
| 330 | } |
| 331 | } |
| 332 | for _, c := range k.Comments { |
| 333 | if err := ctx.Err(); err != nil { |
| 334 | return err |
| 335 | } |
| 336 | if !validTextContext(ctx, c.Body) || !validTextContext(ctx, c.Author) || !validTextContext(ctx, c.EditedBy) || !validTextContext(ctx, c.Edited) { |
| 337 | if err := ctx.Err(); err != nil { |
| 338 | return err |
| 339 | } |
| 340 | 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) |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | for _, c := range p.Changes { |
| 345 | if err := ctx.Err(); err != nil { |
| 346 | return err |
| 347 | } |
| 348 | if !ValidObjectID(c.Version.Head) { |
| 349 | return fmt.Errorf("change %s of project %s has head %q, which is not an object ID", c.ChangeID, p.Name, c.Version.Head) |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | return e.checkGroups(ctx) |
| 354 | } |
| 355 | |
| 356 | // checkGroups holds the export's groups and the places of its projects, |
| 357 | // which are in project order already, to CheckNames, and each group to |
| 358 | // having a project, so an export names no group that only private projects |
| 359 | // belong to. It checks ctx before each project and each group. |
| 360 | func (e *Export) checkGroups(ctx context.Context) error { |
| 361 | var placed []Placed |
| 362 | named := map[string]bool{} |
| 363 | for _, p := range e.Projects { |
| 364 | if err := ctx.Err(); err != nil { |
| 365 | return err |
| 366 | } |
| 367 | if p.Group != "" || p.WebName != "" || len(p.FormerNames) > 0 || len(p.Aliases) > 0 { |
| 368 | placed = append(placed, Placed{p.Name, Placement{Group: p.Group, Name: p.WebName, FormerNames: p.FormerNames, Aliases: p.Aliases}}) |
| 369 | named[p.Group] = true |
| 370 | } |
| 371 | } |
| 372 | if err := CheckNames(ctx, e.Groups, placed); err != nil { |
| 373 | if stopped := ctx.Err(); stopped != nil { |
| 374 | return stopped |
| 375 | } |
| 376 | return fmt.Errorf("export.json: %w", err) |
| 377 | } |
| 378 | for _, g := range e.Groups { |
| 379 | if err := ctx.Err(); err != nil { |
| 380 | return err |
| 381 | } |
| 382 | if !named[g.ID] { |
| 383 | return fmt.Errorf("export.json lists group %s, which none of its projects belongs to", g.ID) |
| 384 | } |
| 385 | } |
| 386 | return nil |
| 387 | } |
| 388 | |
| 389 | // ValidName reports whether s is a project's name: the plan graph's rule |
| 390 | // for a project's or a principal's name. Such a name holds no slash and |
| 391 | // cannot be a dot segment, so it is safe as a file name. |
| 392 | func ValidName(s string) bool { return knot.IsName(s) } |
| 393 | |
| 394 | // ValidObjectID reports whether s is a full git object ID, SHA-1 or |
| 395 | // SHA-256, in lowercase hex. |
| 396 | func ValidObjectID(s string) bool { |
| 397 | return (len(s) == 40 || len(s) == 64) && isHex(s) |
| 398 | } |
| 399 | |
| 400 | func isHex(s string) bool { |
| 401 | for _, c := range []byte(s) { |
| 402 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| 403 | return false |
| 404 | } |
| 405 | } |
| 406 | return true |
| 407 | } |
| 408 | |
| 409 | // validText is valid UTF-8 holding no control character but a newline or |
| 410 | // a tab. |
| 411 | func validText(s string) bool { |
| 412 | return validTextContext(context.Background(), s) |
| 413 | } |
| 414 | |
| 415 | func validTextContext(ctx context.Context, s string) bool { |
| 416 | nextCheck := 0 |
| 417 | for i := 0; i < len(s); { |
| 418 | if i >= nextCheck { |
| 419 | if ctx.Err() != nil { |
| 420 | return false |
| 421 | } |
| 422 | nextCheck = i + 4096 |
| 423 | } |
| 424 | r, n := utf8.DecodeRuneInString(s[i:]) |
| 425 | if (r == utf8.RuneError && n == 1) || (unicode.IsControl(r) && r != '\n' && r != '\t') { |
| 426 | return false |
| 427 | } |
| 428 | i += n |
| 429 | } |
| 430 | return ctx.Err() == nil |
| 431 | } |