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>
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Repo is one repository, spelled the way a tracker spells it.
|
||||
type Repo struct {
|
||||
Owner string
|
||||
Name string
|
||||
}
|
||||
|
||||
func (r Repo) String() string {
|
||||
if r.Zero() {
|
||||
return ""
|
||||
}
|
||||
return r.Owner + "/" + r.Name
|
||||
}
|
||||
|
||||
// Zero reports whether this names no repository. Both halves are required:
|
||||
// half a name addresses nothing.
|
||||
func (r Repo) Zero() bool { return r.Owner == "" || r.Name == "" }
|
||||
|
||||
// ParseRepo reads owner/name.
|
||||
func ParseRepo(s string) (Repo, error) {
|
||||
owner, name, ok := strings.Cut(strings.TrimSpace(s), "/")
|
||||
if !ok || owner == "" || name == "" {
|
||||
return Repo{}, fmt.Errorf("repo %q is not owner/name", s)
|
||||
}
|
||||
return Repo{Owner: owner, Name: name}, nil
|
||||
}
|
||||
|
||||
// Key is a stable cross-repo handle for one issue: owner/repo#42.
|
||||
//
|
||||
// It is what the ledger is keyed by and what the `gitea:` metadata field holds,
|
||||
// so it has to survive being written to a file and read back — which is why it
|
||||
// is a repository and a number and not a bare number. A number is ambiguous the
|
||||
// moment a dependency lives in another repository, and dependencies are allowed
|
||||
// to.
|
||||
type Key struct {
|
||||
// Repo is zero when the caller named a number and nothing else, which is
|
||||
// the common case on a command line: "42" means "42 in this project's
|
||||
// repository", and which repository that is, is the client's business.
|
||||
Repo Repo
|
||||
Number int
|
||||
}
|
||||
|
||||
func (k Key) String() string {
|
||||
if k.Repo.Zero() {
|
||||
return "#" + strconv.Itoa(k.Number)
|
||||
}
|
||||
return fmt.Sprintf("%s#%d", k.Repo, k.Number)
|
||||
}
|
||||
|
||||
// In returns this key with r filled in when it names no repository of its own.
|
||||
func (k Key) In(r Repo) Key {
|
||||
if k.Repo.Zero() {
|
||||
k.Repo = r
|
||||
}
|
||||
return k
|
||||
}
|
||||
|
||||
// The four spellings, as patterns.
|
||||
//
|
||||
// Digits and only digits after the `#`, which is the test strconv.Atoi is too
|
||||
// generous to make on its own: it accepts a sign, and `owner/repo#-3` is not a
|
||||
// handle anybody ever wrote. Anything that is not a key has to be recognizable
|
||||
// as not a key — a hand-edited metadata line and a number are told apart here
|
||||
// and nowhere else.
|
||||
var (
|
||||
keyURL = regexp.MustCompile(`^https?://[^/]+/([^/]+)/([^/]+)/issues/(\d+)/?$`)
|
||||
keyQualified = regexp.MustCompile(`^([\w.-]+/[\w.-]+)#(\d+)$`)
|
||||
keyNumber = regexp.MustCompile(`^#?(\d+)$`)
|
||||
)
|
||||
|
||||
// ParseKey reads an issue key: 42, #42, owner/repo#42, or the issue's URL.
|
||||
//
|
||||
// All four spellings because all four are what somebody has in hand — a number
|
||||
// from a receipt, a `#42` copied out of a body, a qualified key out of the
|
||||
// ledger, a URL pasted from a browser. Refusing three of them buys nothing.
|
||||
func ParseKey(s string) (Key, error) {
|
||||
s = strings.TrimSpace(s)
|
||||
if m := keyURL.FindStringSubmatch(s); m != nil {
|
||||
n, _ := strconv.Atoi(m[3])
|
||||
return Key{Repo: Repo{Owner: m[1], Name: m[2]}, Number: n}, nil
|
||||
}
|
||||
if m := keyQualified.FindStringSubmatch(s); m != nil {
|
||||
repo, err := ParseRepo(m[1])
|
||||
if err != nil {
|
||||
return Key{}, err
|
||||
}
|
||||
n, _ := strconv.Atoi(m[2])
|
||||
return Key{Repo: repo, Number: n}, nil
|
||||
}
|
||||
if m := keyNumber.FindStringSubmatch(s); m != nil {
|
||||
n, _ := strconv.Atoi(m[1])
|
||||
return Key{Number: n}, nil
|
||||
}
|
||||
return Key{}, fmt.Errorf("cannot parse issue key %q — want 42, #42, owner/repo#42, or an issue URL", s)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.noodles.cam/claude-skills/marketplace/cli/internal/wire"
|
||||
)
|
||||
|
||||
func TestParseKey(t *testing.T) {
|
||||
acme := wire.Repo{Owner: "acme", Name: "widgets"}
|
||||
for _, tc := range []struct {
|
||||
in string
|
||||
want wire.Key
|
||||
}{
|
||||
{"42", wire.Key{Number: 42}},
|
||||
{"#42", wire.Key{Number: 42}},
|
||||
{" acme/widgets#42 ", wire.Key{Repo: acme, Number: 42}},
|
||||
{"https://git.example.test/acme/widgets/issues/42", wire.Key{Repo: acme, Number: 42}},
|
||||
{"https://git.example.test/acme/widgets/issues/42/", wire.Key{Repo: acme, Number: 42}},
|
||||
} {
|
||||
got, err := wire.ParseKey(tc.in)
|
||||
if err != nil {
|
||||
t.Errorf("ParseKey(%q): %v", tc.in, err)
|
||||
continue
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("ParseKey(%q) = %v, want %v", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
|
||||
// What is not a key has to be refused as one. `o/r#-3` is the case a bare
|
||||
// strconv.Atoi accepts and nobody ever wrote: a key read back out of a
|
||||
// metadata line somebody hand-edited must come back as "not a key", never
|
||||
// as issue -3.
|
||||
for _, bad := range []string{"not an issue", "o/r#-3", "o/r#4x", "o/r#", "o/r", ""} {
|
||||
if got, err := wire.ParseKey(bad); err == nil {
|
||||
t.Errorf("ParseKey(%q) = %v, want a refusal", bad, got)
|
||||
}
|
||||
}
|
||||
|
||||
if got := (wire.Key{Repo: acme, Number: 42}).String(); got != "acme/widgets#42" {
|
||||
t.Errorf("a qualified key formatted as %q", got)
|
||||
}
|
||||
if got := (wire.Key{Number: 42}).In(acme).String(); got != "acme/widgets#42" {
|
||||
t.Errorf("an unqualified key filled in as %q", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package wire
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The protocol is shared by two layers that may not import each other, and it
|
||||
// can only be shared because it reaches for nothing itself: no domain, no
|
||||
// configuration, no path resolution, no third party. One import from any of
|
||||
// those would drag every user of this package into that layer — which is the
|
||||
// whole reason these shapes were lifted out of the transport rather than left
|
||||
// there for the bridge to reimplement.
|
||||
//
|
||||
// The dependency walk, so a helper pulled in three packages deep is caught as
|
||||
// the same violation as one written at the top of a file.
|
||||
func TestWireDependsOnNothing(t *testing.T) {
|
||||
out, err := exec.Command("go", "list", "-deps", ".").Output()
|
||||
if err != nil {
|
||||
t.Fatalf("go list: %v", err)
|
||||
}
|
||||
for _, dep := range strings.Fields(string(out)) {
|
||||
if dep == "git.noodles.cam/claude-skills/marketplace/cli/internal/wire" {
|
||||
continue
|
||||
}
|
||||
// A standard-library import path has no dot in its first element,
|
||||
// because it has no domain name in front of it.
|
||||
first, _, _ := strings.Cut(dep, "/")
|
||||
if strings.Contains(first, ".") {
|
||||
t.Errorf("the protocol imports %s — these are shapes and identifiers, and nothing else belongs here", dep)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The other half: net/http and os are standard library, so "no third-party
|
||||
// imports" would not catch a transport or a file read written by hand here.
|
||||
// Name them.
|
||||
//
|
||||
// DIRECT imports, not the dependency walk — fmt reaches os on its own, and the
|
||||
// question this asks is what THIS package reaches for.
|
||||
func TestWireReachesNeitherTheNetworkNorTheDisk(t *testing.T) {
|
||||
forbidden := map[string]string{
|
||||
"net/http": "an HTTP call belongs in the transport",
|
||||
"net": "an HTTP call belongs in the transport",
|
||||
"os": "a shape reads no file and no environment",
|
||||
"os/exec": "nothing here shells out",
|
||||
"io": "nothing here is a stream",
|
||||
"time": "a timestamp crosses as the string the tracker sent",
|
||||
}
|
||||
|
||||
out, err := exec.Command("go", "list", "-f", `{{join .Imports "\n"}}`, ".").Output()
|
||||
if err != nil {
|
||||
t.Fatalf("go list: %v", err)
|
||||
}
|
||||
for _, dep := range strings.Fields(string(out)) {
|
||||
if why, bad := forbidden[dep]; bad {
|
||||
t.Errorf("wire imports %s — %s", dep, why)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package wire
|
||||
|
||||
// The bodies that go up, and the shorthand that fills them.
|
||||
//
|
||||
// The omitted keys carry meaning of their own on a PATCH: a key that is absent
|
||||
// leaves the tracker's value alone, and a key that is present overwrites it. So
|
||||
// "no opinion" and "empty" must not marshal the same way, which is what every
|
||||
// pointer and every omitempty below is for.
|
||||
|
||||
// IssueRequest is the body of a create or an edit.
|
||||
//
|
||||
// Every field is a pointer because Gitea reads an absent key as "no opinion"
|
||||
// and a present one as "make it this", and the difference is not academic: an
|
||||
// empty `ref` CLEARS the branch an issue is pinned to, and an empty `labels`
|
||||
// clears its labels. A caller meaning to change only the state would do both by
|
||||
// accident with plain zero values. Set fills a field; leaving it nil leaves the
|
||||
// tracker's copy alone.
|
||||
type IssueRequest struct {
|
||||
Title *string `json:"title,omitempty"`
|
||||
Body *string `json:"body,omitempty"`
|
||||
// Labels is a pointer because `[]` is a statement — it clears every label
|
||||
// on the issue — while a caller that has not resolved label ids at all has
|
||||
// no business making it. A plain slice with omitempty cannot say both.
|
||||
Labels *[]int64 `json:"labels,omitempty"`
|
||||
Assignees *[]string `json:"assignees,omitempty"`
|
||||
// Milestone is a pointer for the same reason, and because 0 is Gitea's
|
||||
// "detach from its milestone" — a value somebody may well mean.
|
||||
Milestone *int64 `json:"milestone,omitempty"`
|
||||
State *string `json:"state,omitempty"`
|
||||
Ref *string `json:"ref,omitempty"`
|
||||
}
|
||||
|
||||
// LabelRequest is the body of a label create or edit — everything a repository
|
||||
// needs to make one label.
|
||||
//
|
||||
// Value fields, not pointers, and every one of them is sent: Gitea 1.26 patches
|
||||
// only what it is given, but an older server reads an absent field as empty and
|
||||
// blanks it. A label edit is rare enough that sending the unchanged name and
|
||||
// description along costs nothing and removes a way to lose them.
|
||||
//
|
||||
// It goes up as a request body of its own because `tea labels create` could not
|
||||
// set `exclusive` — the flag that makes `type/*` behave like a single choice —
|
||||
// which is the whole reason label creation went through the API rather than a
|
||||
// CLI wrapper.
|
||||
//
|
||||
// What a label MEANS — which namespaces are exclusive, what colour a severity
|
||||
// is — is not decided here. This is the shape; the taxonomy is the domain's and
|
||||
// the palette is the bridge's.
|
||||
type LabelRequest struct {
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
Description string `json:"description"`
|
||||
Exclusive bool `json:"exclusive"`
|
||||
}
|
||||
|
||||
// Set is a pointer to v, for filling the optional fields of a request. Gitea
|
||||
// reads an absent key as "no opinion" and a present one as "make it this", so
|
||||
// those fields are pointers and this is the shorthand that fills them.
|
||||
func Set[T any](v T) *T { return &v }
|
||||
@@ -0,0 +1,155 @@
|
||||
// Package wire is the protocol: the JSON shapes a Gitea instance sends and
|
||||
// takes, the identifiers that address them, and nothing else.
|
||||
//
|
||||
// It is a package because two layers need the same vocabulary and neither may
|
||||
// import the other. internal/gitea is the transport — HTTP verbs, pagination,
|
||||
// status codes, credentials — and internal/mapping is the bridge — md <-> JSON,
|
||||
// pure functions, no network. Both have to name a Gitea issue, and when each
|
||||
// named it with a struct of its own, every command written on top of the two
|
||||
// would have had to copy a payload field by field from one spelling into the
|
||||
// other. Two copies of a shape also drift: the first field only one of them
|
||||
// learns is a field the other silently drops.
|
||||
//
|
||||
// THIS PACKAGE IMPORTS THE STANDARD LIBRARY AND NOTHING ELSE — no HTTP, no
|
||||
// filesystem, no configuration, and above all not internal/issue. That is what
|
||||
// lets the transport and the bridge share it without either one landing inside
|
||||
// the other's layer, and layering_test.go fails the moment it stops being true.
|
||||
//
|
||||
// Structs and not map[string]any, because the two representations disagreeing
|
||||
// is the failure this vocabulary exists to make debuggable: a typo in a key is
|
||||
// a compile error here and a silently dropped field there. Anything Gitea sends
|
||||
// that is not named below is not read by anybody — decoding is lossy on
|
||||
// purpose, since the tracker is not the record for anything the domain owns.
|
||||
package wire
|
||||
|
||||
// User is whoever wrote or was assigned something.
|
||||
//
|
||||
// Only the login crosses this boundary — it is the one field of a Gitea user
|
||||
// that means anything to a command, it is what `assignees:` holds, and a
|
||||
// display name is not an identity anything can be pushed against. A transport
|
||||
// that carries the rest invites somebody to use it.
|
||||
type User struct {
|
||||
Login string `json:"login"`
|
||||
}
|
||||
|
||||
// Label as the tracker holds it.
|
||||
//
|
||||
// Color is hex. Gitea returns it without the leading `#` (`ee0701`) and accepts
|
||||
// it either way; both spellings are the same color, so a comparison has to
|
||||
// strip before it compares.
|
||||
type Label struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Color string `json:"color"`
|
||||
Description string `json:"description"`
|
||||
Exclusive bool `json:"exclusive"`
|
||||
}
|
||||
|
||||
// Milestone as the tracker holds it. The domain carries its title; the id
|
||||
// exists only long enough to be sent back.
|
||||
type Milestone struct {
|
||||
ID int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
State string `json:"state"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// RepoRef is the repository an issue payload says it belongs to. Present on a
|
||||
// dependency listing, where the answer may well be another repository.
|
||||
type RepoRef struct {
|
||||
Owner string `json:"owner"`
|
||||
Name string `json:"name"`
|
||||
FullName string `json:"full_name"`
|
||||
}
|
||||
|
||||
// PullRequest is non-nil on a row that is a pull request rather than an issue.
|
||||
// Gitea's issue endpoints return both, and `type=issues` is a filter the server
|
||||
// has been known to ignore — which is why every listing re-checks it.
|
||||
type PullRequest struct {
|
||||
Merged bool `json:"merged"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
}
|
||||
|
||||
// Issue is a tracker row: a Gitea issue as the API reports it.
|
||||
//
|
||||
// Timestamps stay strings. They are written into an issue's metadata verbatim
|
||||
// and compared as opaque values; parsing them here would mean formatting them
|
||||
// back, and a round trip through a time package is a chance to hand the store a
|
||||
// different string than the tracker sent.
|
||||
type Issue struct {
|
||||
Number int `json:"number"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
State string `json:"state"`
|
||||
// Ref is the branch the issue is pinned to.
|
||||
Ref string `json:"ref"`
|
||||
// HTMLURL and UpdatedAt are the tracker's own bookkeeping and land in the
|
||||
// domain's Extra untouched.
|
||||
HTMLURL string `json:"html_url"`
|
||||
// Comments is a count, not a thread: the thread is fetched separately and
|
||||
// parked beside the issue as a sidecar.
|
||||
Comments int `json:"comments"`
|
||||
Labels []Label `json:"labels"`
|
||||
Assignees []User `json:"assignees"`
|
||||
Milestone *Milestone `json:"milestone"`
|
||||
Repository *RepoRef `json:"repository"`
|
||||
PullRequest *PullRequest `json:"pull_request"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// IsPullRequest reports whether this row is a pull request.
|
||||
func (i *Issue) IsPullRequest() bool { return i.PullRequest != nil }
|
||||
|
||||
// LabelNames are the label names, in the order the tracker listed them.
|
||||
func (i *Issue) LabelNames() []string {
|
||||
out := make([]string, 0, len(i.Labels))
|
||||
for _, l := range i.Labels {
|
||||
out = append(out, l.Name)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// AssigneeLogins are the assignees, as logins.
|
||||
func (i *Issue) AssigneeLogins() []string {
|
||||
out := make([]string, 0, len(i.Assignees))
|
||||
for _, a := range i.Assignees {
|
||||
out = append(out, a.Login)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// MilestoneTitle is the milestone's title, or "" when there is none.
|
||||
func (i *Issue) MilestoneTitle() string {
|
||||
if i.Milestone == nil {
|
||||
return ""
|
||||
}
|
||||
return i.Milestone.Title
|
||||
}
|
||||
|
||||
// KeyIn is this issue's cross-repo handle. The payload's own repository wins
|
||||
// when it carries one — a dependency listing answers with issues from other
|
||||
// repositories — and fallback is the repository that was asked.
|
||||
func (i *Issue) KeyIn(fallback Repo) Key {
|
||||
repo := fallback
|
||||
if i.Repository != nil {
|
||||
if r, err := ParseRepo(i.Repository.FullName); err == nil {
|
||||
repo = r
|
||||
}
|
||||
}
|
||||
return Key{Repo: repo, Number: i.Number}
|
||||
}
|
||||
|
||||
// Comment is one entry in an issue's thread.
|
||||
//
|
||||
// Read only, in practice: a thread is flattened to markdown for a reader and
|
||||
// nothing writes that markdown back, which is why the rendering may be as lossy
|
||||
// as a reader needs.
|
||||
type Comment struct {
|
||||
ID int64 `json:"id"`
|
||||
Body string `json:"body"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
User User `json:"user"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
Reference in New Issue
Block a user