| 1 | package export |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "slices" |
| 9 | "sort" |
| 10 | "strings" |
| 11 | "unicode" |
| 12 | "unicode/utf8" |
| 13 | ) |
| 14 | |
| 15 | // The schemas of the valley's own JSON the projection reads. |
| 16 | const ( |
| 17 | planSchema = "valley-plan/v1" |
| 18 | pendingSchema = "valley-pending/v1" |
| 19 | detailSchema = "valley-knot/v1" |
| 20 | ) |
| 21 | |
| 22 | // ErrInconsistent is the error for inputs read at different graph tips: a |
| 23 | // knot's detail read after the plan saw a later graph. The publisher reads |
| 24 | // the inputs again rather than publish them. |
| 25 | var ErrInconsistent = errors.New("the inputs were read at different graph tips") |
| 26 | |
| 27 | // Inputs are what the projection reads: the valley's own JSON, as its |
| 28 | // commands print it, and the publisher's configuration. |
| 29 | type Inputs struct { |
| 30 | // Plan is `valley plan --all --json`. |
| 31 | Plan []byte |
| 32 | // Pending is `valley pending --all --json`. |
| 33 | Pending []byte |
| 34 | // Details are `valley knot show --project P <id> --json`, one for |
| 35 | // every knot of every exported project. |
| 36 | Details [][]byte |
| 37 | // Identities are the publication identities of the projects to |
| 38 | // export, by project: every project with an effective reader at the |
| 39 | // destination, whoever that reader is. A project it does not name is |
| 40 | // not exported, and is not on the edge. |
| 41 | Identities map[string]string |
| 42 | // SourceTips are each exported project's main. |
| 43 | SourceTips map[string]string |
| 44 | // Principals is the display mapping principal names pass through. |
| 45 | Principals Principals |
| 46 | // Groups are the instance's groups, in identifier order, and |
| 47 | // Placements where the instance places each project on the web, by |
| 48 | // project, as the instance declares them. Every exported project has |
| 49 | // a group. The export carries the groups of its exported projects |
| 50 | // alone. |
| 51 | Groups []Group |
| 52 | Placements map[string]Placement |
| 53 | } |
| 54 | |
| 55 | // Principals maps a principal's name to what an export shows in its place, |
| 56 | // such as operator or agent. A name it does not map is shown as Unmapped. |
| 57 | type Principals map[string]string |
| 58 | |
| 59 | // Unmapped is what an export shows for a principal the display mapping |
| 60 | // does not name. |
| 61 | const Unmapped = "contributor" |
| 62 | |
| 63 | // Show is what an export shows for the principal name: its mapping, or |
| 64 | // Unmapped. The empty name, for a field that names nobody, stays empty. |
| 65 | func (p Principals) Show(name string) string { |
| 66 | if name == "" { |
| 67 | return "" |
| 68 | } |
| 69 | if shown, ok := p[name]; ok { |
| 70 | return shown |
| 71 | } |
| 72 | return Unmapped |
| 73 | } |
| 74 | |
| 75 | // check holds every shown name to 1 to 64 bytes of UTF-8 with no control |
| 76 | // character. |
| 77 | func (p Principals) check() error { |
| 78 | for name, shown := range p { |
| 79 | bad := len(shown) == 0 || len(shown) > 64 || !utf8.ValidString(shown) || |
| 80 | containsControl(shown) |
| 81 | if bad { |
| 82 | return fmt.Errorf("the display mapping shows %q as %q; a shown name is 1 to 64 bytes of text, with no control character", name, shown) |
| 83 | } |
| 84 | } |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | func containsControl(s string) bool { |
| 89 | for _, r := range s { |
| 90 | if unicode.IsControl(r) { |
| 91 | return true |
| 92 | } |
| 93 | } |
| 94 | return false |
| 95 | } |
| 96 | |
| 97 | // The valley's JSON, as far as the projection reads it. A field not named |
| 98 | // here is never read, so a field the valley adds is not exported until the |
| 99 | // projection names it. |
| 100 | type ( |
| 101 | planDoc struct { |
| 102 | Schema string `json:"schema"` |
| 103 | Now string `json:"now"` |
| 104 | Projects []planProject `json:"projects"` |
| 105 | } |
| 106 | planProject struct { |
| 107 | Project string `json:"project"` |
| 108 | Error *string `json:"error"` |
| 109 | Tip string `json:"tip"` |
| 110 | Roots []string `json:"roots"` |
| 111 | Knots []planKnot `json:"knots"` |
| 112 | InFlight []string `json:"in_flight"` |
| 113 | OpenResiduals []Residuals `json:"open_residuals"` |
| 114 | } |
| 115 | planKnot struct { |
| 116 | ID string `json:"id"` |
| 117 | Type string `json:"type"` |
| 118 | Status string `json:"status"` |
| 119 | Active bool `json:"active"` |
| 120 | Title string `json:"title"` |
| 121 | Created string `json:"created"` |
| 122 | CreatedBy string `json:"created_by"` |
| 123 | Source string `json:"source"` |
| 124 | Parent string `json:"parent"` |
| 125 | Owner string `json:"owner"` |
| 126 | BlockedBy []string `json:"blocked_by"` |
| 127 | Changes []string `json:"changes"` |
| 128 | Target string `json:"target"` |
| 129 | GraduatedInto string `json:"graduated_into"` |
| 130 | CommentCount int `json:"comment_count"` |
| 131 | State string `json:"state"` |
| 132 | Ready bool `json:"ready"` |
| 133 | WaitingOn []string `json:"waiting_on"` |
| 134 | ClearedByEnding []string `json:"cleared_by_ending"` |
| 135 | Dangling []string `json:"dangling"` |
| 136 | Unresolved []string `json:"unresolved"` |
| 137 | Children []string `json:"children"` |
| 138 | Blocks []string `json:"blocks"` |
| 139 | Progress *Progress `json:"progress"` |
| 140 | Claim *Claim `json:"claim"` |
| 141 | Edges []Edge `json:"edges"` |
| 142 | } |
| 143 | detailDoc struct { |
| 144 | Schema string `json:"schema"` |
| 145 | Project string `json:"project"` |
| 146 | Tip string `json:"tip"` |
| 147 | Knot struct { |
| 148 | ID string `json:"id"` |
| 149 | Body *string `json:"body"` |
| 150 | Comments []Comment `json:"comments"` |
| 151 | } `json:"knot"` |
| 152 | } |
| 153 | pendingDoc struct { |
| 154 | Schema string `json:"schema"` |
| 155 | Repos []pendingRepo `json:"repos"` |
| 156 | } |
| 157 | pendingRepo struct { |
| 158 | Repo string `json:"repo"` |
| 159 | Error *string `json:"error"` |
| 160 | Changes []pendingChange `json:"changes"` |
| 161 | } |
| 162 | pendingChange struct { |
| 163 | ChangeID string `json:"change_id"` |
| 164 | Handle string `json:"handle"` |
| 165 | Target string `json:"target"` |
| 166 | Owner string `json:"owner"` |
| 167 | State string `json:"state"` |
| 168 | Active int `json:"active"` |
| 169 | Versions []pendingVersion `json:"versions"` |
| 170 | Includes []string `json:"includes"` |
| 171 | Knots []KnotRef `json:"knots"` |
| 172 | } |
| 173 | pendingVersion struct { |
| 174 | N int `json:"n"` |
| 175 | Head string `json:"head"` |
| 176 | Title string `json:"title"` |
| 177 | Uploader string `json:"uploader"` |
| 178 | Landing string `json:"landing"` |
| 179 | Request *struct { |
| 180 | Commit string `json:"commit"` |
| 181 | } `json:"request"` |
| 182 | Outcome *Outcome `json:"outcome"` |
| 183 | } |
| 184 | ) |
| 185 | |
| 186 | // detailKey names a knot's detail: IDs are unique within a project, not |
| 187 | // across projects. |
| 188 | type detailKey struct{ project, id string } |
| 189 | |
| 190 | // Build projects the valley's JSON onto the export. It is a pure function |
| 191 | // of its inputs. It exports each project Identities names, which is every |
| 192 | // project on this edge, and everything of it a reader of the project may |
| 193 | // see: every knot, with its body and comments, and the changes ready for |
| 194 | // approval, each with its active version alone. It leaves out what is no |
| 195 | // reader's: the plan's operator sections, drafts, branches, older |
| 196 | // versions, repository URLs, error text and the file each knot is stored |
| 197 | // in. Every principal name passes through the display mapping. |
| 198 | // |
| 199 | // The export is one for every reader of the edge, and says nothing of who |
| 200 | // may read which project. So its projection is the storage boundary's: a |
| 201 | // reference to a project that is not exported keeps only the reference, |
| 202 | // and a reference to an exported project keeps what the plan says of it. |
| 203 | // The server takes out, for each reader, what the references say of the |
| 204 | // exported projects that reader may not read. |
| 205 | // |
| 206 | // An exported project whose plan or pending listing could not be read, |
| 207 | // whose knot lacks its detail, or whose detail was read at another graph |
| 208 | // tip fails the projection, so a partial export is never published. The |
| 209 | // last of these is ErrInconsistent. |
| 210 | func Build(in Inputs) (*Export, error) { |
| 211 | if err := in.Principals.check(); err != nil { |
| 212 | return nil, err |
| 213 | } |
| 214 | if err := CheckNames(context.Background(), in.Groups, Sorted(in.Placements)); err != nil { |
| 215 | return nil, err |
| 216 | } |
| 217 | var plan planDoc |
| 218 | if err := decodeInput("the plan", in.Plan, planSchema, &plan, &plan.Schema); err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | var pending pendingDoc |
| 222 | if err := decodeInput("the pending listing", in.Pending, pendingSchema, &pending, &pending.Schema); err != nil { |
| 223 | return nil, err |
| 224 | } |
| 225 | details := map[detailKey]detailDoc{} |
| 226 | for _, raw := range in.Details { |
| 227 | var d detailDoc |
| 228 | if err := decodeInput("a knot's detail", raw, detailSchema, &d, &d.Schema); err != nil { |
| 229 | return nil, err |
| 230 | } |
| 231 | key := detailKey{d.Project, d.Knot.ID} |
| 232 | if _, twice := details[key]; twice { |
| 233 | return nil, fmt.Errorf("the details hold knot %s:%s twice", d.Project, d.Knot.ID) |
| 234 | } |
| 235 | details[key] = d |
| 236 | } |
| 237 | |
| 238 | exported := map[string]bool{} |
| 239 | for name, id := range in.Identities { |
| 240 | if !ValidIdentity(id) { |
| 241 | return nil, fmt.Errorf("project %s has identity %q, which is not 32 lowercase hex digits", name, id) |
| 242 | } |
| 243 | exported[name] = true |
| 244 | } |
| 245 | planned := map[string]planProject{} |
| 246 | for _, p := range plan.Projects { |
| 247 | planned[p.Project] = p |
| 248 | } |
| 249 | repos := map[string]pendingRepo{} |
| 250 | for _, r := range pending.Repos { |
| 251 | repos[r.Repo] = r |
| 252 | } |
| 253 | |
| 254 | e := &Export{Schema: Schema, Collected: plan.Now, Projects: []Project{}} |
| 255 | var names []string |
| 256 | for name := range exported { |
| 257 | names = append(names, name) |
| 258 | } |
| 259 | sort.Strings(names) |
| 260 | for _, name := range names { |
| 261 | p, ok := planned[name] |
| 262 | if !ok { |
| 263 | return nil, fmt.Errorf("exported project %s is not in the plan", name) |
| 264 | } |
| 265 | if p.Error != nil { |
| 266 | return nil, fmt.Errorf("the plan could not read exported project %s", name) |
| 267 | } |
| 268 | r, ok := repos[name] |
| 269 | if !ok { |
| 270 | return nil, fmt.Errorf("exported project %s is not in the pending listing", name) |
| 271 | } |
| 272 | if r.Error != nil { |
| 273 | return nil, fmt.Errorf("the pending listing could not read exported project %s", name) |
| 274 | } |
| 275 | out, err := project(name, p, r, details, exported, in) |
| 276 | if err != nil { |
| 277 | return nil, err |
| 278 | } |
| 279 | e.Projects = append(e.Projects, out) |
| 280 | } |
| 281 | groups, err := exportedGroups(e.Projects, in.Groups) |
| 282 | if err != nil { |
| 283 | return nil, err |
| 284 | } |
| 285 | e.Groups = groups |
| 286 | if err := e.Validate(); err != nil { |
| 287 | return nil, err |
| 288 | } |
| 289 | return e, nil |
| 290 | } |
| 291 | |
| 292 | func project(name string, p planProject, r pendingRepo, details map[detailKey]detailDoc, |
| 293 | exported map[string]bool, in Inputs) (Project, error) { |
| 294 | shown := in.Principals.Show |
| 295 | tip := in.SourceTips[name] |
| 296 | if !ValidObjectID(tip) { |
| 297 | return Project{}, fmt.Errorf("exported project %s has no source tip, or one that is not an object ID", name) |
| 298 | } |
| 299 | placed := in.Placements[name] |
| 300 | if placed.Group == "" { |
| 301 | return Project{}, fmt.Errorf("exported project %s belongs to no group, so it has no web URL", name) |
| 302 | } |
| 303 | out := Project{ |
| 304 | Name: name, Identity: in.Identities[name], |
| 305 | Group: placed.Group, WebName: placed.Name, FormerNames: placed.FormerNames, Aliases: placed.Aliases, |
| 306 | GraphTip: p.Tip, SourceTip: tip, |
| 307 | Roots: nonNil(p.Roots), Knots: []Knot{}, OpenResiduals: []Residuals{}, Changes: []Change{}, |
| 308 | } |
| 309 | for _, g := range p.OpenResiduals { |
| 310 | out.OpenResiduals = append(out.OpenResiduals, Residuals{Parent: g.Parent, Bugs: nonNil(g.Bugs)}) |
| 311 | } |
| 312 | |
| 313 | // A reference to another project's knot is hidden when that project |
| 314 | // is not exported. Each list of the view a hidden reference appears in |
| 315 | // says something of the knot's status, so it is dropped from them all. |
| 316 | // A knot that waits on a hidden knot says only that it does. |
| 317 | hidden := func(ref string) bool { |
| 318 | project, _, qualified := strings.Cut(ref, ":") |
| 319 | return qualified && !exported[project] |
| 320 | } |
| 321 | var inProgress []string |
| 322 | inFlight := map[string]bool{} |
| 323 | for _, id := range p.InFlight { |
| 324 | inFlight[id] = true |
| 325 | } |
| 326 | for _, k := range p.Knots { |
| 327 | d, ok := details[detailKey{name, k.ID}] |
| 328 | if !ok { |
| 329 | return Project{}, fmt.Errorf("knot %s:%s has no detail", name, k.ID) |
| 330 | } |
| 331 | if d.Tip != p.Tip { |
| 332 | return Project{}, fmt.Errorf("%w: the plan read %s at %s, and knot %s at %s", ErrInconsistent, name, p.Tip, k.ID, d.Tip) |
| 333 | } |
| 334 | if d.Knot.Body == nil { |
| 335 | return Project{}, fmt.Errorf("the detail of knot %s:%s holds no body", name, k.ID) |
| 336 | } |
| 337 | if len(d.Knot.Comments) != k.CommentCount { |
| 338 | return Project{}, fmt.Errorf("%w: the plan counts %d comments on %s:%s, and its detail holds %d", ErrInconsistent, k.CommentCount, name, k.ID, len(d.Knot.Comments)) |
| 339 | } |
| 340 | x := Knot{ |
| 341 | ID: k.ID, Type: k.Type, Status: k.Status, Active: k.Active, Title: k.Title, |
| 342 | Created: k.Created, CreatedBy: shown(k.CreatedBy), Source: k.Source, Parent: k.Parent, |
| 343 | Owner: shown(k.Owner), BlockedBy: nonNil(k.BlockedBy), Changes: nonNil(k.Changes), |
| 344 | Target: k.Target, GraduatedInto: k.GraduatedInto, |
| 345 | State: k.State, Ready: k.Ready, |
| 346 | WaitingOn: drop(k.WaitingOn, hidden), WaitingOnHidden: slices.ContainsFunc(k.WaitingOn, hidden), |
| 347 | ClearedByEnding: drop(k.ClearedByEnding, hidden), Dangling: drop(k.Dangling, hidden), |
| 348 | Unresolved: drop(k.Unresolved, hidden), Children: nonNil(k.Children), Blocks: nonNil(k.Blocks), |
| 349 | Progress: k.Progress, Edges: []Edge{}, Body: *d.Knot.Body, Comments: []Comment{}, |
| 350 | CommentCount: k.CommentCount, |
| 351 | } |
| 352 | if k.Claim != nil { |
| 353 | c := *k.Claim |
| 354 | c.By = shown(c.By) |
| 355 | x.Claim = &c |
| 356 | } |
| 357 | for _, edge := range k.Edges { |
| 358 | if !exported[edge.Project] { |
| 359 | edge = Edge{Field: edge.Field, Ref: edge.Ref, Project: edge.Project, ID: edge.ID, State: EdgeHidden} |
| 360 | } |
| 361 | x.Edges = append(x.Edges, edge) |
| 362 | } |
| 363 | for _, c := range d.Knot.Comments { |
| 364 | c.Author, c.EditedBy = shown(c.Author), shown(c.EditedBy) |
| 365 | x.Comments = append(x.Comments, c) |
| 366 | } |
| 367 | if k.Active && k.Status == "in-progress" && !inFlight[k.ID] { |
| 368 | inProgress = append(inProgress, k.ID) |
| 369 | } |
| 370 | out.Knots = append(out.Knots, x) |
| 371 | } |
| 372 | out.InFlight = append(nonNil(p.InFlight), inProgress...) |
| 373 | |
| 374 | // includes may name a draft, which the export does not carry. |
| 375 | listed := map[string]bool{} |
| 376 | for _, c := range r.Changes { |
| 377 | listed[c.ChangeID] = true |
| 378 | } |
| 379 | for _, c := range r.Changes { |
| 380 | if c.State != "ready" { |
| 381 | return Project{}, fmt.Errorf("the pending listing holds change %s of %s, which is %s, among the ready changes", c.ChangeID, name, c.State) |
| 382 | } |
| 383 | var active *pendingVersion |
| 384 | for i := range c.Versions { |
| 385 | if c.Versions[i].N == c.Active { |
| 386 | active = &c.Versions[i] |
| 387 | } |
| 388 | } |
| 389 | if active == nil { |
| 390 | return Project{}, fmt.Errorf("change %s of %s holds no version %d, its active one", c.ChangeID, name, c.Active) |
| 391 | } |
| 392 | v := Version{N: active.N, Head: active.Head, Title: active.Title, Uploader: shown(active.Uploader), |
| 393 | Landing: active.Landing, Outcome: active.Outcome} |
| 394 | if active.Request != nil { |
| 395 | v.Request = &Request{Commit: active.Request.Commit} |
| 396 | } |
| 397 | out.Changes = append(out.Changes, Change{ |
| 398 | ChangeID: c.ChangeID, Handle: c.Handle, Target: c.Target, Owner: shown(c.Owner), Version: v, |
| 399 | Includes: drop(c.Includes, func(id string) bool { return !listed[id] }), Knots: nonNilRefs(c.Knots), |
| 400 | }) |
| 401 | } |
| 402 | return out, nil |
| 403 | } |
| 404 | |
| 405 | // exportedGroups are the groups the exported projects belong to, in |
| 406 | // identifier order, as groups are: a group no exported project belongs to |
| 407 | // is not exported. |
| 408 | func exportedGroups(projects []Project, groups []Group) ([]Group, error) { |
| 409 | used := map[string]bool{} |
| 410 | for _, p := range projects { |
| 411 | used[p.Group] = true |
| 412 | } |
| 413 | var out []Group |
| 414 | for _, g := range groups { |
| 415 | if used[g.ID] { |
| 416 | out = append(out, g) |
| 417 | delete(used, g.ID) |
| 418 | } |
| 419 | } |
| 420 | for _, p := range projects { |
| 421 | if used[p.Group] { |
| 422 | return nil, fmt.Errorf("exported project %s belongs to the group %q, which is not declared", p.Name, p.Group) |
| 423 | } |
| 424 | } |
| 425 | return out, nil |
| 426 | } |
| 427 | |
| 428 | // decodeInput reads one of the valley's documents and checks its schema. |
| 429 | func decodeInput(what string, raw []byte, schema string, into any, got *string) error { |
| 430 | if err := json.Unmarshal(raw, into); err != nil { |
| 431 | return fmt.Errorf("%s: %w", what, err) |
| 432 | } |
| 433 | if *got != schema { |
| 434 | return fmt.Errorf("%s has schema %q, not %q", what, *got, schema) |
| 435 | } |
| 436 | return nil |
| 437 | } |
| 438 | |
| 439 | // drop is list without the entries hidden reports, never nil. |
| 440 | func drop(list []string, hidden func(string) bool) []string { |
| 441 | kept := []string{} |
| 442 | for _, s := range list { |
| 443 | if !hidden(s) { |
| 444 | kept = append(kept, s) |
| 445 | } |
| 446 | } |
| 447 | return kept |
| 448 | } |
| 449 | |
| 450 | func nonNil(list []string) []string { |
| 451 | if list == nil { |
| 452 | return []string{} |
| 453 | } |
| 454 | return list |
| 455 | } |
| 456 | |
| 457 | func nonNilRefs(list []KnotRef) []KnotRef { |
| 458 | if list == nil { |
| 459 | return []KnotRef{} |
| 460 | } |
| 461 | return list |
| 462 | } |