@@ -2,7 +2,6 @@ package userlist
22
33import (
44 "context"
5- "encoding/json"
65 "fmt"
76 "github.com/shurcooL/githubv4"
87 "golang.org/x/oauth2"
@@ -46,6 +45,10 @@ func (c *UserListConfig) loadCollaborators() error {
4645 Login string
4746 Name string
4847 }
48+ PageInfo struct {
49+ HasNextPage bool
50+ EndCursor githubv4.String
51+ }
4952 } `graphql:"organizations(first:100)"`
5053 } `graphql:"enterprise(slug: $slug)"`
5154 }
@@ -62,6 +65,11 @@ func (c *UserListConfig) loadCollaborators() error {
6265 }
6366 slog .Info ("Loaded organizations" , "organization.count" , len (organizations .Enterprise .Organizations .Nodes ))
6467
68+ if organizations .Enterprise .Organizations .PageInfo .HasNextPage {
69+ slog .Warn ("More organizations available - not yet implemented" )
70+ c .userList .addWarning ("More organizations available - not yet implemented" )
71+ }
72+
6573 /*
6674 {
6775 organization(login:"prodyna") {
@@ -92,6 +100,7 @@ func (c *UserListConfig) loadCollaborators() error {
92100
93101 userNumber := 0
94102 slog .Info ("Iterating organizatons" , "organization.count" , len (organizations .Enterprise .Organizations .Nodes ))
103+
95104 for _ , org := range organizations .Enterprise .Organizations .Nodes {
96105 slog .Info ("Loading repositories and external collaborators" , "organization" , org .Login )
97106 var query struct {
@@ -110,82 +119,95 @@ func (c *UserListConfig) loadCollaborators() error {
110119 }
111120 }
112121 }
122+ PageInfo struct {
123+ HasNextPage bool
124+ EndCursor githubv4.String
125+ }
113126 } `graphql:"collaborators(first:100,affiliation:OUTSIDE)"`
114127 }
115- } `graphql:"repositories(first:100)"`
128+ PageInfo struct {
129+ HasNextPage bool
130+ EndCursor githubv4.String
131+ }
132+ } `graphql:"repositories(first:$first,after:$after)"`
116133 } `graphql:"organization(login: $organization)"`
117134 }
118135
119136 variables := map [string ]interface {}{
120137 "organization" : githubv4 .String (org .Login ),
138+ "first" : githubv4 .Int (100 ),
139+ "after" : (* githubv4 .String )(nil ),
121140 }
122141
123- err := client .Query (ctx , & query , variables )
124- if err != nil {
125- slog .WarnContext (ctx , "Unable to query - will skip this organization" , "error" , err , "organization" , org .Login )
126- continue
127- }
142+ for {
143+ err := client .Query (ctx , & query , variables )
144+ if err != nil {
145+ slog .WarnContext (ctx , "Unable to query - will skip this organization" , "error" , err , "organization" , org .Login )
146+ c .userList .addWarning (fmt .Sprintf ("Unable to query organization %s" , org .Login ))
147+ break
148+ }
128149
129- // count the collaborators
130- collaboratorCount := 0
131- for _ , repo := range query .Organization .Repositories .Nodes {
132- collaboratorCount += len (repo .Collaborators .Nodes )
133- }
134- if collaboratorCount == 0 {
135- slog .DebugContext (ctx , "No collaborators found" , "organization" , org .Login )
136- continue
137- }
150+ // count the collaborators
151+ collaboratorCount := 0
152+ for _ , repo := range query .Organization .Repositories .Nodes {
153+ collaboratorCount += len (repo .Collaborators .Nodes )
154+ }
155+ if collaboratorCount == 0 {
156+ slog .DebugContext (ctx , "No collaborators found" , "organization" , org .Login )
157+ break
158+ }
138159
139- for _ , repo := range query .Organization .Repositories .Nodes {
140- slog .DebugContext (ctx , "Processing repository" , "repository" , repo .Name , "collaborator.count" , len (repo .Collaborators .Nodes ))
141- for _ , collaborator := range repo .Collaborators .Nodes {
142- slog .DebugContext (ctx , "Processing collaborator" , "login" , collaborator .Login , "name" , collaborator .Name , "contributions" , collaborator .ContributionsCollection .ContributionCalendar .TotalContributions )
143-
144- // User
145- user := c .userList .findUser (collaborator .Login )
146- if user == nil {
147- user = c .userList .createUser (userNumber + 1 , collaborator .Login , collaborator .Name , "" , collaborator .ContributionsCollection .ContributionCalendar .TotalContributions )
148- userNumber ++
149- } else {
150- slog .Info ("Found existing user" , "login" , user .Login )
151- }
160+ for _ , repo := range query .Organization .Repositories .Nodes {
161+ slog .DebugContext (ctx , "Processing repository" , "repository" , repo .Name , "collaborator.count" , len (repo .Collaborators .Nodes ))
162+ for _ , collaborator := range repo .Collaborators .Nodes {
163+ slog .DebugContext (ctx , "Processing collaborator" , "login" , collaborator .Login , "name" , collaborator .Name , "contributions" , collaborator .ContributionsCollection .ContributionCalendar .TotalContributions )
164+
165+ // User
166+ user := c .userList .findUser (collaborator .Login )
167+ if user == nil {
168+ user = c .userList .createUser (userNumber + 1 , collaborator .Login , collaborator .Name , "" , collaborator .ContributionsCollection .ContributionCalendar .TotalContributions )
169+ userNumber ++
170+ } else {
171+ slog .Info ("Found existing user" , "login" , user .Login )
172+ }
152173
153- // Organization
154- organization := user .findOrganization (org .Login )
155- if organization == nil {
156- organization = user .createOrganization (org .Login , org .Name )
157- } else {
158- slog .Info ("Found existing organization" , "organization" , organization .Name )
159- }
174+ // Organization
175+ organization := user .findOrganization (org .Login )
176+ if organization == nil {
177+ organization = user .createOrganization (org .Login , org .Name )
178+ } else {
179+ slog .Info ("Found existing organization" , "organization" , organization .Name )
180+ }
160181
161- // Repository
162- repository := organization .findRepository (repo .Name )
163- if repository == nil {
164- repository = organization .createRepository (repo .Name )
165- } else {
166- slog .Info ("Found existing repository" , "repository" , repository .Name )
182+ // Repository
183+ repository := organization .findRepository (repo .Name )
184+ if repository == nil {
185+ repository = organization .createRepository (repo .Name )
186+ } else {
187+ slog .Info ("Found existing repository" , "repository" , repository .Name )
188+ }
189+ organization .upsertRepository (* repository )
167190 }
168- organization .upsertRepository (* repository )
169191 }
170- }
171192
172- slog .InfoContext (ctx , "Loaded repositories" ,
173- "repository.count" , len (query .Organization .Repositories .Nodes ),
174- "organization" , org .Login ,
175- "collaborator.count" , collaboratorCount )
193+ slog .InfoContext (ctx , "Loaded repositories" ,
194+ "repository.count" , len (query .Organization .Repositories .Nodes ),
195+ "organization" , org .Login ,
196+ "collaborator.count" , collaboratorCount )
176197
177- if collaboratorCount == 0 {
178- continue
179- }
198+ if collaboratorCount == 0 {
199+ continue
200+ }
180201
181- output , err := json .MarshalIndent (c .userList , "" , " " )
182- if err != nil {
183- slog .ErrorContext (ctx , "Unable to marshal json" , "error" , err )
184- return err
185- }
186- fmt .Printf ("%s\n " , output )
202+ slog .InfoContext (ctx , "Adding collaborators" , "organization" , org .Login )
187203
188- slog .InfoContext (ctx , "Adding collaborators" , "organization" , org .Login )
204+ if ! query .Organization .Repositories .PageInfo .HasNextPage {
205+ break
206+ }
207+
208+ slog .Info ("More repositories available" , "organization" , org .Login , "after" , query .Organization .Repositories .PageInfo .EndCursor )
209+ variables ["after" ] = githubv4 .NewString (query .Organization .Repositories .PageInfo .EndCursor )
210+ }
189211 }
190212
191213 c .loaded = true
0 commit comments