feat(tools): repo_search with allowlist post-filter
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type Repo struct {
|
||||
@@ -38,6 +39,37 @@ func (c *Client) ListRepos(ctx context.Context, owner string, page, limit int) (
|
||||
return repos, nil
|
||||
}
|
||||
|
||||
type repoSearchEnvelope struct {
|
||||
Data []Repo `json:"data"`
|
||||
OK bool `json:"ok"`
|
||||
}
|
||||
|
||||
func (c *Client) SearchRepos(ctx context.Context, q, owner string, page, limit int) ([]Repo, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if limit < 1 {
|
||||
limit = 30
|
||||
}
|
||||
path := fmt.Sprintf("/api/v1/repos/search?q=%s&page=%d&limit=%d",
|
||||
url.QueryEscape(q), page, limit)
|
||||
if owner != "" {
|
||||
path += "&owner=" + url.QueryEscape(owner)
|
||||
}
|
||||
body, status, err := c.GetJSON(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := MapStatus(status, body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var env repoSearchEnvelope
|
||||
if err := json.Unmarshal(body, &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return env.Data, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetRepo(ctx context.Context, owner, name string) (*Repo, error) {
|
||||
path := fmt.Sprintf("/api/v1/repos/%s/%s", owner, name)
|
||||
body, status, err := c.GetJSON(ctx, path)
|
||||
|
||||
@@ -11,6 +11,23 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSearchRepos(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "/api/v1/repos/search", r.URL.Path)
|
||||
assert.Equal(t, "infra", r.URL.Query().Get("q"))
|
||||
assert.Equal(t, "mathias", r.URL.Query().Get("owner"))
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"data":[{"name":"infra","full_name":"mathias/infra","default_branch":"main"}],"ok":true}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
repos, err := c.SearchRepos(context.Background(), "infra", "mathias", 1, 30)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, repos, 1)
|
||||
assert.Equal(t, "mathias/infra", repos[0].FullName)
|
||||
}
|
||||
|
||||
func TestListRepos(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, "/api/v1/users/mathias/repos", r.URL.Path)
|
||||
|
||||
Reference in New Issue
Block a user