11package list
22
33import (
4- "context "
4+ "fmt "
55 "net/http"
6- "reflect"
76 "strings"
8- "time"
97
10- "github.com/cli/cli/internal/ghinstance "
8+ "github.com/cli/cli/api "
119 "github.com/cli/cli/pkg/githubsearch"
1210 "github.com/shurcooL/githubv4"
13- "github.com/shurcooL/graphql"
1411)
1512
16- type Repository struct {
17- NameWithOwner string
18- Description string
19- IsFork bool
20- IsPrivate bool
21- IsArchived bool
22- PushedAt time.Time
23- }
24-
25- func (r Repository ) Info () string {
26- var tags []string
27-
28- if r .IsPrivate {
29- tags = append (tags , "private" )
30- } else {
31- tags = append (tags , "public" )
32- }
33- if r .IsFork {
34- tags = append (tags , "fork" )
35- }
36- if r .IsArchived {
37- tags = append (tags , "archived" )
38- }
39-
40- return strings .Join (tags , ", " )
41- }
42-
4313type RepositoryList struct {
4414 Owner string
45- Repositories []Repository
15+ Repositories []api. Repository
4616 TotalCount int
4717 FromSearch bool
4818}
@@ -54,6 +24,7 @@ type FilterOptions struct {
5424 Language string
5525 Archived bool
5626 NonArchived bool
27+ Fields []string
5728}
5829
5930func listRepos (client * http.Client , hostname string , limit int , owner string , filter FilterOptions ) (* RepositoryList , error ) {
@@ -67,62 +38,65 @@ func listRepos(client *http.Client, hostname string, limit int, owner string, fi
6738 }
6839
6940 variables := map [string ]interface {}{
70- "perPage" : githubv4 .Int (perPage ),
71- "endCursor" : (* githubv4 .String )(nil ),
41+ "perPage" : githubv4 .Int (perPage ),
7242 }
7343
7444 if filter .Visibility != "" {
7545 variables ["privacy" ] = githubv4 .RepositoryPrivacy (strings .ToUpper (filter .Visibility ))
76- } else {
77- variables ["privacy" ] = (* githubv4 .RepositoryPrivacy )(nil )
7846 }
7947
8048 if filter .Fork {
8149 variables ["fork" ] = githubv4 .Boolean (true )
8250 } else if filter .Source {
8351 variables ["fork" ] = githubv4 .Boolean (false )
84- } else {
85- variables ["fork" ] = (* githubv4 .Boolean )(nil )
8652 }
8753
54+ inputs := []string {"$perPage:Int!" , "$endCursor:String" , "$privacy:RepositoryPrivacy" , "$fork:Boolean" }
8855 var ownerConnection string
8956 if owner == "" {
90- ownerConnection = `graphql: "repositoryOwner: viewer"`
57+ ownerConnection = "repositoryOwner: viewer"
9158 } else {
92- ownerConnection = `graphql: "repositoryOwner(login: $owner)"`
59+ ownerConnection = "repositoryOwner(login: $owner)"
9360 variables ["owner" ] = githubv4 .String (owner )
61+ inputs = append (inputs , "$owner:String!" )
62+ }
63+
64+ type result struct {
65+ RepositoryOwner struct {
66+ Login string
67+ Repositories struct {
68+ Nodes []api.Repository
69+ TotalCount int
70+ PageInfo struct {
71+ HasNextPage bool
72+ EndCursor string
73+ }
74+ }
75+ }
9476 }
9577
96- type repositoryOwner struct {
97- Login string
98- Repositories struct {
99- Nodes []Repository
100- TotalCount int
101- PageInfo struct {
102- HasNextPage bool
103- EndCursor string
78+ query := fmt .Sprintf (`query RepositoryList(%s) {
79+ %s {
80+ login
81+ repositories(first: $perPage, after: $endCursor, privacy: $privacy, isFork: $fork, ownerAffiliations: OWNER, orderBy: { field: PUSHED_AT, direction: DESC }) {
82+ nodes{%s}
83+ totalCount
84+ pageInfo{hasNextPage,endCursor}
10485 }
105- } `graphql:"repositories(first: $perPage, after: $endCursor, privacy: $privacy, isFork: $fork, ownerAffiliations: OWNER, orderBy: { field: PUSHED_AT, direction: DESC })"`
106- }
107- query := reflect .StructOf ([]reflect.StructField {
108- {
109- Name : "RepositoryOwner" ,
110- Type : reflect .TypeOf (repositoryOwner {}),
111- Tag : reflect .StructTag (ownerConnection ),
112- },
113- })
86+ }
87+ }` , strings .Join (inputs , "," ), ownerConnection , api .RepositoryGraphQL (filter .Fields ))
11488
115- gql := graphql . NewClient ( ghinstance . GraphQLEndpoint ( hostname ), client )
89+ apiClient := api . NewClientFromHTTP ( client )
11690 listResult := RepositoryList {}
11791pagination:
11892 for {
119- result := reflect . New ( query )
120- err := gql . QueryNamed ( context . Background (), "RepositoryList" , result . Interface (), variables )
93+ var res result
94+ err := apiClient . GraphQL ( hostname , query , variables , & res )
12195 if err != nil {
12296 return nil , err
12397 }
12498
125- owner := result . Elem (). FieldByName ( " RepositoryOwner" ). Interface ().( repositoryOwner )
99+ owner := res . RepositoryOwner
126100 listResult .TotalCount = owner .Repositories .TotalCount
127101 listResult .Owner = owner .Login
128102
@@ -143,47 +117,52 @@ pagination:
143117}
144118
145119func searchRepos (client * http.Client , hostname string , limit int , owner string , filter FilterOptions ) (* RepositoryList , error ) {
146- type query struct {
120+ type result struct {
147121 Search struct {
148122 RepositoryCount int
149- Nodes []struct {
150- Repository Repository `graphql:"...on Repository"`
151- }
152- PageInfo struct {
123+ Nodes []api.Repository
124+ PageInfo struct {
153125 HasNextPage bool
154126 EndCursor string
155127 }
156- } `graphql:"search(type: REPOSITORY, query: $query, first: $perPage, after: $endCursor)"`
128+ }
157129 }
158130
131+ query := fmt .Sprintf (`query RepositoryListSearch($query:String!,$perPage:Int!,$endCursor:String) {
132+ search(type: REPOSITORY, query: $query, first: $perPage, after: $endCursor) {
133+ repositoryCount
134+ nodes{...on Repository{%s}}
135+ pageInfo{hasNextPage,endCursor}
136+ }
137+ }` , api .RepositoryGraphQL (filter .Fields ))
138+
159139 perPage := limit
160140 if perPage > 100 {
161141 perPage = 100
162142 }
163143
164144 variables := map [string ]interface {}{
165- "query" : githubv4 .String (searchQuery (owner , filter )),
166- "perPage" : githubv4 .Int (perPage ),
167- "endCursor" : (* githubv4 .String )(nil ),
145+ "query" : githubv4 .String (searchQuery (owner , filter )),
146+ "perPage" : githubv4 .Int (perPage ),
168147 }
169148
170- gql := graphql . NewClient ( ghinstance . GraphQLEndpoint ( hostname ), client )
149+ apiClient := api . NewClientFromHTTP ( client )
171150 listResult := RepositoryList {FromSearch : true }
172151pagination:
173152 for {
174- var result query
175- err := gql . QueryNamed ( context . Background (), "RepositoryListSearch" , & result , variables )
153+ var result result
154+ err := apiClient . GraphQL ( hostname , query , variables , & result )
176155 if err != nil {
177156 return nil , err
178157 }
179158
180159 listResult .TotalCount = result .Search .RepositoryCount
181- for _ , node := range result .Search .Nodes {
160+ for _ , repo := range result .Search .Nodes {
182161 if listResult .Owner == "" {
183- idx := strings .IndexRune (node . Repository .NameWithOwner , '/' )
184- listResult .Owner = node . Repository .NameWithOwner [:idx ]
162+ idx := strings .IndexRune (repo .NameWithOwner , '/' )
163+ listResult .Owner = repo .NameWithOwner [:idx ]
185164 }
186- listResult .Repositories = append (listResult .Repositories , node . Repository )
165+ listResult .Repositories = append (listResult .Repositories , repo )
187166 if len (listResult .Repositories ) >= limit {
188167 break pagination
189168 }
0 commit comments