package issue import ( "fmt" "os" "path/filepath" "regexp" "sort" "strings" ) var treeFileRe = regexp.MustCompile(`^tree-.+\.md$`) const indexPreamble = "Every issue this project knows about. `origin: local` means it " + "exists nowhere else — a complete state, not a pending one. Any other value " + "names the tracker it also lives in; the handle is in the file. `progress` " + "counts the body's checkboxes, ticked over total, and is blank for an issue " + "that has none — read off the body at build time, stored nowhere. Rebuild " + "with `kettle index`; tick a box with `kettle ac`." // BuildIndex rewrites INDEX.md from what is on disk and returns its path and // the number of issues in it. // // An index of a store that is not there is not an empty index, it is a bad // path: failing beats writing INDEX.md into a directory nobody asked for. An // existing store with nothing in it is a legitimate thing to index and gets an // "_empty_" table. func BuildIndex(root string) (string, int, error) { if err := RequireStore(root); err != nil { return "", 0, err } issues, err := LoadAll(root) if err != nil { return "", 0, err } ids := make([]string, 0, len(issues)) for id := range issues { ids = append(ids, id) } sort.Strings(ids) out := []string{"# Issue store", "", indexPreamble, ""} if len(ids) > 0 { out = append(out, "| id | state | progress | type | labels | title | milestone | depends | origin |", "|---|---|---|---|---|---|---|---|---|") for _, id := range ids { i := issues[id] var rest []string for _, l := range i.Labels { if !strings.HasPrefix(l, "type/") { rest = append(rest, l) } } out = append(out, fmt.Sprintf("| [%s](%s.md) | %s | %s | %s | %s | %s | %s | %s | %s |", id, id, cell(i.State), progress(i.Body), cell(i.Type()), cellList(rest), cell(i.Title), cell(i.Milestone), cellList(i.Depends), cell(i.Origin))) } } else { out = append(out, "_empty_") } if trees := treeFiles(root); len(trees) > 0 { out = append(out, "", "## Dependency trees", "") for _, t := range trees { out = append(out, fmt.Sprintf("- [%s](%s)", t, t)) } } if cycles := FindCycles(Graph(issues)); len(cycles) > 0 { out = append(out, "", "## Dependency cycles", "") for _, c := range cycles { out = append(out, "- "+strings.Join(c, " -> ")) } } out = append(out, "") path := filepath.Join(root, "INDEX.md") if err := os.WriteFile(path, []byte(strings.Join(out, "\n")), 0o644); err != nil { return "", 0, err } return path, len(ids), nil } // progress is `3/7` for a body with checkboxes, "" for one without. // // Counted from the body every time the index is built and stored nowhere — the // boxes are the state, and a second copy of it in a metadata field would be // wrong by the next edit. func progress(body string) string { done, total := CheckboxProgress(body) if total == 0 { return "" } return fmt.Sprintf("%d/%d", done, total) } func cell(v string) string { v = strings.TrimSpace(v) if v == "" { return "—" } return strings.ReplaceAll(v, "|", `\|`) } func cellList(xs []string) string { if len(xs) == 0 { return "—" } return strings.Join(xs, ", ") } func treeFiles(root string) []string { entries, err := os.ReadDir(root) if err != nil { return nil } var out []string for _, e := range entries { if treeFileRe.MatchString(e.Name()) { out = append(out, e.Name()) } } sort.Strings(out) return out }