-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathall_org_users.go
More file actions
74 lines (62 loc) · 1.57 KB
/
all_org_users.go
File metadata and controls
74 lines (62 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
type member struct {
Login string
}
func main() {
org := flag.String("org", "rtCamp", "Name of a GitHub Organization")
flag.Parse()
members := fetchUsers(context.Background(), *org, 100)
for i := 0; i < len(members); i++ {
fmt.Println(members[i].Login)
}
}
func fetchUsers(ctx context.Context, orgName string, numberOfMembers int) []member {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := githubv4.NewClient(httpClient)
var query struct {
RateLimit struct {
Cost int
Remaining int
ResetAt time.Time
}
Organization struct {
Members struct {
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
}
Nodes []member
} `graphql:"members(first: $numberOfMembers, after: $membersCursor)"`
} `graphql:"organization(login: $orgName)"`
}
variables := map[string]interface{}{
"orgName": githubv4.String(orgName),
"numberOfMembers": githubv4.Int(numberOfMembers),
"membersCursor": (*githubv4.String)(nil),
}
var allMembers []member
for {
err := client.Query(ctx, &query, variables)
if err != nil {
panic(err)
}
allMembers = append(allMembers, query.Organization.Members.Nodes...)
if !query.Organization.Members.PageInfo.HasNextPage {
break
}
variables["membersCursor"] = githubv4.NewString(query.Organization.Members.PageInfo.EndCursor)
}
return allMembers
}