11package list
22
33import (
4- "fmt"
54 "strings"
65
76 "github.com/cli/cli/api"
@@ -14,35 +13,21 @@ type RepositoryList struct {
1413}
1514
1615func listRepos (client * api.Client , hostname string , limit int , owner string , filter FilterOptions ) (* RepositoryList , error ) {
17- type reposBlock struct {
18- TotalCount int
19- RepositoryCount int
20- Nodes []Repository
21- PageInfo struct {
22- HasNextPage bool
23- EndCursor string
24- }
25- }
26-
2716 type response struct {
2817 RepositoryOwner struct {
29- Repositories reposBlock
18+ Repositories struct {
19+ TotalCount int
20+ RepositoryCount int
21+ Nodes []Repository
22+ PageInfo struct {
23+ HasNextPage bool
24+ EndCursor string
25+ }
26+ }
3027 }
31- Search reposBlock
3228 }
3329
34- fragment := `
35- fragment repo on Repository {
36- nameWithOwner
37- description
38- isFork
39- isPrivate
40- isArchived
41- updatedAt
42- }`
43-
44- // If `--archived` wasn't specified, use `repositoryOwner.repositores`
45- query := fragment + `
30+ query := `
4631 query RepoList($owner: String!, $per_page: Int!, $endCursor: String, $fork: Boolean, $privacy: RepositoryPrivacy) {
4732 repositoryOwner(login: $owner) {
4833 repositories(
@@ -54,7 +39,12 @@ func listRepos(client *api.Client, hostname string, limit int, owner string, fil
5439 orderBy: { field: UPDATED_AT, direction: DESC }) {
5540 totalCount
5641 nodes {
57- ...repo
42+ nameWithOwner
43+ description
44+ isFork
45+ isPrivate
46+ isArchived
47+ updatedAt
5848 }
5949 pageInfo {
6050 hasNextPage
@@ -70,59 +60,23 @@ func listRepos(client *api.Client, hostname string, limit int, owner string, fil
7060 }
7161
7262 variables := map [string ]interface {}{
63+ "owner" : githubv4 .String (owner ),
7364 "per_page" : githubv4 .Int (perPage ),
7465 "endCursor" : (* githubv4 .String )(nil ),
7566 }
7667
77- hasArchivedFilter := filter .Archived
78-
79- if hasArchivedFilter {
80- // If `--archived` was specified, use the `search` API rather than
81- // `repositoryOwner.repositories`
82- query = fragment + `
83- query RepoList($per_page: Int!, $endCursor: String, $query: String!) {
84- search(first: $per_page, after:$endCursor, type: REPOSITORY, query: $query) {
85- repositoryCount
86- nodes {
87- ... on Repository {
88- ...repo
89- }
90- }
91- pageInfo {
92- hasNextPage
93- endCursor
94- }
95- }
96- }`
97-
98- search := []string {fmt .Sprintf ("user:%s archived:true fork:true sort:updated-desc" , owner )}
99-
100- switch filter .Visibility {
101- case "private" :
102- search = append (search , "is:private" )
103- case "public" :
104- search = append (search , "is:public" )
105- default :
106- search = append (search , "is:all" )
107- }
108-
109- variables ["query" ] = strings .Join (search , " " )
68+ if filter .Visibility != "" {
69+ variables ["privacy" ] = githubv4 .RepositoryPrivacy (strings .ToUpper (filter .Visibility ))
11070 } else {
111- variables ["owner" ] = githubv4 .String (owner )
112-
113- if filter .Visibility != "" {
114- variables ["privacy" ] = githubv4 .RepositoryPrivacy (strings .ToUpper (filter .Visibility ))
115- } else {
116- variables ["privacy" ] = (* githubv4 .RepositoryPrivacy )(nil )
117- }
71+ variables ["privacy" ] = (* githubv4 .RepositoryPrivacy )(nil )
72+ }
11873
119- if filter .Fork {
120- variables ["fork" ] = githubv4 .Boolean (true )
121- } else if filter .Source {
122- variables ["fork" ] = githubv4 .Boolean (false )
123- } else {
124- variables ["fork" ] = (* githubv4 .Boolean )(nil )
125- }
74+ if filter .Fork {
75+ variables ["fork" ] = githubv4 .Boolean (true )
76+ } else if filter .Source {
77+ variables ["fork" ] = githubv4 .Boolean (false )
78+ } else {
79+ variables ["fork" ] = (* githubv4 .Boolean )(nil )
12680 }
12781
12882 var repos []Repository
@@ -137,11 +91,7 @@ pagination:
13791 return nil , err
13892 }
13993
140- if hasArchivedFilter {
141- repos = append (repos , result .Search .Nodes ... )
142- } else {
143- repos = append (repos , result .RepositoryOwner .Repositories .Nodes ... )
144- }
94+ repos = append (repos , result .RepositoryOwner .Repositories .Nodes ... )
14595
14696 if len (repos ) >= limit {
14797 if len (repos ) > limit {
@@ -151,21 +101,13 @@ pagination:
151101 }
152102
153103 if ! result .RepositoryOwner .Repositories .PageInfo .HasNextPage {
154- if ! result .Search .PageInfo .HasNextPage {
155- break
156- }
104+ break
157105 }
158106
159107 variables ["endCursor" ] = githubv4 .String (result .RepositoryOwner .Repositories .PageInfo .EndCursor )
160- if hasArchivedFilter {
161- variables ["endCursor" ] = githubv4 .String (result .Search .PageInfo .EndCursor )
162- }
163108 }
164109
165110 totalCount = result .RepositoryOwner .Repositories .TotalCount
166- if hasArchivedFilter {
167- totalCount = result .Search .RepositoryCount
168- }
169111
170112 listResult := & RepositoryList {
171113 Repositories : repos ,
0 commit comments