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:
+116
-22
@@ -69,8 +69,9 @@ const (
|
||||
type Client struct {
|
||||
// api is the SDK client: one per run, shared by every copy For makes.
|
||||
api *sdk.Client
|
||||
// http is the SDK's transport, kept because AddDependency still sends one
|
||||
// request by hand — see there.
|
||||
// http is the SDK's transport, kept because Do sends requests by hand — the
|
||||
// dependency endpoint the SDK cannot spell, and every endpoint this package
|
||||
// has no method for.
|
||||
http *http.Client
|
||||
// dump is the RoundTripper that files request bodies. Shared with every
|
||||
// copy For makes, because the scratchpad is one directory per run.
|
||||
@@ -186,9 +187,31 @@ func (e *APIError) Error() string {
|
||||
if status != "" {
|
||||
status = " " + status
|
||||
}
|
||||
return fmt.Sprintf("%s %s: %d%s: %s", e.Method, e.URL, e.Status, status, body)
|
||||
out := fmt.Sprintf("%s %s: %d%s: %s", e.Method, e.URL, e.Status, status, body)
|
||||
if e.Status == http.StatusForbidden {
|
||||
out += "\n" + scopeHint
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// scopeHint is what a 403 gets said after it.
|
||||
//
|
||||
// Gitea scopes a token as <read|write>:<category>, and a token minted to file
|
||||
// issues carries `write:issue` and nothing more — which is exactly right until
|
||||
// the first request outside issues, where releases, pull requests, branches and
|
||||
// tags all live under `repository` and the answer is a 403 that names no scope
|
||||
// at all. The server will not say which one is missing, so this does not guess
|
||||
// one; it names the two commands that show what was recorded and let it be
|
||||
// re-recorded.
|
||||
//
|
||||
// Blanket rather than per-call, because the transport does not know which
|
||||
// category an arbitrary endpoint belongs to — and a 403 on a request that had
|
||||
// the scope is a permissions problem on the repository, which this sentence does
|
||||
// not contradict.
|
||||
const scopeHint = "a 403 is usually the token's scopes rather than the request: Gitea scopes a token as " +
|
||||
"<read|write>:<category>, and everything outside issues (releases, pull requests, branches, tags, actions) " +
|
||||
"is `repository`. `kettle auth list` shows what each login on this machine records."
|
||||
|
||||
// StatusIs reports whether err is an API answer with this status code, for the
|
||||
// handful of places where one code means something specific — a 409 from a
|
||||
// dependency link that is already there, say.
|
||||
@@ -404,30 +427,58 @@ func safeName(name string) string {
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// the one request the SDK cannot express
|
||||
// the requests the SDK cannot express
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
// post sends one JSON body to a path under this instance's API and ignores
|
||||
// whatever comes back.
|
||||
// Do sends one request to a path under this instance's API and returns the
|
||||
// status and the body exactly as they came back.
|
||||
//
|
||||
// It exists for AddDependency and for nothing else — see there for what the SDK
|
||||
// leaves out. It goes through the same http.Client, so the body is filed and a
|
||||
// failure carries the status and the server's words exactly as every other call
|
||||
// in this package does.
|
||||
func (c *Client) post(path string, body any, name string) error {
|
||||
raw, err := json.Marshal(body)
|
||||
// It is the escape hatch, and it was here before it was one: the dependency
|
||||
// endpoint needed a body the SDK's own type cannot spell (see AddDependency),
|
||||
// so a hand-rolled request already existed. What has changed is that it is
|
||||
// exported, which is what lets `kettle api` reach a release, a pull request or a
|
||||
// webhook without this package growing a method per entity — and without a
|
||||
// second client that would hold the credentials all over again.
|
||||
//
|
||||
// It goes through the same http.Client as everything else, which is the whole
|
||||
// point: the same dump-RoundTripper files the body under `.kettle/payload/`, the
|
||||
// same `token …` header authenticates it, and a non-2xx comes back as the same
|
||||
// *APIError carrying the status AND what the server said.
|
||||
//
|
||||
// THREE THINGS IT DELIBERATELY DOES NOT DO:
|
||||
//
|
||||
// - IT DOES NOT PAGINATE. One call is one HTTP request. The pagination in this
|
||||
// package exists for a listing with a budget to spend, and a passthrough that
|
||||
// quietly stitched pages together would report as one answer something that
|
||||
// was several — `?page=` and `?limit=` are the caller's to spell.
|
||||
// - IT DOES NOT PARSE OR REFORMAT THE ANSWER. Bytes in, bytes out. Whoever
|
||||
// asked knows what they asked for; re-indenting it here would only be a
|
||||
// second opinion about somebody else's JSON.
|
||||
// - IT DOES NOT KNOW WHAT AN ISSUE IS. Nothing about it names a domain concept,
|
||||
// so the layering rule holds unchanged — this is still transport, and a
|
||||
// generic one is no more a domain than a specific one was.
|
||||
//
|
||||
// A nil body sends no body at all, which is what a GET and a DELETE want; the
|
||||
// Content-Type goes on only when there is something to type.
|
||||
func (c *Client) Do(method, path string, body []byte, name string) (int, []byte, error) {
|
||||
endpoint, err := c.endpoint(path)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, nil, err
|
||||
}
|
||||
endpoint := c.base + "/api/v1/" + strings.TrimLeft(path, "/")
|
||||
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewReader(raw))
|
||||
var reader io.Reader
|
||||
if body != nil {
|
||||
reader = bytes.NewReader(body)
|
||||
}
|
||||
req, err := http.NewRequest(method, endpoint, reader)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, nil, err
|
||||
}
|
||||
// Gitea's own scheme, and what the `tea` CLI this replaces sent: the word
|
||||
// `token`, not `Bearer`. An instance answers 401 to the other spelling.
|
||||
req.Header.Set("Authorization", "token "+c.token)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
|
||||
@@ -436,14 +487,57 @@ func (c *Client) post(path string, body any, name string) error {
|
||||
if err != nil {
|
||||
// The token travels in a header and never in the URL, so an error is
|
||||
// free to quote the URL in full.
|
||||
return err
|
||||
return 0, nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
answer, _ := io.ReadAll(resp.Body)
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
return &APIError{Method: http.MethodPost, URL: endpoint, Status: resp.StatusCode, Body: string(answer)}
|
||||
answer, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return resp.StatusCode, nil, fmt.Errorf("%s %s: %d answered with a body that could not be read: %w",
|
||||
method, endpoint, resp.StatusCode, err)
|
||||
}
|
||||
return nil
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
return resp.StatusCode, answer, &APIError{
|
||||
Method: method, URL: endpoint, Status: resp.StatusCode, Body: string(answer)}
|
||||
}
|
||||
return resp.StatusCode, answer, nil
|
||||
}
|
||||
|
||||
// endpoint resolves what a caller spelled into an absolute URL on this instance.
|
||||
//
|
||||
// The three spellings are the ones `tea api` accepted, so a table of endpoints
|
||||
// written for that tool still works here: a bare path is under `/api/v1/`, a
|
||||
// path already starting `/api/` is taken as it stands (that is how anything
|
||||
// outside v1 is reached), and a full URL is a full URL.
|
||||
//
|
||||
// A FULL URL MUST BE ON THIS INSTANCE, and that is the one place this is
|
||||
// stricter than the tool it replaces. Every request made here carries the
|
||||
// project's token in a header; a URL pointing somewhere else would hand that
|
||||
// token to whatever host was named, which is a credential leak spelled as a
|
||||
// convenience. Reaching another instance is what KETTLE_URL is for.
|
||||
func (c *Client) endpoint(path string) (string, error) {
|
||||
switch {
|
||||
case strings.HasPrefix(path, "http://"), strings.HasPrefix(path, "https://"):
|
||||
if path != c.base && !strings.HasPrefix(path, c.base+"/") {
|
||||
return "", fmt.Errorf("%s is not on %s — this token belongs to that instance and is sent nowhere else"+
|
||||
" (point %s at the other one instead)", path, c.base, config.EnvURL)
|
||||
}
|
||||
return path, nil
|
||||
case strings.HasPrefix(path, "/api/"):
|
||||
return c.base + path, nil
|
||||
default:
|
||||
return c.base + "/api/v1/" + strings.TrimLeft(path, "/"), nil
|
||||
}
|
||||
}
|
||||
|
||||
// post sends one JSON body to a path under this instance's API and ignores
|
||||
// whatever comes back. AddDependency is what it is for.
|
||||
func (c *Client) post(path string, body any, name string) error {
|
||||
raw, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, _, err = c.Do(http.MethodPost, path, raw, name)
|
||||
return err
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user