From a6694ad3c76c88f57fd5bee8d6523c35f6767113 Mon Sep 17 00:00:00 2001 From: Jehiah Czebotar Date: Sat, 6 Jun 2015 14:03:59 -0400 Subject: [PATCH] github: handle users part of an Org not on a team --- providers/github.go | 57 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/providers/github.go b/providers/github.go index 4aaa2fe..3f8d342 100644 --- a/providers/github.go +++ b/providers/github.go @@ -49,7 +49,45 @@ func (p *GitHubProvider) SetOrgTeam(org, team string) { } } +func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) { + // https://developer.github.com/v3/orgs/#list-your-organizations + + var orgs []struct { + Login string `json:"login"` + } + + params := url.Values{ + "access_token": {accessToken}, + "limit": {"100"}, + } + + req, _ := http.NewRequest("GET", "https://api.github.com/user/orgs?"+params.Encode(), nil) + req.Header.Set("Accept", "application/vnd.github.moondragon+json") + resp, err := http.DefaultClient.Do(req) + if err != nil { + return false, err + } + + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + return false, err + } + + if err := json.Unmarshal(body, &orgs); err != nil { + return false, err + } + + for _, org := range orgs { + if p.Org == org.Login { + return true, nil + } + } + return false, nil +} + func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) { + // https://developer.github.com/v3/orgs/teams/#list-user-teams var teams []struct { Name string `json:"name"` @@ -61,6 +99,7 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) { params := url.Values{ "access_token": {accessToken}, + "limit": {"100"}, } req, _ := http.NewRequest("GET", "https://api.github.com/user/teams?"+params.Encode(), nil) @@ -97,17 +136,23 @@ func (p *GitHubProvider) GetEmailAddress(body []byte, access_token string) (stri Primary bool `json:"primary"` } - params := url.Values{ - "access_token": {access_token}, - } // if we require an Org or Team, check that first - if p.Org != "" || p.Team != "" { - if ok, err := p.hasOrgAndTeam(access_token); err != nil || !ok { - return "", err + if p.Org != "" { + if p.Team != "" { + if ok, err := p.hasOrgAndTeam(access_token); err != nil || !ok { + return "", err + } + } else { + if ok, err := p.hasOrg(access_token); err != nil || !ok { + return "", err + } } } + params := url.Values{ + "access_token": {access_token}, + } resp, err := http.DefaultClient.Get("https://api.github.com/user/emails?" + params.Encode()) if err != nil { return "", err