@@ -3,6 +3,7 @@ package api
33import (
44 "bytes"
55 "encoding/json"
6+ "errors"
67 "fmt"
78 "io"
89 "io/ioutil"
@@ -195,19 +196,18 @@ func (err HTTPError) Error() string {
195196 return fmt .Sprintf ("HTTP %d (%s)" , err .StatusCode , err .RequestURL )
196197}
197198
198- // Returns whether or not scopes are present, appID, and error
199- func ( c Client ) HasScopes ( wantedScopes ... string ) ( bool , string , error ) {
200- url := "https://api.github.com/user"
201- req , err := http .NewRequest ("GET" , url , nil )
199+ func ( c Client ) HasMinimumScopes ( hostname string ) ( bool , error ) {
200+ apiEndpoint := ghinstance . RESTPrefix ( hostname )
201+
202+ req , err := http .NewRequest ("GET" , apiEndpoint , nil )
202203 if err != nil {
203- return false , "" , err
204+ return false , err
204205 }
205206
206207 req .Header .Set ("Content-Type" , "application/json; charset=utf-8" )
207-
208208 res , err := c .http .Do (req )
209209 if err != nil {
210- return false , "" , err
210+ return false , err
211211 }
212212
213213 defer func () {
@@ -218,26 +218,36 @@ func (c Client) HasScopes(wantedScopes ...string) (bool, string, error) {
218218 }()
219219
220220 if res .StatusCode != 200 {
221- return false , "" , handleHTTPError (res )
221+ return false , handleHTTPError (res )
222222 }
223223
224- appID := res .Header .Get ("X-Oauth-Client-Id" )
225224 hasScopes := strings .Split (res .Header .Get ("X-Oauth-Scopes" ), "," )
226225
227- found := 0
226+ search := map [string ]bool {
227+ "repo" : false ,
228+ "read:org" : false ,
229+ "admin:org" : false ,
230+ }
231+
228232 for _ , s := range hasScopes {
229- for _ , w := range wantedScopes {
230- if w == strings .TrimSpace (s ) {
231- found ++
232- }
233- }
233+ search [strings .TrimSpace (s )] = true
234234 }
235235
236- if found == len (wantedScopes ) {
237- return true , appID , nil
236+ errorMsgs := []string {}
237+ if ! search ["repo" ] {
238+ errorMsgs = append (errorMsgs , "missing required scope 'repo'" )
238239 }
239240
240- return false , appID , nil
241+ if ! search ["read:org" ] && ! search ["admin:org" ] {
242+ errorMsgs = append (errorMsgs , "missing required scope 'read:org'" )
243+ }
244+
245+ if len (errorMsgs ) > 0 {
246+ return false , errors .New (strings .Join (errorMsgs , ";" ))
247+ }
248+
249+ return true , nil
250+
241251}
242252
243253// GraphQL performs a GraphQL request and parses the response
0 commit comments