| 1 | // valley plan: the plan graph as a view, for the operator to read where |
| 2 | // things stand without asking anyone. |
| 3 | // |
| 4 | // It reads a project's graph at refs/the-valley/knots as the knot verbs |
| 5 | // do (knot.go): one ls-remote, one fetch and one batched read. Then it |
| 6 | // prints the graph as a tree. The roots are the outcomes with no parent, |
| 7 | // the milestones. Under each come the knots whose parent it is, indented, |
| 8 | // and theirs under them, at every depth. Within a level, knots are in the |
| 9 | // order they were created, then by title. Every knot shows a glyph for its |
| 10 | // state, its ID and its title. An active knot also shows its claim, with |
| 11 | // the claim's age, its owner, the blockers that still hold it or that |
| 12 | // ended without finishing, and where its work lands. A root shows its |
| 13 | // progress at the right margin: its descendants that are terminal and not |
| 14 | // abandoned, out of its descendants that are not abandoned, as a count and |
| 15 | // a bar. A root whose descendants are all terminal is one line. Active |
| 16 | // knots with no parent that are not outcomes are listed after the roots, |
| 17 | // under "no milestone". |
| 18 | // |
| 19 | // ◐ oc-1d4a2b7 Slice two lands 2/7 ██░░░░░░░░ |
| 20 | // ✓ bd-2b3c4d5 The hook reads too much |
| 21 | // ◐ oc-3f4e5d6 Draw the tree · claimed by run-7 2h ago · owner klaus-env |
| 22 | // ⊘ oc-8a7b6c5 Golden files · blocked by oc-3f4e5d6 |
| 23 | // |
| 24 | // Three sections follow the tree. "waiting on you" lists the active |
| 25 | // outcomes and bugs, and the proposed decisions, that the operator owns, |
| 26 | // then the changes ready for approval, as valley pending selects them |
| 27 | // (pending.go). The operator is the principal git config valley.principal |
| 28 | // names, or the one --owner names. "in flight" lists the claimed knots, |
| 29 | // the oldest claim first. "open residuals" lists the open bugs, grouped |
| 30 | // under their parent. |
| 31 | // |
| 32 | // Without a flag it shows the current repository's project, and --project |
| 33 | // shows another on the same host. --all shows every project the host |
| 34 | // publishes, as valley pending --all reads them (list.go), each under its |
| 35 | // name. A project with no graph is named on one line at the end, and one |
| 36 | // that cannot be read is reported in its own section. |
| 37 | // |
| 38 | // Lines fit the terminal as pending's do (terminal.go): a title is cut |
| 39 | // short with …, and an ID, a run or a principal never is. On a terminal, |
| 40 | // each glyph has a colour and each root's title is bold, unless NO_COLOR |
| 41 | // is set. |
| 42 | // |
| 43 | // With --json it prints valley-plan/v1, which docs/architecture.md |
| 44 | // documents: every knot with what the graph derives of it, and the three |
| 45 | // sections. |
| 46 | |
| 47 | package main |
| 48 | |
| 49 | import ( |
| 50 | "fmt" |
| 51 | "io" |
| 52 | "slices" |
| 53 | "sort" |
| 54 | "strings" |
| 55 | "time" |
| 56 | |
| 57 | "the-valley/knot" |
| 58 | ) |
| 59 | |
| 60 | const ( |
| 61 | planUsage = "usage: valley plan [--project P | --all] [--owner P] [--json]" |
| 62 | planSchema = "valley-plan/v1" |
| 63 | // planStart is the command that starts a graph. |
| 64 | planStart = `valley knot new outcome "<title>"` |
| 65 | ) |
| 66 | |
| 67 | // planArgs are plan's arguments. |
| 68 | type planArgs struct { |
| 69 | project, owner string |
| 70 | all, json bool |
| 71 | } |
| 72 | |
| 73 | func parsePlanArgs(args []string) (planArgs, error) { |
| 74 | var a planArgs |
| 75 | for i := 0; i < len(args); i++ { |
| 76 | flag, value, inline := strings.Cut(args[i], "=") |
| 77 | switch flag { |
| 78 | case "--json", "--all": |
| 79 | if inline { |
| 80 | return a, failure(flag + " takes no value") |
| 81 | } |
| 82 | if flag == "--json" { |
| 83 | a.json = true |
| 84 | } else { |
| 85 | a.all = true |
| 86 | } |
| 87 | case "--project", "--owner": |
| 88 | if !inline { |
| 89 | if i+1 >= len(args) { |
| 90 | return a, failure(flag + " needs a value") |
| 91 | } |
| 92 | i++ |
| 93 | value = args[i] |
| 94 | } |
| 95 | if !knot.IsName(value) { |
| 96 | return a, failure(fmt.Sprintf("%s takes a name, and %q is not one", flag, value)) |
| 97 | } |
| 98 | if flag == "--project" { |
| 99 | a.project = value |
| 100 | } else { |
| 101 | a.owner = value |
| 102 | } |
| 103 | default: |
| 104 | return a, failure(fmt.Sprintf("plan does not take %s\n%s", args[i], planUsage)) |
| 105 | } |
| 106 | } |
| 107 | if a.all && a.project != "" { |
| 108 | return a, failure("plan takes --project or --all, not both") |
| 109 | } |
| 110 | return a, nil |
| 111 | } |
| 112 | |
| 113 | // planProject is one project as plan reads it: where its graph is, the |
| 114 | // graph as read, and its changes ready for approval. |
| 115 | type planProject struct { |
| 116 | remote |
| 117 | url string |
| 118 | state knotState |
| 119 | err error // why the graph could not be read |
| 120 | view *planView |
| 121 | changes []planChange // nil when they were not read |
| 122 | changesErr error |
| 123 | } |
| 124 | |
| 125 | // planChange is a change ready for approval, as valley pending selects |
| 126 | // them: ready, with an active version that has not landed. |
| 127 | type planChange struct { |
| 128 | Repo string `json:"repo"` |
| 129 | Handle string `json:"handle"` |
| 130 | ChangeID string `json:"change_id"` |
| 131 | Version int `json:"version"` |
| 132 | Title string `json:"title"` |
| 133 | Head string `json:"head"` |
| 134 | } |
| 135 | |
| 136 | func runPlan(g git, args []string, stdout, stderr io.Writer) error { |
| 137 | a, err := parsePlanArgs(args) |
| 138 | if err != nil { |
| 139 | return err |
| 140 | } |
| 141 | if !g.succeeds(true, "rev-parse", "--git-dir") { |
| 142 | return failure("not a git repository") |
| 143 | } |
| 144 | now := clock() |
| 145 | operator := a.owner |
| 146 | if operator == "" { |
| 147 | if p, err := g.output(true, "config", "valley.principal"); err == nil && knot.IsName(p) { |
| 148 | operator = p |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | var projects []*planProject |
| 153 | if a.all { |
| 154 | list, _, err := sources(g, listing{all: true}, stderr) |
| 155 | if err != nil { |
| 156 | return err |
| 157 | } |
| 158 | for _, s := range list { |
| 159 | if s.g.dir != "" { |
| 160 | // Another project's git speaks only through its error, |
| 161 | // which its section reports. |
| 162 | s.g.stderr = io.Discard |
| 163 | } |
| 164 | projects = append(projects, &planProject{remote: remote{g: s.g, name: s.name}, url: s.url, err: s.err}) |
| 165 | } |
| 166 | } else { |
| 167 | r, err := knotRemote(g, a.project) |
| 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | url, _ := r.g.output(true, "remote", "get-url", "origin") |
| 172 | projects = []*planProject{{remote: r, url: url}} |
| 173 | } |
| 174 | |
| 175 | // Each graph is read once, and a reference to another project's knot |
| 176 | // is resolved against the graphs read here before any other is read. |
| 177 | quiet := g |
| 178 | quiet.stderr = io.Discard |
| 179 | foreign := &foreignGraphs{g: quiet, graphs: map[string]*knot.Graph{}, failed: map[string]error{}} |
| 180 | for _, p := range projects { |
| 181 | if p.err == nil { |
| 182 | p.state, p.err = p.read() |
| 183 | } |
| 184 | if p.err != nil && !a.all { |
| 185 | return p.err |
| 186 | } |
| 187 | if p.err == nil { |
| 188 | foreign.graphs[p.name] = p.state.graph |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | var sourced []source |
| 193 | for _, p := range projects { |
| 194 | if p.err == nil && p.state.tip != "" { |
| 195 | p.view = newPlanView(p.remote, p.state, now, foreign.resolve) |
| 196 | sourced = append(sourced, source{name: p.name, url: p.url, g: p.g}) |
| 197 | } |
| 198 | } |
| 199 | repos := readSources(sourced, verifierFromEnv()) |
| 200 | h := handles(repos) |
| 201 | for _, l := range repos { |
| 202 | for _, p := range projects { |
| 203 | if p.name == l.name { |
| 204 | p.changes, p.changesErr = readyForApproval(l, h) |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | warnUnverified(repos, stderr) |
| 209 | |
| 210 | if a.json { |
| 211 | return writeKnotJSON(stdout, planDocument(projects, now, operator)) |
| 212 | } |
| 213 | t := terminalOf(stdout) |
| 214 | _, err = io.WriteString(stdout, t.planText(projects, operator, a.all || a.project != "", a.project)) |
| 215 | return err |
| 216 | } |
| 217 | |
| 218 | // readyForApproval are a repository's changes that valley pending lists |
| 219 | // as ready: ready, with an active version that has not landed. |
| 220 | func readyForApproval(l listed, h handleMap) ([]planChange, error) { |
| 221 | view := l.repoView(h, pendingChange, unlanded) |
| 222 | if view.Error != nil { |
| 223 | return nil, failure(*view.Error) |
| 224 | } |
| 225 | list := []planChange{} |
| 226 | for _, c := range view.Changes { |
| 227 | v := c.Versions[c.Active-1] |
| 228 | list = append(list, planChange{Repo: l.name, Handle: c.Handle, ChangeID: c.ChangeID, Version: v.N, |
| 229 | Title: v.Title, Head: v.Head}) |
| 230 | } |
| 231 | return list, nil |
| 232 | } |
| 233 | |
| 234 | // foreignGraphs resolves references to other projects' knots, reading |
| 235 | // each project's graph at most once. |
| 236 | type foreignGraphs struct { |
| 237 | g git |
| 238 | graphs map[string]*knot.Graph |
| 239 | failed map[string]error |
| 240 | } |
| 241 | |
| 242 | func (f *foreignGraphs) resolve(ref string) (*knot.Knot, bool, error) { |
| 243 | project, id, _ := strings.Cut(ref, ":") |
| 244 | if err := f.failed[project]; err != nil { |
| 245 | return nil, false, err |
| 246 | } |
| 247 | graph := f.graphs[project] |
| 248 | if graph == nil { |
| 249 | r, err := knotRemote(f.g, project) |
| 250 | var s knotState |
| 251 | if err == nil { |
| 252 | s, err = r.read() |
| 253 | } |
| 254 | if err != nil { |
| 255 | f.failed[project] = err |
| 256 | return nil, false, err |
| 257 | } |
| 258 | graph = s.graph |
| 259 | f.graphs[project] = graph |
| 260 | } |
| 261 | k, ok := graph.Knots[id] |
| 262 | return k, ok, nil |
| 263 | } |
| 264 | |
| 265 | // planEndings are the terminal statuses that end a knot without finishing |
| 266 | // it, as the knot module's view reads them (knot/derive.go). |
| 267 | var planEndings = []string{"abandoned", "discarded", "superseded"} |
| 268 | |
| 269 | // planView is one project's graph as plan shows it. |
| 270 | type planView struct { |
| 271 | graph *knot.Graph |
| 272 | knots map[string]*planKnot |
| 273 | children map[string][]string // each knot's children, in tree order |
| 274 | order []string // every knot, in the order the tree lists them |
| 275 | rank map[string]int // each knot's place in order |
| 276 | roots []string // the outcomes with no parent |
| 277 | loose []string // the active knots with no parent that are no outcome |
| 278 | } |
| 279 | |
| 280 | // planKnot is one knot as valley-plan/v1 writes it: as valley knot list |
| 281 | // writes it, with its children in tree order and its progress counted |
| 282 | // over every descendant, and with its state and its edges to other |
| 283 | // projects' knots. |
| 284 | type planKnot struct { |
| 285 | *jsonKnot |
| 286 | State string `json:"state"` |
| 287 | Edges []planEdge `json:"edges"` |
| 288 | } |
| 289 | |
| 290 | // planEdge is a reference to another project's knot, from blocked_by or |
| 291 | // target. It is resolved when that project's graph holds the knot, and |
| 292 | // dangling when it does not (why: missing) or could not be read (why: |
| 293 | // unreadable). |
| 294 | type planEdge struct { |
| 295 | Field string `json:"field"` |
| 296 | Ref string `json:"ref"` |
| 297 | Project string `json:"project"` |
| 298 | ID string `json:"id"` |
| 299 | State string `json:"state"` |
| 300 | Why *string `json:"why"` |
| 301 | Status *string `json:"status"` |
| 302 | Title *string `json:"title"` |
| 303 | } |
| 304 | |
| 305 | func newPlanView(r remote, s knotState, now time.Time, foreign knot.Foreign) *planView { |
| 306 | g := s.graph |
| 307 | v := &planView{graph: g, knots: map[string]*planKnot{}, children: map[string][]string{}, rank: map[string]int{}} |
| 308 | inOrder := func(ids []string) { |
| 309 | sort.Slice(ids, func(i, j int) bool { return before(g.Knots[ids[i]], g.Knots[ids[j]]) }) |
| 310 | } |
| 311 | var unparented []string |
| 312 | for _, id := range g.IDs() { |
| 313 | switch k := g.Knots[id]; { |
| 314 | case k.Parent != "": |
| 315 | v.children[k.Parent] = append(v.children[k.Parent], id) |
| 316 | case k.Type == "outcome": |
| 317 | v.roots = append(v.roots, id) |
| 318 | default: |
| 319 | unparented = append(unparented, id) |
| 320 | } |
| 321 | } |
| 322 | for _, list := range v.children { |
| 323 | inOrder(list) |
| 324 | } |
| 325 | inOrder(v.roots) |
| 326 | inOrder(unparented) |
| 327 | var walk func(id string) |
| 328 | walk = func(id string) { |
| 329 | v.rank[id] = len(v.order) |
| 330 | v.order = append(v.order, id) |
| 331 | for _, c := range v.children[id] { |
| 332 | walk(c) |
| 333 | } |
| 334 | } |
| 335 | for _, id := range v.roots { |
| 336 | walk(id) |
| 337 | } |
| 338 | for _, id := range unparented { |
| 339 | walk(id) |
| 340 | if g.Knots[id].Active() { |
| 341 | v.loose = append(v.loose, id) |
| 342 | } |
| 343 | } |
| 344 | progress := v.progress() |
| 345 | for _, id := range v.order { |
| 346 | k := g.Knots[id] |
| 347 | j := r.jsonKnot(s, k, false, foreign) |
| 348 | j.Children = append([]string{}, v.children[id]...) |
| 349 | j.Progress = nil |
| 350 | if len(v.children[id]) > 0 { |
| 351 | p := progress[id] |
| 352 | j.Progress = &p |
| 353 | } |
| 354 | v.knots[id] = &planKnot{jsonKnot: j, State: stateOf(k, j.View), Edges: edgesOf(k, foreign)} |
| 355 | } |
| 356 | return v |
| 357 | } |
| 358 | |
| 359 | // before orders the knots of one level: by the date each was created, |
| 360 | // then by title, then by ID, so two knots of one title keep one order. |
| 361 | func before(a, b *knot.Knot) bool { |
| 362 | if a.Created != b.Created { |
| 363 | return a.Created < b.Created |
| 364 | } |
| 365 | if a.Title != b.Title { |
| 366 | return a.Title < b.Title |
| 367 | } |
| 368 | return a.ID < b.ID |
| 369 | } |
| 370 | |
| 371 | // progress is each knot's progress over every descendant: those that are |
| 372 | // terminal and not abandoned, out of those that are not abandoned. A |
| 373 | // descendant under an abandoned knot counts as any other does. |
| 374 | func (v *planView) progress() map[string]knot.Progress { |
| 375 | all := map[string]knot.Progress{} |
| 376 | // order lists a knot before its descendants, so walking it backwards |
| 377 | // counts every child before its parent. |
| 378 | for i := len(v.order) - 1; i >= 0; i-- { |
| 379 | id := v.order[i] |
| 380 | var p knot.Progress |
| 381 | for _, c := range v.children[id] { |
| 382 | if k := v.graph.Knots[c]; k.Status != "abandoned" { |
| 383 | p.Of++ |
| 384 | if !k.Active() { |
| 385 | p.Done++ |
| 386 | } |
| 387 | } |
| 388 | p.Done += all[c].Done |
| 389 | p.Of += all[c].Of |
| 390 | } |
| 391 | all[id] = p |
| 392 | } |
| 393 | return all |
| 394 | } |
| 395 | |
| 396 | // stateOf is the state a knot's glyph shows. A terminal knot is done, or |
| 397 | // abandoned when it ended without finishing. An active one is blocked |
| 398 | // while a blocker holds it, in progress while it is claimed or its status |
| 399 | // says so, ready when it is an outcome or a bug the graph reads as ready, |
| 400 | // and otherwise active, as an open idea or a proposed decision is. |
| 401 | func stateOf(k *knot.Knot, view knot.View) string { |
| 402 | switch { |
| 403 | case !k.Active() && slices.Contains(planEndings, k.Status): |
| 404 | return "abandoned" |
| 405 | case !k.Active(): |
| 406 | return "done" |
| 407 | case len(view.WaitingOn) > 0: |
| 408 | return "blocked" |
| 409 | case k.ClaimedBy != "" || k.Status == "in-progress": |
| 410 | return "in-progress" |
| 411 | case view.Ready && (k.Type == "outcome" || k.Type == "bug"): |
| 412 | return "ready" |
| 413 | } |
| 414 | return "active" |
| 415 | } |
| 416 | |
| 417 | // edgesOf are a knot's references to other projects' knots, resolved. |
| 418 | func edgesOf(k *knot.Knot, foreign knot.Foreign) []planEdge { |
| 419 | edges := []planEdge{} |
| 420 | add := func(field, ref string) { |
| 421 | project, id, _ := strings.Cut(ref, ":") |
| 422 | e := planEdge{Field: field, Ref: ref, Project: project, ID: id, State: "resolved"} |
| 423 | var found *knot.Knot |
| 424 | ok, err := false, error(failure("no resolver")) |
| 425 | if foreign != nil { |
| 426 | found, ok, err = foreign(ref) |
| 427 | } |
| 428 | switch { |
| 429 | case err != nil: |
| 430 | e.State, e.Why = "dangling", optional("unreadable") |
| 431 | case !ok: |
| 432 | e.State, e.Why = "dangling", optional("missing") |
| 433 | default: |
| 434 | e.Status, e.Title = optional(found.Status), optional(found.Title) |
| 435 | } |
| 436 | edges = append(edges, e) |
| 437 | } |
| 438 | for _, ref := range k.BlockedBy { |
| 439 | if knot.IsQualifiedID(ref) { |
| 440 | add("blocked_by", ref) |
| 441 | } |
| 442 | } |
| 443 | if knot.IsQualifiedID(k.Target) { |
| 444 | add("target", k.Target) |
| 445 | } |
| 446 | return edges |
| 447 | } |
| 448 | |
| 449 | // collapsed reports whether a root's descendants are all terminal, so it |
| 450 | // is shown as one line. |
| 451 | func (v *planView) collapsed(id string) bool { |
| 452 | for _, c := range v.children[id] { |
| 453 | if v.graph.Knots[c].Active() || !v.collapsed(c) { |
| 454 | return false |
| 455 | } |
| 456 | } |
| 457 | return true |
| 458 | } |
| 459 | |
| 460 | // waiting are the knots that wait on the operator, in tree order: the |
| 461 | // active outcomes and bugs they own, and the proposed decisions they own. |
| 462 | func (v *planView) waiting(operator string) []string { |
| 463 | ids := []string{} |
| 464 | for _, id := range v.order { |
| 465 | k := v.graph.Knots[id] |
| 466 | if k.Owner != operator || !k.Active() { |
| 467 | continue |
| 468 | } |
| 469 | if k.Type == "outcome" || k.Type == "bug" || k.Type == "decision" && k.Status == "proposed" { |
| 470 | ids = append(ids, id) |
| 471 | } |
| 472 | } |
| 473 | return ids |
| 474 | } |
| 475 | |
| 476 | // inFlight are the active knots that are claimed, the oldest claim first. |
| 477 | // claimed_at is a UTC time of one fixed form, so its text sorts as its |
| 478 | // time does. |
| 479 | func (v *planView) inFlight() []string { |
| 480 | ids := []string{} |
| 481 | for _, id := range v.order { |
| 482 | if k := v.graph.Knots[id]; k.ClaimedBy != "" && k.Active() { |
| 483 | ids = append(ids, id) |
| 484 | } |
| 485 | } |
| 486 | sort.SliceStable(ids, func(i, j int) bool { |
| 487 | return v.graph.Knots[ids[i]].ClaimedAt < v.graph.Knots[ids[j]].ClaimedAt |
| 488 | }) |
| 489 | return ids |
| 490 | } |
| 491 | |
| 492 | // residualGroup is the open bugs under one parent, or under none. |
| 493 | type residualGroup struct { |
| 494 | parent string |
| 495 | bugs []string |
| 496 | } |
| 497 | |
| 498 | // residuals are the open bugs: first those with no parent, then those |
| 499 | // with one, grouped under it, the groups in tree order. |
| 500 | func (v *planView) residuals() []residualGroup { |
| 501 | loose := residualGroup{} |
| 502 | var groups []residualGroup |
| 503 | at := map[string]int{} |
| 504 | for _, id := range v.order { |
| 505 | k := v.graph.Knots[id] |
| 506 | switch { |
| 507 | case k.Type != "bug" || !k.Active(): |
| 508 | case k.Parent == "": |
| 509 | loose.bugs = append(loose.bugs, id) |
| 510 | default: |
| 511 | i, ok := at[k.Parent] |
| 512 | if !ok { |
| 513 | i = len(groups) |
| 514 | at[k.Parent] = i |
| 515 | groups = append(groups, residualGroup{parent: k.Parent}) |
| 516 | } |
| 517 | groups[i].bugs = append(groups[i].bugs, id) |
| 518 | } |
| 519 | } |
| 520 | sort.SliceStable(groups, func(i, j int) bool { return v.rank[groups[i].parent] < v.rank[groups[j].parent] }) |
| 521 | if len(loose.bugs) > 0 { |
| 522 | groups = append([]residualGroup{loose}, groups...) |
| 523 | } |
| 524 | return groups |
| 525 | } |
| 526 | |
| 527 | // notes are what a line says of an active knot after its title: its |
| 528 | // claim, its owner, its blockers, and where its work lands. A blocker is |
| 529 | // listed while it holds the knot, and when it ended without finishing, |
| 530 | // marked with how it ended. A reference to another project's knot that |
| 531 | // cannot be read is marked dangling. |
| 532 | func (v *planView) notes(id string) []string { |
| 533 | k, pk := v.graph.Knots[id], v.knots[id] |
| 534 | if !k.Active() { |
| 535 | return nil |
| 536 | } |
| 537 | dangling := map[string]bool{} |
| 538 | for _, e := range pk.Edges { |
| 539 | if e.State == "dangling" { |
| 540 | dangling[e.Ref] = true |
| 541 | } |
| 542 | } |
| 543 | var notes []string |
| 544 | if c := pk.Claim; c != nil { |
| 545 | note := fmt.Sprintf("claimed by %s %s ago", visible(c.By), age(c.AgeSeconds)) |
| 546 | if c.Stale { |
| 547 | note += ", stale" |
| 548 | } |
| 549 | notes = append(notes, note) |
| 550 | } |
| 551 | if k.Owner != "" { |
| 552 | notes = append(notes, "owner "+visible(k.Owner)) |
| 553 | } |
| 554 | var blockers []string |
| 555 | for _, ref := range k.BlockedBy { |
| 556 | switch { |
| 557 | case dangling[ref]: |
| 558 | blockers = append(blockers, ref+" (dangling)") |
| 559 | case slices.Contains(pk.ClearedByEnding, ref): |
| 560 | blockers = append(blockers, ref+" ("+v.statusOf(ref, pk.Edges)+")") |
| 561 | case slices.Contains(pk.WaitingOn, ref): |
| 562 | blockers = append(blockers, ref) |
| 563 | } |
| 564 | } |
| 565 | if len(blockers) > 0 { |
| 566 | notes = append(notes, "blocked by "+strings.Join(blockers, ", ")) |
| 567 | } |
| 568 | if k.Target != "" { |
| 569 | note := "lands in " + visible(k.Target) |
| 570 | if dangling[k.Target] { |
| 571 | note += " (dangling)" |
| 572 | } |
| 573 | notes = append(notes, note) |
| 574 | } |
| 575 | return notes |
| 576 | } |
| 577 | |
| 578 | // statusOf is the status of the knot a blocker names, in this graph or, |
| 579 | // through the knot's edges, in another. |
| 580 | func (v *planView) statusOf(ref string, edges []planEdge) string { |
| 581 | if k, ok := v.graph.Knots[ref]; ok { |
| 582 | return k.Status |
| 583 | } |
| 584 | for _, e := range edges { |
| 585 | if e.Ref == ref && e.Status != nil { |
| 586 | return *e.Status |
| 587 | } |
| 588 | } |
| 589 | return "ended" |
| 590 | } |
| 591 | |
| 592 | // planGlyphs are the glyph of each state, in the order the legend lists |
| 593 | // them, with the SGR code of the colour a terminal shows it in. |
| 594 | var planGlyphs = []struct{ state, glyph, word, colour string }{ |
| 595 | {"ready", "○", "ready", "32"}, |
| 596 | {"in-progress", "◐", "in progress", "33"}, |
| 597 | {"blocked", "⊘", "blocked", "31"}, |
| 598 | {"active", "◇", "active", "36"}, |
| 599 | {"done", "✓", "done", "2"}, |
| 600 | {"abandoned", "✗", "abandoned", "2"}, |
| 601 | } |
| 602 | |
| 603 | func (t terminal) glyph(state string) (plain, shown string) { |
| 604 | for _, g := range planGlyphs { |
| 605 | if g.state == state { |
| 606 | return g.glyph, t.tint(g.colour, g.glyph) |
| 607 | } |
| 608 | } |
| 609 | return "?", "?" |
| 610 | } |
| 611 | |
| 612 | // barWidth is how many cells a root's progress bar has. |
| 613 | const barWidth = 10 |
| 614 | |
| 615 | // bar is a root's progress as a count and a bar of barWidth cells, a cell |
| 616 | // filled for each whole tenth done. |
| 617 | func bar(p knot.Progress) string { |
| 618 | filled := 0 |
| 619 | if p.Of > 0 { |
| 620 | filled = p.Done * barWidth / p.Of |
| 621 | } |
| 622 | return fmt.Sprintf("%d/%d %s%s", p.Done, p.Of, strings.Repeat("█", filled), strings.Repeat("░", barWidth-filled)) |
| 623 | } |
| 624 | |
| 625 | // minTitle is the fewest columns a title is cut to before a knot's notes |
| 626 | // move to a line of their own. |
| 627 | const minTitle = 24 |
| 628 | |
| 629 | // knotLine is a knot's line: indent, its glyph, its ID, its title, and its |
| 630 | // notes, with right, when given, at the right margin. The title is cut |
| 631 | // short so every note fits after it. When that would leave the title |
| 632 | // fewer than minTitle columns, it keeps the room it needs, and the notes |
| 633 | // run on after it and over lines of their own under the title, as many to |
| 634 | // a line as fit. A note breaks only after a comma in its list, so an ID, |
| 635 | // a run or a principal is never cut. |
| 636 | func (t terminal) knotLine(v *planView, id, indent, right string, strong bool) []string { |
| 637 | k := v.graph.Knots[id] |
| 638 | plain, shown := t.glyph(v.knots[id].State) |
| 639 | name := fmt.Sprintf("%-11s ", id) |
| 640 | lead := indent + plain + " " + name |
| 641 | notes := v.notes(id) |
| 642 | margin := 0 |
| 643 | if right != "" { |
| 644 | margin = columns(right) + 2 |
| 645 | } |
| 646 | room := t.width - columns(lead) - margin |
| 647 | title := visible(k.Title) |
| 648 | tail := "" |
| 649 | if len(notes) > 0 { |
| 650 | tail = " · " + strings.Join(notes, " · ") |
| 651 | } |
| 652 | var rest []string |
| 653 | if columns(title+tail) <= room || room-columns(tail) >= minTitle { |
| 654 | title = fit(title, room-columns(tail)) |
| 655 | } else { |
| 656 | title = fit(title, room) |
| 657 | // Each note is a run of pieces, broken after each comma. A piece |
| 658 | // that opens a note follows another after " · ", and one that |
| 659 | // continues a note follows it directly. |
| 660 | type piece struct { |
| 661 | text string |
| 662 | opens bool |
| 663 | } |
| 664 | var pieces []piece |
| 665 | for _, n := range notes { |
| 666 | for i, p := range strings.SplitAfter(n, ", ") { |
| 667 | pieces = append(pieces, piece{p, i == 0}) |
| 668 | } |
| 669 | } |
| 670 | join := func(line string, p piece) string { |
| 671 | if line == "" || !p.opens { |
| 672 | return line + p.text |
| 673 | } |
| 674 | return line + " · " + p.text |
| 675 | } |
| 676 | fill := func(line, lead string, width int) string { |
| 677 | for len(pieces) > 0 && columns(lead+strings.TrimRight(join(line, pieces[0]), " ")) <= width { |
| 678 | line, pieces = join(line, pieces[0]), pieces[1:] |
| 679 | } |
| 680 | return strings.TrimRight(line, " ") |
| 681 | } |
| 682 | tail = strings.TrimPrefix(fill(title, "", room), title) |
| 683 | under := strings.Repeat(" ", columns(lead)) |
| 684 | for len(pieces) > 0 { |
| 685 | first := pieces[0].text |
| 686 | pieces = pieces[1:] |
| 687 | rest = append(rest, under+fill(first, under, t.width)) |
| 688 | } |
| 689 | } |
| 690 | styled := title |
| 691 | if strong { |
| 692 | styled = t.strong(title) |
| 693 | } |
| 694 | line := indent + shown + " " + name + styled + tail |
| 695 | if right != "" { |
| 696 | gap := max(t.width-columns(lead+title+tail)-columns(right), 2) |
| 697 | line += strings.Repeat(" ", gap) + right |
| 698 | } |
| 699 | return append([]string{line}, rest...) |
| 700 | } |
| 701 | |
| 702 | // subtree is the lines of every knot under id, each level indented two |
| 703 | // columns more. |
| 704 | func (t terminal) subtree(v *planView, id, indent string) []string { |
| 705 | var lines []string |
| 706 | for _, c := range v.children[id] { |
| 707 | lines = append(lines, t.knotLine(v, c, indent, "", false)...) |
| 708 | lines = append(lines, t.subtree(v, c, indent+" ")...) |
| 709 | } |
| 710 | return lines |
| 711 | } |
| 712 | |
| 713 | // planText is the whole of plan's text: each project's section, the |
| 714 | // projects with no graph, and the legend. |
| 715 | func (t terminal) planText(projects []*planProject, operator string, headed bool, named string) string { |
| 716 | var b strings.Builder |
| 717 | var blank []string // the projects with no graph |
| 718 | shown := false |
| 719 | for _, p := range projects { |
| 720 | if p.err == nil && p.state.tip == "" { |
| 721 | blank = append(blank, p.name) |
| 722 | continue |
| 723 | } |
| 724 | if shown { |
| 725 | b.WriteString("\n") |
| 726 | } |
| 727 | shown = true |
| 728 | if headed { |
| 729 | fmt.Fprintf(&b, "%s\n%s\n", t.strong(p.name), strings.Repeat("─", min(columns(p.name), t.width))) |
| 730 | } |
| 731 | if p.err != nil { |
| 732 | fmt.Fprintf(&b, "its plan graph could not be read: %v\n", p.err) |
| 733 | continue |
| 734 | } |
| 735 | for _, line := range t.projectLines(p, operator) { |
| 736 | b.WriteString(line + "\n") |
| 737 | } |
| 738 | } |
| 739 | switch { |
| 740 | case !headed && len(blank) > 0: |
| 741 | fmt.Fprintf(&b, "%s has no plan yet. Start one with: %s\n", blank[0], planStart) |
| 742 | case named != "" && len(blank) > 0: |
| 743 | fmt.Fprintf(&b, "%s has no plan yet. Start one with: %s --project %s\n", blank[0], planStart, blank[0]) |
| 744 | case len(blank) > 0: |
| 745 | if shown { |
| 746 | b.WriteString("\n") |
| 747 | } |
| 748 | fmt.Fprintf(&b, "no plan yet: %s. Start one with: %s --project <name>\n", strings.Join(blank, ", "), planStart) |
| 749 | } |
| 750 | if shown { |
| 751 | var legend []string |
| 752 | for _, g := range planGlyphs { |
| 753 | legend = append(legend, t.tint(g.colour, g.glyph)+" "+g.word) |
| 754 | } |
| 755 | fmt.Fprintf(&b, "\n%s\n", strings.Join(legend, " ")) |
| 756 | } |
| 757 | return b.String() |
| 758 | } |
| 759 | |
| 760 | // projectLines are one project's tree and its three sections. |
| 761 | func (t terminal) projectLines(p *planProject, operator string) []string { |
| 762 | v := p.view |
| 763 | var lines []string |
| 764 | gap := func() { |
| 765 | if len(lines) > 0 { |
| 766 | lines = append(lines, "") |
| 767 | } |
| 768 | } |
| 769 | for _, id := range v.roots { |
| 770 | gap() |
| 771 | right := "" |
| 772 | if pk := v.knots[id]; pk.Progress != nil { |
| 773 | right = bar(*pk.Progress) |
| 774 | } else { |
| 775 | right = bar(knot.Progress{}) |
| 776 | } |
| 777 | lines = append(lines, t.knotLine(v, id, "", right, true)...) |
| 778 | if !v.collapsed(id) { |
| 779 | lines = append(lines, t.subtree(v, id, " ")...) |
| 780 | } |
| 781 | } |
| 782 | if len(v.loose) > 0 { |
| 783 | gap() |
| 784 | lines = append(lines, "no milestone") |
| 785 | for _, id := range v.loose { |
| 786 | lines = append(lines, t.knotLine(v, id, " ", "", false)...) |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | gap() |
| 791 | if operator == "" { |
| 792 | lines = append(lines, "waiting on you", |
| 793 | " set git config valley.principal <name>, or pass --owner <name>, to see the knots you own") |
| 794 | } else { |
| 795 | lines = append(lines, "waiting on you ("+operator+")") |
| 796 | waiting := v.waiting(operator) |
| 797 | for _, id := range waiting { |
| 798 | lines = append(lines, t.knotLine(v, id, " ", "", false)...) |
| 799 | } |
| 800 | if len(waiting) == 0 { |
| 801 | lines = append(lines, " no open knot is yours") |
| 802 | } |
| 803 | } |
| 804 | lines = append(lines, " ready for approval") |
| 805 | switch { |
| 806 | case p.changesErr != nil: |
| 807 | lines = append(lines, " its changes could not be read: "+p.changesErr.Error()) |
| 808 | case len(p.changes) == 0: |
| 809 | lines = append(lines, " no change is ready for approval") |
| 810 | } |
| 811 | for _, c := range p.changes { |
| 812 | lead := fmt.Sprintf(" %s v%d %s ", c.Handle, c.Version, c.Repo) |
| 813 | styled := fmt.Sprintf(" %s %s ", t.strong(fmt.Sprintf("%s v%d", c.Handle, c.Version)), c.Repo) |
| 814 | lines = append(lines, styled+t.fit(readable(c.Title), lead)) |
| 815 | } |
| 816 | |
| 817 | lines = append(lines, "", "in flight") |
| 818 | flying := v.inFlight() |
| 819 | for _, id := range flying { |
| 820 | lines = append(lines, t.knotLine(v, id, " ", "", false)...) |
| 821 | } |
| 822 | if len(flying) == 0 { |
| 823 | lines = append(lines, " nothing is claimed") |
| 824 | } |
| 825 | |
| 826 | lines = append(lines, "", "open residuals") |
| 827 | groups := v.residuals() |
| 828 | for _, group := range groups { |
| 829 | indent := " " |
| 830 | if group.parent != "" { |
| 831 | lead := fmt.Sprintf(" under %-11s ", group.parent) |
| 832 | lines = append(lines, lead+t.fit(visible(v.graph.Knots[group.parent].Title), lead)) |
| 833 | indent = " " |
| 834 | } |
| 835 | for _, id := range group.bugs { |
| 836 | lines = append(lines, t.knotLine(v, id, indent, "", false)...) |
| 837 | } |
| 838 | } |
| 839 | if len(groups) == 0 { |
| 840 | lines = append(lines, " no bug is open") |
| 841 | } |
| 842 | return lines |
| 843 | } |
| 844 | |
| 845 | // The valley-plan/v1 document. docs/architecture.md documents it, and a |
| 846 | // change to any field's name or meaning is a new schema version. |
| 847 | type jsonPlan struct { |
| 848 | Schema string `json:"schema"` |
| 849 | Now string `json:"now"` |
| 850 | Operator *string `json:"operator"` |
| 851 | Projects []jsonPlanProject `json:"projects"` |
| 852 | } |
| 853 | |
| 854 | type jsonPlanProject struct { |
| 855 | Project string `json:"project"` |
| 856 | Error *string `json:"error"` |
| 857 | Tip string `json:"tip"` |
| 858 | Roots []string `json:"roots"` |
| 859 | Knots []*planKnot `json:"knots"` |
| 860 | WaitingOnYou *jsonWaiting `json:"waiting_on_you"` |
| 861 | InFlight []string `json:"in_flight"` |
| 862 | OpenResiduals []jsonResiduals `json:"open_residuals"` |
| 863 | } |
| 864 | |
| 865 | type jsonWaiting struct { |
| 866 | Knots []string `json:"knots"` |
| 867 | Changes []planChange `json:"changes"` |
| 868 | ChangesError *string `json:"changes_error"` |
| 869 | } |
| 870 | |
| 871 | type jsonResiduals struct { |
| 872 | Parent *string `json:"parent"` |
| 873 | Bugs []string `json:"bugs"` |
| 874 | } |
| 875 | |
| 876 | func planDocument(projects []*planProject, now time.Time, operator string) jsonPlan { |
| 877 | doc := jsonPlan{Schema: planSchema, Now: now.Format(time.RFC3339), Operator: optional(operator), |
| 878 | Projects: []jsonPlanProject{}} |
| 879 | for _, p := range projects { |
| 880 | j := jsonPlanProject{Project: p.name, Tip: p.state.tip, Roots: []string{}, Knots: []*planKnot{}} |
| 881 | if p.err != nil { |
| 882 | msg := p.err.Error() |
| 883 | j.Error = &msg |
| 884 | } |
| 885 | if v := p.view; v != nil { |
| 886 | j.Roots = append(j.Roots, v.roots...) |
| 887 | for _, id := range v.order { |
| 888 | j.Knots = append(j.Knots, v.knots[id]) |
| 889 | } |
| 890 | w := &jsonWaiting{Changes: p.changes} |
| 891 | if operator != "" { |
| 892 | w.Knots = v.waiting(operator) |
| 893 | } |
| 894 | if p.changesErr != nil { |
| 895 | msg := p.changesErr.Error() |
| 896 | w.ChangesError = &msg |
| 897 | } |
| 898 | j.WaitingOnYou = w |
| 899 | j.InFlight = v.inFlight() |
| 900 | j.OpenResiduals = []jsonResiduals{} |
| 901 | for _, g := range v.residuals() { |
| 902 | j.OpenResiduals = append(j.OpenResiduals, jsonResiduals{Parent: optional(g.parent), Bugs: g.bugs}) |
| 903 | } |
| 904 | } |
| 905 | doc.Projects = append(doc.Projects, j) |
| 906 | } |
| 907 | return doc |
| 908 | } |