8
8
"net/http"
9
9
"net/url"
10
10
"path"
11
+ "regexp"
11
12
"strconv"
12
13
"strings"
13
14
)
@@ -69,7 +70,7 @@ func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) {
69
70
pn := 1
70
71
for {
71
72
params := url.Values {
72
- "limit" : {"200 " },
73
+ "limit" : {"100 " },
73
74
"page" : {strconv .Itoa (pn )},
74
75
}
75
76
@@ -134,7 +135,7 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
134
135
}
135
136
136
137
params := url.Values {
137
- "limit" : {"200 " },
138
+ "limit" : {"100 " },
138
139
}
139
140
140
141
endpoint := & url.URL {
@@ -143,45 +144,58 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
143
144
Path : path .Join (p .ValidateURL .Path , "/user/teams" ),
144
145
RawQuery : params .Encode (),
145
146
}
146
- req , _ := http .NewRequest ("GET" , endpoint .String (), nil )
147
- req .Header .Set ("Accept" , "application/vnd.github.v3+json" )
148
- req .Header .Set ("Authorization" , fmt .Sprintf ("token %s" , accessToken ))
149
- resp , err := http .DefaultClient .Do (req )
150
- if err != nil {
151
- return false , err
152
- }
153
-
154
- body , err := ioutil .ReadAll (resp .Body )
155
- resp .Body .Close ()
156
- if err != nil {
157
- return false , err
158
- }
159
- if resp .StatusCode != 200 {
160
- return false , fmt .Errorf (
161
- "got %d from %q %s" , resp .StatusCode , endpoint .String (), body )
162
- }
163
-
164
- if err := json .Unmarshal (body , & teams ); err != nil {
165
- return false , fmt .Errorf ("%s unmarshaling %s" , err , body )
166
- }
147
+ team_url := endpoint .String ()
167
148
149
+ pattern := regexp .MustCompile (`<([^>]+)>; rel="next"` )
168
150
var hasOrg bool
169
151
presentOrgs := make (map [string ]bool )
170
152
var presentTeams []string
171
- for _ , team := range teams {
172
- presentOrgs [team .Org .Login ] = true
173
- if p .Org == team .Org .Login {
174
- hasOrg = true
175
- ts := strings .Split (p .Team , "," )
176
- for _ , t := range ts {
177
- if t == team .Slug {
178
- log .Printf ("Found Github Organization:%q Team:%q (Name:%q)" , team .Org .Login , team .Slug , team .Name )
179
- return true , nil
153
+ for {
154
+ req , _ := http .NewRequest ("GET" , team_url , nil )
155
+ req .Header .Set ("Accept" , "application/vnd.github.hellcat-preview+json" )
156
+ req .Header .Set ("Authorization" , fmt .Sprintf ("token %s" , accessToken ))
157
+ resp , err := http .DefaultClient .Do (req )
158
+ if err != nil {
159
+ return false , err
160
+ }
161
+
162
+ body , err := ioutil .ReadAll (resp .Body )
163
+ resp .Body .Close ()
164
+ if err != nil {
165
+ return false , err
166
+ }
167
+ if resp .StatusCode != 200 {
168
+ return false , fmt .Errorf (
169
+ "got %d from %q %s" , resp .StatusCode , endpoint .String (), body )
170
+ }
171
+
172
+ if err := json .Unmarshal (body , & teams ); err != nil {
173
+ return false , fmt .Errorf ("%s unmarshaling %s" , err , body )
174
+ }
175
+
176
+ for _ , team := range teams {
177
+ presentOrgs [team .Org .Login ] = true
178
+ if p .Org == team .Org .Login {
179
+ hasOrg = true
180
+ ts := strings .Split (p .Team , "," )
181
+ for _ , t := range ts {
182
+ if t == team .Slug {
183
+ log .Printf ("Found Github Organization:%q Team:%q (Name:%q)" ,
184
+ team .Org .Login , team .Slug , team .Name )
185
+ return true , nil
186
+ }
180
187
}
188
+ presentTeams = append (presentTeams , team .Slug )
181
189
}
182
- presentTeams = append (presentTeams , team .Slug )
183
190
}
191
+
192
+ matches := pattern .FindStringSubmatch (resp .Header ["Link" ][0 ])
193
+ if len (matches ) == 0 {
194
+ break
195
+ }
196
+ team_url = matches [1 ]
184
197
}
198
+
185
199
if hasOrg {
186
200
log .Printf ("Missing Team:%q from Org:%q in teams: %v" , p .Team , p .Org , presentTeams )
187
201
} else {
0 commit comments