Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
github: handle users part of an Org not on a team
Browse files Browse the repository at this point in the history
  • Loading branch information
jehiah committed Jun 6, 2015
1 parent b313e99 commit a6694ad
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions providers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a6694ad

Please sign in to comment.