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

Function searchRepos

pkg/cmd/repo/list/http.go:118–174  ·  view source on GitHub ↗
(client *http.Client, hostname string, limit int, owner string, filter FilterOptions)

Source from the content-addressed store, hash-verified

116}
117
118func searchRepos(client *http.Client, hostname string, limit int, owner string, filter FilterOptions) (*RepositoryList, error) {
119 type result struct {
120 Search struct {
121 RepositoryCount int
122 Nodes []api.Repository
123 PageInfo struct {
124 HasNextPage bool
125 EndCursor string
126 }
127 }
128 }
129
130 query := fmt.Sprintf(`query RepositoryListSearch($query:String!,$perPage:Int!,$endCursor:String) {
131 search(type: REPOSITORY, query: $query, first: $perPage, after: $endCursor) {
132 repositoryCount
133 nodes{...on Repository{%s}}
134 pageInfo{hasNextPage,endCursor}
135 }
136 }`, api.RepositoryGraphQL(filter.Fields))
137
138 perPage := min(limit, 100)
139
140 variables := map[string]any{
141 "query": githubv4.String(searchQuery(owner, filter)),
142 "perPage": githubv4.Int(perPage),
143 }
144
145 apiClient := api.NewClientFromHTTP(client)
146 listResult := RepositoryList{FromSearch: true}
147pagination:
148 for {
149 var result result
150 err := apiClient.GraphQL(hostname, query, variables, &result)
151 if err != nil {
152 return nil, err
153 }
154
155 listResult.TotalCount = result.Search.RepositoryCount
156 for _, repo := range result.Search.Nodes {
157 if listResult.Owner == "" && repo.NameWithOwner != "" {
158 idx := strings.IndexRune(repo.NameWithOwner, '/')
159 listResult.Owner = repo.NameWithOwner[:idx]
160 }
161 listResult.Repositories = append(listResult.Repositories, repo)
162 if len(listResult.Repositories) >= limit {
163 break pagination
164 }
165 }
166
167 if !result.Search.PageInfo.HasNextPage {
168 break
169 }
170 variables["endCursor"] = githubv4.String(result.Search.PageInfo.EndCursor)
171 }
172
173 return &listResult, nil
174}
175

Callers 1

listReposFunction · 0.85

Calls 5

RepositoryGraphQLFunction · 0.92
NewClientFromHTTPFunction · 0.92
searchQueryFunction · 0.85
GraphQLMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected