feat(tools): dir_list

This commit is contained in:
Mathias Bergqvist
2026-05-06 22:49:50 +02:00
parent 284d5e19f6
commit 0eb9ebcafd
4 changed files with 211 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
)
type FileContents struct {
@@ -150,6 +151,36 @@ func (c *Client) GetBranchProtection(ctx context.Context, owner, repo, branch st
return &bp, nil
}
type DirEntry struct {
Name string `json:"name"`
Path string `json:"path"`
Type string `json:"type"`
Sha string `json:"sha"`
Size int64 `json:"size"`
}
func (c *Client) ListContents(ctx context.Context, owner, repo, path, ref string) ([]DirEntry, error) {
p := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, path)
if ref != "" {
p += "?ref=" + url.QueryEscape(ref)
}
body, status, err := c.GetJSON(ctx, p)
if err != nil {
return nil, err
}
if err := MapStatus(status, body); err != nil {
return nil, err
}
if len(body) > 0 && body[0] == '{' {
return nil, fmt.Errorf("path is a file, not a directory — use file_read: %w", ErrValidation)
}
var entries []DirEntry
if err := json.Unmarshal(body, &entries); err != nil {
return nil, err
}
return entries, nil
}
// UpsertFile creates a file when args.Sha is empty (POST) or updates an existing
// file when args.Sha is set (PUT). Gitea routes both operations by HTTP method on
// the same /contents/{path} URL, and rejects PUT without a sha.