-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphlike.go
185 lines (175 loc) · 4.8 KB
/
graphlike.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"context"
"github.com/shurcooL/githubv4"
"log"
"net/http"
)
type teamInfo struct {
name string
orgId string
teamId string
description string
slug string
url string
access string
}
type samlNode struct {
User struct {
Login string
}
Guid string `graphql:"guid"`
SamlIdentity struct {
NameId string
}
}
type teamNode struct {
ID string
Name string
Description string
Slug string
URL string
}
// Checks if the user is SSO enabled
func userIsSSO(ctx context.Context, httpClient *http.Client, org string, login string) (bool, error) {
var q struct {
Organization struct {
SamlIdentityProvider struct {
ExternalIdentities struct {
Nodes []samlNode
PageInfo struct {
EndCursor githubv4.String
HasNextPage githubv4.Boolean
}
} `graphql:"externalIdentities(first: 100, after: $cursor)"`
}
} `graphql:"organization(login: $login)"`
}
variables := map[string]interface{}{
"login": githubv4.String(org),
"cursor": (*githubv4.String)(nil), // Null after argument to get first page.
}
client := githubv4.NewClient(httpClient)
for {
err := client.Query(ctx, &q, variables)
if err != nil {
log.Println("Got error querying SSO:", err)
return false, err
}
for _, node := range q.Organization.SamlIdentityProvider.ExternalIdentities.Nodes {
if node.User.Login == login {
// found the user
return true, nil
}
}
if !q.Organization.SamlIdentityProvider.ExternalIdentities.PageInfo.HasNextPage {
break
}
variables["cursor"] = githubv4.NewString(q.Organization.SamlIdentityProvider.ExternalIdentities.PageInfo.EndCursor)
}
return false, nil
}
func findUserByEmail(ctx context.Context, httpClient *http.Client, org string, email string) (string, error) {
var q struct {
Organization struct {
SamlIdentityProvider struct {
ExternalIdentities struct {
TotalCount githubv4.Int
Nodes []samlNode
} `graphql:"externalIdentities(first: 100, userName: $email, after: $cursor)"`
}
} `graphql:"organization(login: $login)"`
}
variables := map[string]interface{}{
"login": githubv4.String(org),
"email": githubv4.String(email),
"cursor": (*githubv4.String)(nil), // Null after argument to get first page.
}
client := githubv4.NewClient(httpClient)
for {
err := client.Query(ctx, &q, variables)
if err != nil {
log.Println("Got error querying email:", err)
return "", err
}
if q.Organization.SamlIdentityProvider.ExternalIdentities.TotalCount != 0 {
return q.Organization.SamlIdentityProvider.ExternalIdentities.Nodes[0].User.Login, nil
} else {
return "", nil
}
}
}
// Get the Team Details
//func getOrganisationAndTeamId(ctx context.Context, httpClient *http.Client,
// org string, teamName string) (*teamInfo, error) {
// var q struct {
// Organization struct {
// ID string
// Login string
// Teams struct {
// totalCount int64
// nodes []teamNode
// } `graphql:"teams(query: $teamName, first: 1)"`
// } `graphql:"organization(login: $login)"`
// }
// variables := map[string]interface{}{
// "login": githubv4.String(org),
// "teamName": githubv4.String(teamName),
// }
// client := githubv4.NewClient(httpClient)
// err := client.Query(ctx, &q, variables)
//
// if err != nil {
// log.Println("Got error querying Org/Team ID:", err)
// return nil, err
// }
// if q.Organization.Teams.totalCount != 1 {
// if q.Organization.Teams.totalCount == 0 {
// log.Println("Team", teamName, " not found:")
// } else {
// log.Println("Got ", q.Organization.Teams.totalCount, " matches for Team ", teamName)
// }
// return nil, err
// }
// team := teamInfo{name: teamName,
// orgId: q.Organization.ID,
// teamId: q.Organization.Teams.nodes[0].ID}
// return &team, nil
//}
// Get a Users Teams
func getUserTeams(ctx context.Context, httpClient *http.Client,
org string, userLogin string) ([]teamInfo, error) {
var q struct {
Organization struct {
ID string
Login string
Teams struct {
TotalCount int64
Nodes []teamNode
} `graphql:"teams(first: $first, userLogins: $userLogin)"`
} `graphql:"organization(login: $org)"`
}
variables := map[string]interface{}{
"org": githubv4.String(org),
"userLogin": []githubv4.String{githubv4.String(userLogin)},
"first": githubv4.Int(100),
}
client := githubv4.NewClient(httpClient)
err := client.Query(ctx, &q, variables)
if err != nil {
log.Println("Got error querying Team Lists:", err)
return nil, err
}
var teams []teamInfo
for _, team := range q.Organization.Teams.Nodes {
teams = append(teams, teamInfo{
name: team.Name,
orgId: q.Organization.ID,
teamId: team.ID,
slug: team.Slug,
description: team.Description,
url: team.URL,
})
}
return teams, nil
}