feat: reach the rest of Gitea with kettle api, and drop tea

The plugin required `tea`, Gitea's own CLI, for everything that is not an
issue: releases, pull requests, milestones, branches, actions, webhooks. That
put a second binary, a second set of logins nothing here could see, and 400
lines documenting somebody else's flags outside anything this repository can
test. One command over the transport that already existed removes all three.

Transport: `post` — the hand-rolled request the SDK cannot express, written for
the dependency endpoint — is generalized to an exported `Do`, and `post` is
three lines on top of it. Same http.Client, so the same RoundTripper files the
body under .kettle/payload/, the same `token …` header authenticates it, and a
non-2xx is the same *APIError. It does not paginate, does not reformat the
answer, and names no domain concept, so the layering test is untouched.

The endpoint rule is `tea api`'s, so an endpoint table written for that tool
still works — with one restriction it did not have: a full URL must be on this
instance. Every request carries the project's token in a header, and a URL on
another host would hand the token to whatever was typed.

Command: `kettle api <endpoint>` in a new `api` group, so the generator writes
plugins/kettle/skills/api/SKILL.md — group, directory and /kettle:api are one
word. No --repo and no --login, for the reason no sync command has them: a
cross-repository address is an address, and another instance is KETTLE_URL.
`-X DELETE` needs `--yes`; a flag typed on purpose is an operator's decision.

Scopes: a token minted for issues carries write:issue and answers 403 on the
first request outside issues, naming no scope. Gitea cannot be asked what a
token may do — its own token listing needs a password — so `auth add --scopes`
records it, `auth list` and `config` show it, and a 403 says which category it
is likely to be. Documentation only; nothing is checked against it.

