package gitea import ( "context" "encoding/json" "fmt" "net/url" ) type CodeSearchHit struct { Path string `json:"path"` Snippet string `json:"snippet"` HTMLURL string `json:"html_url"` Score float64 `json:"score,omitempty"` } type codeSearchEnvelope struct { Data []CodeSearchHit `json:"data"` OK bool `json:"ok"` } func (c *Client) SearchCode(ctx context.Context, owner, repo, q string, page, limit int) ([]CodeSearchHit, error) { if page < 1 { page = 1 } if limit < 1 { limit = 30 } path := fmt.Sprintf("/api/v1/repos/%s/%s/search?q=%s&type=code&page=%d&limit=%d", owner, repo, url.QueryEscape(q), page, limit) 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 codeSearchEnvelope if err := json.Unmarshal(body, &env); err != nil { return nil, err } return env.Data, nil }