github: handle users part of an Org not on a team

This commit is contained in:
Jehiah Czebotar 2015-06-06 14:03:59 -04:00
parent b313e99352
commit a6694ad3c7

View File

@ -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