Skip to content

Commit

Permalink
PullRequest Resource: Derive API URL from PR URL.
Browse files Browse the repository at this point in the history
Currently, we always use the default API URL for all requests, which
means that the PR resource cannot be used with GitHub Enterprise or self
hosted GitLab instances. This changes the behavior to build the API Base
URL from the PR resource if the URL is not the default:

PR URL | API Base URL
-------|-------------
https://github.com | https://api.github.com
https://github.example.com | https://github.example.com/v3/api
https://gitlab.com | https://gitlab.com
https://gitlab.example.com | https://gitlab.example.com

GitHub Documentation: https://developer.github.com/v3/#schema
GitHub Enterprise Documentation:
https://developer.github.com/enterprise/2.17/v3/#schema
GitLab Documentation: https://developer.github.com/enterprise/2.17/v3/#current-version

Fixes tektoncd#1816
  • Loading branch information
wlynch committed Jan 7, 2020
1 parent f9e303a commit c386a3b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
14 changes: 14 additions & 0 deletions pkg/pullrequest/scm.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ func githubHandlerFromURL(u *url.URL, token string, logger *zap.SugaredLogger) (
)

client := github.NewDefault()
if u.Host != "github.com" {
var err error
client, err = github.New(fmt.Sprintf("%s://%s/api/v3", u.Scheme, u.Host))
if err != nil {
return nil, fmt.Errorf("error creating client: %w", err)
}
}
ownerRepo := fmt.Sprintf("%s/%s", owner, repo)
h := NewHandler(logger, client, ownerRepo, prNumber)
if token != "" {
Expand Down Expand Up @@ -110,6 +117,13 @@ func gitlabHandlerFromURL(u *url.URL, token string, logger *zap.SugaredLogger) (
zap.String("pr", prNum),
)
client := gitlab.NewDefault()
if u.Host != "gitlab.com" {
var err error
client, err = gitlab.New(fmt.Sprintf("%s://%s", u.Scheme, u.Host))
if err != nil {
return nil, fmt.Errorf("error creating client: %w", err)
}
}
if token != "" {
client.Client = &http.Client{
Transport: &gitlabClient{
Expand Down
66 changes: 37 additions & 29 deletions pkg/pullrequest/scm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,44 @@ import (

func TestNewSCMHandler(t *testing.T) {
tests := []struct {
name string
raw string
wantRepo string
wantNum int
wantErr bool
name string
raw string
wantBaseURL string
wantRepo string
wantNum int
wantErr bool
}{
{
name: "github",
raw: "https://github.com/foo/bar/pull/1",
wantRepo: "foo/bar",
wantNum: 1,
wantErr: false,
name: "github",
raw: "https://github.com/foo/bar/pull/1",
wantBaseURL: "https://api.github.com/",
wantRepo: "foo/bar",
wantNum: 1,
wantErr: false,
},
{
name: "custom github",
raw: "https://github.tekton.dev/foo/baz/pull/2",
wantRepo: "foo/baz",
wantNum: 2,
wantErr: false,
name: "custom github",
raw: "https://github.tekton.dev/foo/baz/pull/2",
wantBaseURL: "https://github.tekton.dev/api/v3/",
wantRepo: "foo/baz",
wantNum: 2,
wantErr: false,
},
{
name: "gitlab",
raw: "https://gitlab.com/foo/bar/merge_requests/3",
wantRepo: "foo/bar",
wantNum: 3,
wantErr: false,
name: "gitlab",
raw: "https://gitlab.com/foo/bar/merge_requests/3",
wantBaseURL: "https://gitlab.com/",
wantRepo: "foo/bar",
wantNum: 3,
wantErr: false,
},
{
name: "gitlab multi-level",
raw: "https://gitlab.com/foo/bar/baz/merge_requests/3",
wantRepo: "foo/bar/baz",
wantNum: 3,
wantErr: false,
name: "gitlab multi-level",
raw: "https://gitlab.com/foo/bar/baz/merge_requests/3",
wantBaseURL: "https://gitlab.com/",
wantRepo: "foo/bar/baz",
wantNum: 3,
wantErr: false,
},
{
name: "unsupported",
Expand All @@ -77,11 +82,14 @@ func TestNewSCMHandler(t *testing.T) {
}
return
}
if !(got.prNum == tt.wantNum) {
t.Errorf("NewSCMHandler() = %v, want %v", got, tt.wantNum)
if got.prNum != tt.wantNum {
t.Errorf("NewSCMHandler() [pr num] = %v, want %v", got, tt.wantNum)
}
if !(got.repo == tt.wantRepo) {
t.Errorf("NewSCMHandler() = %v, want %v", got, tt.wantRepo)
if got.repo != tt.wantRepo {
t.Errorf("NewSCMHandler() [repo] = %v, want %v", got, tt.wantRepo)
}
if baseURL := got.client.BaseURL.String(); baseURL != tt.wantBaseURL {
t.Errorf("NewSCMHandler() [base url] = %v, want %v", baseURL, tt.wantBaseURL)
}
})
}
Expand Down

0 comments on commit c386a3b

Please sign in to comment.