14type GitHubProvider struct {
20func NewGitHubProvider(p *ProviderData) *GitHubProvider {
21 p.ProviderName = "GitHub"
22 if p.LoginURL == nil || p.LoginURL.String() == "" {
23 p.LoginURL = &url.URL{
26 Path: "/login/oauth/authorize",
29 if p.RedeemURL == nil || p.RedeemURL.String() == "" {
30 p.RedeemURL = &url.URL{
33 Path: "/login/oauth/access_token",
36 // ValidationURL is the API Base URL
37 if p.ValidateURL == nil || p.ValidateURL.String() == "" {
38 p.ValidateURL = &url.URL{
40 Host: "api.github.com",
45 p.Scope = "user:email"
47 return &GitHubProvider{ProviderData: p}
49func (p *GitHubProvider) SetOrgTeam(org, team string) {
52 if org != "" || team != "" {
53 p.Scope += " read:org"
57func (p *GitHubProvider) hasOrg(accessToken string) (bool, error) {
58 // https://developer.github.com/v3/orgs/#list-your-organizations
61 Login string `json:"login"`
65 "access_token": {accessToken},
70 Scheme: p.ValidateURL.Scheme,
71 Host: p.ValidateURL.Host,
72 Path: path.Join(p.ValidateURL.Path, "/user/orgs"),
73 RawQuery: params.Encode(),
75 req, _ := http.NewRequest("GET", endpoint.String(), nil)
76 req.Header.Set("Accept", "application/vnd.github.v3+json")
77 resp, err := http.DefaultClient.Do(req)
82 body, err := ioutil.ReadAll(resp.Body)
87 if resp.StatusCode != 200 {
88 return false, fmt.Errorf("got %d from %q %s", resp.StatusCode, endpoint, body)
91 if err := json.Unmarshal(body, &orgs); err != nil {
95 var presentOrgs []string
96 for _, org := range orgs {
97 if p.Org == org.Login {
98 log.Printf("Found Github Organization: %q", org.Login)
101 presentOrgs = append(presentOrgs, org.Login)
104 log.Printf("Missing Organization:%q in %v", p.Org, presentOrgs)
108func (p *GitHubProvider) hasOrgAndTeam(accessToken string) (bool, error) {
109 // https://developer.github.com/v3/orgs/teams/#list-user-teams
112 Name string `json:"name"`
113 Slug string `json:"slug"`
115 Login string `json:"login"`
116 } `json:"organization"`
119 params := url.Values{
120 "access_token": {accessToken},
124 endpoint := &url.URL{
125 Scheme: p.ValidateURL.Scheme,
126 Host: p.ValidateURL.Host,
127 Path: path.Join(p.ValidateURL.Path, "/user/teams"),
128 RawQuery: params.Encode(),
130 req, _ := http.NewRequest("GET", endpoint.String(), nil)
131 req.Header.Set("Accept", "application/vnd.github.v3+json")
132 resp, err := http.DefaultClient.Do(req)
137 body, err := ioutil.ReadAll(resp.Body)
142 if resp.StatusCode != 200 {
143 return false, fmt.Errorf("got %d from %q %s", resp.StatusCode, endpoint, body)
146 if err := json.Unmarshal(body, &teams); err != nil {
147 return false, fmt.Errorf("%s unmarshaling %s", err, body)
151 presentOrgs := make(map[string]bool)
152 var presentTeams []string
153 for _, team := range teams {
154 presentOrgs[team.Org.Login] = true
155 if p.Org == team.Org.Login {
157 ts := strings.Split(p.Team, ",")
158 for _, t := range ts {
160 log.Printf("Found Github Organization:%q Team:%q (Name:%q)", team.Org.Login, team.Slug, team.Name)
164 presentTeams = append(presentTeams, team.Slug)
168 log.Printf("Missing Team:%q from Org:%q in teams: %v", p.Team, p.Org, presentTeams)
171 for org, _ := range presentOrgs {
172 allOrgs = append(allOrgs, org)
174 log.Printf("Missing Organization:%q in %#v", p.Org, allOrgs)
179func (p *GitHubProvider) GetEmailAddress(s *SessionState) (string, error) {
181 var emails []struct {
182 Email string `json:"email"`
183 Primary bool `json:"primary"`
186 // if we require an Org or Team, check that first
189 if ok, err := p.hasOrgAndTeam(s.AccessToken); err != nil || !ok {
193 if ok, err := p.hasOrg(s.AccessToken); err != nil || !ok {
199 params := url.Values{
200 "access_token": {s.AccessToken},
203 endpoint := &url.URL{
204 Scheme: p.ValidateURL.Scheme,
205 Host: p.ValidateURL.Host,
206 Path: path.Join(p.ValidateURL.Path, "/user/emails"),
207 RawQuery: params.Encode(),
209 resp, err := http.DefaultClient.Get(endpoint.String())
213 body, err := ioutil.ReadAll(resp.Body)
219 if resp.StatusCode != 200 {
220 return "", fmt.Errorf("got %d from %q %s", resp.StatusCode, endpoint, body)
222 log.Printf("got %d from %q %s", resp.StatusCode, endpoint, body)
225 if err := json.Unmarshal(body, &emails); err != nil {
226 return "", fmt.Errorf("%s unmarshaling %s", err, body)
229 for _, email := range emails {
231 return email.Email, nil
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list