MCPcopy Create free account
hub / github.com/cli/cli / search

Method search

pkg/search/searcher.go:208–288  ·  view source on GitHub ↗

search makes a single-page REST search request for code, commits, issues, prs, or repos, and returns the link header from response for further pagination calls. If the link header is not set on the response, empty string is returned. The result argument is populated with the following information:

(query Query, result any)

Source from the content-addressed store, hash-verified

206//
207// For more information, see https://docs.github.com/en/rest/search/search?apiVersion=2022-11-28.
208func (s searcher) search(query Query, result any) (string, error) {
209 u, err := safeurl.JoinPathWithHostPrefix(ghinstance.RESTPrefix(s.host), "search", string(query.Kind))
210 if err != nil {
211 return "", err
212 }
213 u.SetQuery("page", strconv.Itoa(query.Page))
214 u.SetQuery("per_page", strconv.Itoa(query.Limit))
215
216 if query.Kind == KindIssues {
217 // TODO advancedIssueSearchCleanup
218 // We won't need feature detection when GHES 3.17 support ends, since
219 // the advanced issue search is the only available search backend for
220 // issues.
221 features, err := s.detector.SearchFeatures()
222 if err != nil {
223 return "", err
224 }
225
226 if !features.AdvancedIssueSearchAPI {
227 u.SetQuery("q", query.StandardSearchString())
228 } else {
229 u.SetQuery("q", query.AdvancedIssueSearchString())
230
231 // TODO advancedIssueSearchCleanup
232 if features.AdvancedIssueSearchAPIOptIn {
233 // Advanced syntax should be explicitly enabled
234 u.SetQuery("advanced_search", "true")
235 }
236 }
237
238 switch query.IssueSearchType {
239 case "semantic":
240 if !features.SemanticSearch {
241 return "", fmt.Errorf("semantic search is not supported on this host: %s", s.host)
242 }
243 u.SetQuery("search_type", query.IssueSearchType)
244 case "hybrid":
245 if !features.HybridSearch {
246 return "", fmt.Errorf("hybrid search is not supported on this host: %s", s.host)
247 }
248 u.SetQuery("search_type", query.IssueSearchType)
249 }
250 } else {
251 u.SetQuery("q", query.StandardSearchString())
252 }
253
254 if query.Order != "" {
255 u.SetQuery(orderKey, query.Order)
256 }
257 if query.Sort != "" {
258 u.SetQuery(sortKey, query.Sort)
259 }
260 req, err := http.NewRequest("GET", u.String(), nil)
261 if err != nil {
262 return "", err
263 }
264 req.Header.Set("Content-Type", "application/json; charset=utf-8")
265 req.Header.Set("Accept", "application/vnd.github.v3+json")

Callers 4

CodeMethod · 0.95
CommitsMethod · 0.95
RepositoriesMethod · 0.95
IssuesMethod · 0.95

Calls 13

JoinPathWithHostPrefixFunction · 0.92
RESTPrefixFunction · 0.92
handleHTTPErrorFunction · 0.85
SetQueryMethod · 0.80
StandardSearchStringMethod · 0.80
SearchFeaturesMethod · 0.65
ErrorfMethod · 0.65
StringMethod · 0.65
SetMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected