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