This commit is contained in:
Ali Javadi 2017-01-27 01:45:01 +03:30
parent 48fcf66a35
commit 98bfa4fbb1

View file

@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"regexp"
"strconv" "strconv"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -221,7 +222,17 @@ func (c *githubConnector) user(ctx context.Context, client *http.Client) (user,
// The HTTP passed client is expected to be constructed by the golang.org/x/oauth2 package, // The HTTP passed client is expected to be constructed by the golang.org/x/oauth2 package,
// which inserts a bearer token as part of the request. // which inserts a bearer token as part of the request.
func (c *githubConnector) teams(ctx context.Context, client *http.Client, org string) ([]string, error) { func (c *githubConnector) teams(ctx context.Context, client *http.Client, org string) ([]string, error) {
req, err := http.NewRequest("GET", baseURL+"/user/teams", nil)
groups := []string{}
// https://developer.github.com/v3/#pagination
reNext := regexp.MustCompile("<(.*)>; rel=\"next\"")
reLast := regexp.MustCompile("<(.*)>; rel=\"last\"")
apiURL := baseURL + "/user/teams"
for {
req, err := http.NewRequest("GET", apiURL, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("github: new req: %v", err) return nil, fmt.Errorf("github: new req: %v", err)
} }
@ -250,11 +261,28 @@ func (c *githubConnector) teams(ctx context.Context, client *http.Client, org st
if err := json.NewDecoder(resp.Body).Decode(&teams); err != nil { if err := json.NewDecoder(resp.Body).Decode(&teams); err != nil {
return nil, fmt.Errorf("github: unmarshal groups: %v", err) return nil, fmt.Errorf("github: unmarshal groups: %v", err)
} }
groups := []string{}
for _, team := range teams { for _, team := range teams {
if team.Org.Login == org { if team.Org.Login == org {
groups = append(groups, team.Name) groups = append(groups, team.Name)
} }
} }
links := resp.Header.Get("Link")
if len(reLast.FindStringSubmatch(links)) > 1 {
lastPageURL := reLast.FindStringSubmatch(links)[1]
if apiURL == lastPageURL {
break
}
} else {
break
}
if len(reNext.FindStringSubmatch(links)) > 1 {
apiURL = reNext.FindStringSubmatch(links)[1]
} else {
break
}
}
return groups, nil return groups, nil
} }