skills/use — the tea reference, 239 lines of it — becomes skills/api: what to
ask for, which endpoints paginate, and how to write a body. Every mention of
`tea` as a requirement is gone from the manifests, the READMEs, the runner and
the four other skills; what survives is the back-compat with the old plugin,
which is a decision and not a debt.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
naudachu
2026-08-12 14:25:20 +05:00
parent e177f46510
commit f18a633185
32 changed files with 1335 additions and 519 deletions
+141
View File
@@ -487,6 +487,147 @@ func TestDependenciesAreAskedForOnAnInstanceThatHasThem(t *testing.T) {
}
}
// The generic request: bytes out, bytes back, and the same three services every
// other call in this package gets — the header, the scratchpad, the *APIError.
func TestDoAnswersWithWhatTheServerSent(t *testing.T) {
root := newProject(t)
var got struct{ method, uri, auth, ctype string }
var sent []byte
srv := serve(t, modernGitea, func(w http.ResponseWriter, r *http.Request) {
got.method, got.uri = r.Method, r.URL.RequestURI()
got.auth, got.ctype = r.Header.Get("Authorization"), r.Header.Get("Content-Type")
sent, _ = io.ReadAll(r.Body)
w.WriteHeader(http.StatusCreated)
io.WriteString(w, `{"tag_name":"v0.2.0"}`)
})
c := newClient(t, srv.URL)
// A read: no body out, and nothing filed — the scratchpad holds what was
// SENT, and a run that sent nothing leaves no directory behind.
code, answer, err := c.Do(http.MethodGet, "repos/acme/widgets/releases?limit=50", nil, "")
if err != nil {
t.Fatalf("Do: %v", err)
}
if code != http.StatusCreated || string(answer) != `{"tag_name":"v0.2.0"}` {
t.Errorf("got %d %q, want 201 and the server's bytes", code, answer)
}
if got.uri != "/api/v1/repos/acme/widgets/releases?limit=50" {
t.Errorf("the endpoint was rewritten: %s", got.uri)
}
if got.auth != "token s3cret" {
t.Errorf("Authorization was %q, want %q", got.auth, "token s3cret")
}
if got.ctype != "" {
t.Errorf("a request with no body carried Content-Type %q", got.ctype)
}
if _, err := os.Stat(filepath.Join(root, ".kettle", "payload")); !os.IsNotExist(err) {
t.Errorf("a read created the payload directory (%v)", err)
}
// A write: the body goes out verbatim and is filed under the name it was
// given, by the same RoundTripper that files every other request.
body := []byte(`{"tag_name":"v0.2.0","body":"a & b"}`)
if _, _, err := c.Do(http.MethodPost, "/api/v1/repos/acme/widgets/releases", body, "release-v0-2-0"); err != nil {
t.Fatalf("Do: %v", err)
}
if got.method != http.MethodPost || got.ctype != "application/json" {
t.Errorf("the write went out as %s %q", got.method, got.ctype)
}
if string(sent) != string(body) {
t.Errorf("the server got %s, want %s — a passthrough reformatted the body", sent, body)
}
filed, err := os.ReadFile(filepath.Join(root, ".kettle", "payload", "release-v0-2-0.json"))
if err != nil {
t.Fatalf("the body was not filed: %v", err)
}
if !strings.Contains(string(filed), `"tag_name": "v0.2.0"`) {
t.Errorf("the dump is not the body that was sent:\n%s", filed)
}
}
// A refusal comes back as this package's error, with the status and the
// server's own words — and the status and body are returned as well, so a
// caller that would rather print them than wrap them can.
func TestDoReportsAStatusAndTheServersWords(t *testing.T) {
newProject(t)
srv := serve(t, modernGitea, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, `{"message":"release does not exist"}`)
})
code, answer, err := newClient(t, srv.URL).Do(http.MethodGet, "repos/acme/widgets/releases/9", nil, "")
if err == nil {
t.Fatal("a 404 came back as success")
}
var apiErr *gitea.APIError
if !errors.As(err, &apiErr) {
t.Fatalf("error is %T, want *gitea.APIError: %v", err, err)
}
if code != http.StatusNotFound || !strings.Contains(string(answer), "release does not exist") {
t.Errorf("got %d %q; the status and the body are the caller's too", code, answer)
}
for _, want := range []string{"404", "release does not exist", "GET", "/api/v1/repos/acme/widgets/releases/9"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("the error does not mention %q:\n%s", want, err)
}
}
}
// A 403 is answered with what to do about it, because Gitea's own 403 names no
// scope and a token minted for issues is the usual reason.
func TestAForbiddenAnswerNamesTheScopeItMightBe(t *testing.T) {
newProject(t)
srv := serve(t, modernGitea, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusForbidden)
io.WriteString(w, `{"message":"token does not have at least one of required scope(s)"}`)
})
_, _, err := newClient(t, srv.URL).Do(http.MethodPost, "repos/acme/widgets/releases", []byte(`{}`), "")
if err == nil {
t.Fatal("a 403 came back as success")
}
for _, want := range []string{"403", "kettle auth list", "repository"} {
if !strings.Contains(err.Error(), want) {
t.Errorf("a 403 does not say %q — the server named no scope, so this has to:\n%s", want, err)
}
}
}
// The token is this instance's. A full URL somewhere else is refused before a
// socket is opened, because sending it would hand the credential to whatever
// host was typed.
func TestDoRefusesAURLOnAnotherHost(t *testing.T) {
newProject(t)
asked := 0
srv := serve(t, modernGitea, func(w http.ResponseWriter, r *http.Request) {
asked++
writeJSON(t, w, map[string]any{})
})
c := newClient(t, srv.URL)
_, _, err := c.Do(http.MethodGet, "https://gitea.example.invalid/api/v1/user", nil, "")
if err == nil {
t.Fatal("a request to another host was allowed — that sends this project's token to it")
}
if strings.Contains(err.Error(), "s3cret") {
t.Errorf("the refusal quotes the token:\n%s", err)
}
if asked != 0 {
t.Errorf("%d request(s) went out for an endpoint that was refused", asked)
}
// A full URL on the instance itself is the same request as the bare path.
if _, _, err := c.Do(http.MethodGet, srv.URL+"/api/v1/user", nil, ""); err != nil {
t.Errorf("a full URL on this instance was refused: %v", err)
}
if asked != 1 {
t.Errorf("%d request(s) went out, want 1", asked)
}
}
// A half-filled configuration is refused here rather than at the first 401,
// because a 401 names nothing an operator can act on — and before the client is
// built at all, because building one dials.