Merge pull request #109 from jehiah/github_org_109

Github provider "Invalid Account"
This commit is contained in:
Jehiah Czebotar 2015-06-06 14:54:01 -04:00
commit 13e82923f0

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) { func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
// https://developer.github.com/v3/orgs/teams/#list-user-teams
var teams []struct { var teams []struct {
Name string `json:"name"` Name string `json:"name"`
@ -61,6 +99,7 @@ func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
params := url.Values{ params := url.Values{
"access_token": {accessToken}, "access_token": {accessToken},
"limit": {"100"},
} }
req, _ := http.NewRequest("GET", "https://api.github.com/user/teams?"+params.Encode(), nil) 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"` Primary bool `json:"primary"`
} }
params := url.Values{
"access_token": {access_token},
}
// if we require an Org or Team, check that first // if we require an Org or Team, check that first
if p.Org != "" || p.Team != "" { if p.Org != "" {
if ok, err := p.hasOrgAndTeam(access_token); err != nil || !ok { if p.Team != "" {
return "", err 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()) resp, err := http.DefaultClient.Get("https://api.github.com/user/emails?" + params.Encode())
if err != nil { if err != nil {
return "", err return "", err