Files
marketplace/cli/internal/issue/index.go
T
naudachu 9480e48312 feat: add the kettle CLI, replacing the plugin's Python scripts
The plugin resolved its issue store from `__file__`, which put it inside a
versioned plugin cache: issues written from one project were invisible from the
next, and `origin: local` files — the only copy of that work by definition —
were stranded a version bump at a time. The walk that answers "which directory
is the project" was written three times over, and in a linked worktree the three
disagreed. Both are runtime failures rather than logic ones, so the fix is a
compiled binary: one walk, imported rather than re-derived, and a layering rule
the build graph enforces instead of a grep.

Seven packages, knowledge flowing one way. `project` answers which directory is
the project and depends on nothing. `issue` is the domain — format, taxonomy,
validation, checkboxes, dependency graph, the store, eviction — offline, with no
tracker in it. `wire` holds the protocol shapes. `gitea` is the transport,
`mapping` the bridge, `config` the credentials, `cmd` the command tree. Four
tests hold the boundaries, each failing on a real mistake rather than a naming
convention.

The marker moves to `.kettle/` and the login pin moves out of the harness's
settings file into `.kettle/config.yaml`, which pins a login by NAME; the tokens
live in one file per machine, mode 0600, outside every working tree. That
retires the PreToolUse guard hook entirely — the binary holds its own
credentials, so a command running under a login nobody chose is not expressible
rather than caught.

`kettle init` migrates an older `tmp/issues` or `.tea/issues` store in, as a
move: a store left behind at an old path is one somebody edits by accident
months later.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-11 19:05:39 +05:00

129 lines
3.4 KiB
Go

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
}