@@ -89,6 +89,27 @@ func makeGitHubHeader(accessToken string) http.Header {
8989 return makeAuthorizationHeader(tokenTypeToken, accessToken, extraHeaders)
9090}
9191
92+func (p *GitHubProvider) makeGitHubAPIEndpoint(endpoint string, params *url.Values) *url.URL {
93+ basePath := p.ValidateURL.Path
94+
95+ re := regexp.MustCompile(`^/api/v\d+`)
96+ match := re.FindString(p.ValidateURL.Path)
97+ if match != "" {
98+ basePath = match
99+ }
100+
101+ if params == nil {
102+ params = &url.Values{}
103+ }
104+
105+ return &url.URL{
106+ Scheme: p.ValidateURL.Scheme,
107+ Host: p.ValidateURL.Host,
108+ Path: path.Join(basePath, endpoint),
109+ RawQuery: params.Encode(),
110+ }
111+}
112+
92113// setOrgTeam adds GitHub org reading parameters to the OAuth2 scope
93114func (p *GitHubProvider) setOrgTeam(org, team string) {
94115 p.Org = org
@@ -141,12 +162,7 @@ func (p *GitHubProvider) hasOrg(ctx context.Context, accessToken string) (bool,
141162 "page": {strconv.Itoa(pn)},
142163 }
143164
144− endpoint := &url.URL{
145− Scheme: p.ValidateURL.Scheme,
146− Host: p.ValidateURL.Host,
147− Path: path.Join(p.ValidateURL.Path, "/user/orgs"),
148− RawQuery: params.Encode(),
149− }
165+ endpoint := p.makeGitHubAPIEndpoint("/user/orgs", ¶ms)
150166
151167 var op orgsPage
152168 err := requests.New(endpoint.String()).
@@ -206,12 +222,7 @@ func (p *GitHubProvider) hasOrgAndTeam(ctx context.Context, accessToken string)
206222 "page": {strconv.Itoa(pn)},
207223 }
208224
209− endpoint := &url.URL{
210− Scheme: p.ValidateURL.Scheme,
211− Host: p.ValidateURL.Host,
212− Path: path.Join(p.ValidateURL.Path, "/user/teams"),
213− RawQuery: params.Encode(),
214− }
225+ endpoint := p.makeGitHubAPIEndpoint("/user/teams", ¶ms)
215226
216227 // bodyclose cannot detect that the body is being closed later in requests.Into,
217228 // so have to skip the linting for the next line.
@@ -309,11 +320,7 @@ func (p *GitHubProvider) hasRepo(ctx context.Context, accessToken string) (bool,
309320 Private bool `json:"private"`
310321 }
311322
312− endpoint := &url.URL{
313− Scheme: p.ValidateURL.Scheme,
314− Host: p.ValidateURL.Host,
315− Path: path.Join(p.ValidateURL.Path, "/repos/", p.Repo),
316− }
323+ endpoint := p.makeGitHubAPIEndpoint("/repos/"+p.Repo, nil)
317324
318325 var repo repository
319326 err := requests.New(endpoint.String()).
@@ -338,11 +345,7 @@ func (p *GitHubProvider) hasUser(ctx context.Context, accessToken string) (bool,
338345 Email string `json:"email"`
339346 }
340347
341− endpoint := &url.URL{
342− Scheme: p.ValidateURL.Scheme,
343− Host: p.ValidateURL.Host,
344− Path: path.Join(p.ValidateURL.Path, "/user"),
345− }
348+ endpoint := p.makeGitHubAPIEndpoint("/user", nil)
346349
347350 err := requests.New(endpoint.String()).
348351 WithContext(ctx).
@@ -362,11 +365,7 @@ func (p *GitHubProvider) hasUser(ctx context.Context, accessToken string) (bool,
362365func (p *GitHubProvider) isCollaborator(ctx context.Context, username, accessToken string) (bool, error) {
363366 //https://developer.github.com/v3/repos/collaborators/#check-if-a-user-is-a-collaborator
364367
365− endpoint := &url.URL{
366− Scheme: p.ValidateURL.Scheme,
367− Host: p.ValidateURL.Host,
368− Path: path.Join(p.ValidateURL.Path, "/repos/", p.Repo, "/collaborators/", username),
369− }
368+ endpoint := p.makeGitHubAPIEndpoint("/repos/"+p.Repo+"/collaborators/"+username, nil)
370369 result := requests.New(endpoint.String()).
371370 WithContext(ctx).
372371 WithHeaders(makeGitHubHeader(accessToken)).
@@ -426,11 +425,7 @@ func (p *GitHubProvider) getEmail(ctx context.Context, s *sessions.SessionState)
426425 }
427426 }
428427
429− endpoint := &url.URL{
430− Scheme: p.ValidateURL.Scheme,
431− Host: p.ValidateURL.Host,
432− Path: path.Join(p.ValidateURL.Path, "/user/emails"),
433− }
428+ endpoint := p.makeGitHubAPIEndpoint("/user/emails", nil)
434429 err := requests.New(endpoint.String()).
435430 WithContext(ctx).
436431 WithHeaders(makeGitHubHeader(s.AccessToken)).
@@ -459,11 +454,7 @@ func (p *GitHubProvider) getUser(ctx context.Context, s *sessions.SessionState)
459454 Email string `json:"email"`
460455 }
461456
462− endpoint := &url.URL{
463− Scheme: p.ValidateURL.Scheme,
464− Host: p.ValidateURL.Host,
465− Path: path.Join(p.ValidateURL.Path, "/user"),
466− }
457+ endpoint := p.makeGitHubAPIEndpoint("/user", nil)
467458
468459 err := requests.New(endpoint.String()).
469460 WithContext(ctx).