Helper function that executes search queries against different GitHub search types (repositories, commits, code, issues, users, labels) If searchParameters.Query includes multiple condition, it MUST NOT include "+" as condition separator. For example, querying with "language:c++" and "leveldb", the
(ctx context.Context, searchType string, parameters *searchParameters, opts *SearchOptions, result any)
| 305 | // If searchParameters.Query includes multiple condition, it MUST NOT include "+" as condition separator. |
| 306 | // For example, querying with "language:c++" and "leveldb", then searchParameters.Query should be "language:c++ leveldb" but not "language:c+++leveldb". |
| 307 | func (s *SearchService) search(ctx context.Context, searchType string, parameters *searchParameters, opts *SearchOptions, result any) (*Response, error) { |
| 308 | params, err := qs.Values(opts) |
| 309 | if err != nil { |
| 310 | return nil, err |
| 311 | } |
| 312 | |
| 313 | if parameters.RepositoryID != nil { |
| 314 | params.Set("repository_id", strconv.FormatInt(*parameters.RepositoryID, 10)) |
| 315 | } |
| 316 | params.Set("q", parameters.Query) |
| 317 | u := fmt.Sprintf("search/%v?%v", searchType, params.Encode()) |
| 318 | |
| 319 | req, err := s.client.NewRequest(ctx, "GET", u, nil) |
| 320 | if err != nil { |
| 321 | return nil, err |
| 322 | } |
| 323 | var acceptHeaders []string |
| 324 | switch searchType { |
| 325 | case "commits": |
| 326 | // Accept header for search commits preview endpoint |
| 327 | acceptHeaders = append(acceptHeaders, mediaTypeCommitSearchPreview) |
| 328 | case "topics", "repositories": |
| 329 | // Accept header for search repositories based on topics preview endpoint |
| 330 | acceptHeaders = append(acceptHeaders, mediaTypeTopicsPreview) |
| 331 | case "issues": |
| 332 | // Accept header for search issues based on reactions preview endpoint |
| 333 | acceptHeaders = append(acceptHeaders, mediaTypeReactionsPreview) |
| 334 | } |
| 335 | // https://docs.github.com/rest/search?apiVersion=2022-11-28#search-repositories |
| 336 | // Accept header defaults to "application/vnd.github.v3+json" |
| 337 | // We change it here to fetch back text-match metadata |
| 338 | if opts != nil && opts.TextMatch { |
| 339 | acceptHeaders = append(acceptHeaders, "application/vnd.github.v3.text-match+json") |
| 340 | } |
| 341 | req.Header.Set("Accept", strings.Join(acceptHeaders, ", ")) |
| 342 | |
| 343 | return s.client.Do(req, result) |
| 344 | } |