@@ -3,6 +3,7 @@ package list
33import (
44 "context"
55 "net/http"
6+ "reflect"
67 "strings"
78 "time"
89
@@ -39,31 +40,18 @@ func (r Repository) Info() string {
3940}
4041
4142type RepositoryList struct {
43+ Owner string
4244 Repositories []Repository
4345 TotalCount int
4446}
4547
4648func listRepos (client * http.Client , hostname string , limit int , owner string , filter FilterOptions ) (* RepositoryList , error ) {
47- type query struct {
48- RepositoryOwner struct {
49- Repositories struct {
50- Nodes []Repository
51- TotalCount int
52- PageInfo struct {
53- HasNextPage bool
54- EndCursor string
55- }
56- } `graphql:"repositories(first: $perPage, after: $endCursor, privacy: $privacy, isFork: $fork, ownerAffiliations: OWNER, orderBy: { field: PUSHED_AT, direction: DESC })"`
57- } `graphql:"repositoryOwner(login: $owner)"`
58- }
59-
6049 perPage := limit
6150 if perPage > 100 {
6251 perPage = 100
6352 }
6453
6554 variables := map [string ]interface {}{
66- "owner" : githubv4 .String (owner ),
6755 "perPage" : githubv4 .Int (perPage ),
6856 "endCursor" : (* githubv4 .String )(nil ),
6957 }
@@ -82,28 +70,58 @@ func listRepos(client *http.Client, hostname string, limit int, owner string, fi
8270 variables ["fork" ] = (* githubv4 .Boolean )(nil )
8371 }
8472
73+ var ownerConnection string
74+ if owner == "" {
75+ ownerConnection = `graphql:"repositoryOwner: viewer"`
76+ } else {
77+ ownerConnection = `graphql:"repositoryOwner(login: $owner)"`
78+ variables ["owner" ] = githubv4 .String (owner )
79+ }
80+
81+ type repositoryOwner struct {
82+ Login string
83+ Repositories struct {
84+ Nodes []Repository
85+ TotalCount int
86+ PageInfo struct {
87+ HasNextPage bool
88+ EndCursor string
89+ }
90+ } `graphql:"repositories(first: $perPage, after: $endCursor, privacy: $privacy, isFork: $fork, ownerAffiliations: OWNER, orderBy: { field: PUSHED_AT, direction: DESC })"`
91+ }
92+ query := reflect .StructOf ([]reflect.StructField {
93+ {
94+ Name : "RepositoryOwner" ,
95+ Type : reflect .TypeOf (repositoryOwner {}),
96+ Tag : reflect .StructTag (ownerConnection ),
97+ },
98+ })
99+
85100 listResult := RepositoryList {}
86101pagination:
87102 for {
88- var result query
103+ result := reflect . New ( query )
89104 gql := graphql .NewClient (ghinstance .GraphQLEndpoint (hostname ), client )
90- err := gql .QueryNamed (context .Background (), "RepositoryList" , & result , variables )
105+ err := gql .QueryNamed (context .Background (), "RepositoryList" , result . Interface () , variables )
91106 if err != nil {
92107 return nil , err
93108 }
94109
95- listResult .TotalCount = result .RepositoryOwner .Repositories .TotalCount
96- for _ , repo := range result .RepositoryOwner .Repositories .Nodes {
110+ owner := result .Elem ().FieldByName ("RepositoryOwner" ).Interface ().(repositoryOwner )
111+ listResult .TotalCount = owner .Repositories .TotalCount
112+ listResult .Owner = owner .Login
113+
114+ for _ , repo := range owner .Repositories .Nodes {
97115 listResult .Repositories = append (listResult .Repositories , repo )
98116 if len (listResult .Repositories ) >= limit {
99117 break pagination
100118 }
101119 }
102120
103- if ! result . RepositoryOwner .Repositories .PageInfo .HasNextPage {
121+ if ! owner .Repositories .PageInfo .HasNextPage {
104122 break
105123 }
106- variables ["endCursor" ] = githubv4 .String (result . RepositoryOwner .Repositories .PageInfo .EndCursor )
124+ variables ["endCursor" ] = githubv4 .String (owner .Repositories .PageInfo .EndCursor )
107125 }
108126
109127 return & listResult , nil
0 commit comments