diff --git a/Makefile b/Makefile index 3930e378..dda5fd04 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=v0.2.16 +VERSION=v0.2.17 OUT_DIR=dist YEAR?=$(shell date +"%Y") diff --git a/internal/git/provider.go b/internal/git/provider.go index b0ea6314..a0097d1e 100644 --- a/internal/git/provider.go +++ b/internal/git/provider.go @@ -184,9 +184,10 @@ func getProvider(providerType ProviderType, cloneURL, certFile string) (Provider func getGitProviderFromUserSelect(baseURL string, client *http.Client) Provider { var providers = map[string]func(string, *http.Client) (Provider, error){ - "Bitbucket": NewBitbucketServerProvider, - "GitHub": NewGithubProvider, - "GitLab": NewGitlabProvider, + "Bitbucket": NewBitbucketProvider, + "GitHub": NewGithubProvider, + "GitLab": NewGitlabProvider, + "Bitbucket Server": NewBitbucketServerProvider, } templates := &promptui.SelectTemplates{ diff --git a/internal/git/provider_bitbucket-server.go b/internal/git/provider_bitbucket-server.go index 41bcbfcb..b6958ad0 100644 --- a/internal/git/provider_bitbucket-server.go +++ b/internal/git/provider_bitbucket-server.go @@ -52,9 +52,7 @@ func NewBitbucketServerProvider(baseURL string, client *http.Client) (Provider, return nil, fmt.Errorf("wrong domain for bitbucket-server provider: \"%s\"\n maybe you meant to use \"bitbucket\" for the cloud git provider?", baseURL) } - if u.Path == "" { - u.Path = BITBUCKET_SERVER_REST_ENDPOINT - } + u.Path = BITBUCKET_SERVER_REST_ENDPOINT return &bitbucketServer{ providerType: BITBUCKET_SERVER, diff --git a/internal/git/provider_bitbucket-server_test.go b/internal/git/provider_bitbucket-server_test.go index 00a5c36d..4dbac4fc 100644 --- a/internal/git/provider_bitbucket-server_test.go +++ b/internal/git/provider_bitbucket-server_test.go @@ -44,9 +44,9 @@ func TestNewBitbucketServerProvider(t *testing.T) { baseURL: "https://some.server", wantApiURL: "https://some.server/rest/api/1.0", }, - "should use baseUrl as apiUrl if it has path": { + "should ignore baseUrl path and use standard api path": { baseURL: "https://some.server/some/api/v-whatever", - wantApiURL: "https://some.server/some/api/v-whatever", + wantApiURL: "https://some.server/rest/api/1.0", }, "should fail when base is not a valid url": { baseURL: "https://contains-bad-\x7f", diff --git a/internal/git/provider_bitbucket.go b/internal/git/provider_bitbucket.go index 58d96644..77cb5fba 100644 --- a/internal/git/provider_bitbucket.go +++ b/internal/git/provider_bitbucket.go @@ -37,7 +37,8 @@ type ( const ( BITBUCKET_CLOUD_DOMAIN = "bitbucket.org" - BITBUCKET_REST_ENDPOINT = "/api/2.0" + BITBUCKET_CLOUD_API_URL = "api.bitbucket.org" + BITBUCKET_REST_ENDPOINT = "/2.0" BITBUCKET ProviderType = "bitbucket" ) @@ -68,7 +69,10 @@ func NewBitbucketProvider(baseURL string, client *http.Client) (Provider, error) return nil, fmt.Errorf("wrong domain for bitbucket provider: \"%s\", expected \"%s\"\n maybe you meant to use \"bitbucket-server\" for on-prem git provider?", baseURL, BITBUCKET_CLOUD_DOMAIN) } + // new api token working only with api.bitbucket.org/2.0 + u.Host = BITBUCKET_CLOUD_API_URL u.Path = BITBUCKET_REST_ENDPOINT + u.User = nil // make sure to remove prefix of https://user:pass@.. return &bitbucket{ providerType: BITBUCKET, apiURL: u, diff --git a/internal/git/provider_bitbucket_test.go b/internal/git/provider_bitbucket_test.go index 6e8ceba3..79307cc6 100644 --- a/internal/git/provider_bitbucket_test.go +++ b/internal/git/provider_bitbucket_test.go @@ -42,11 +42,11 @@ func TestNewBitbucketProvider(t *testing.T) { }{ "should use standard api path when base is host only": { baseURL: "https://bitbucket.org", - wantApiURL: "https://bitbucket.org/api/2.0", + wantApiURL: "https://api.bitbucket.org/2.0", }, "should ignore baseUrl path if it contains it": { baseURL: "https://bitbucket.org/some/api/v-whatever", - wantApiURL: "https://bitbucket.org/api/2.0", + wantApiURL: "https://api.bitbucket.org/2.0", }, "should fail when base is not a valid url": { baseURL: "https://bitbucket.org/\x7f", @@ -78,7 +78,7 @@ func Test_bitbucket_verifyToken(t *testing.T) { beforeFn func(c *mocks.MockRoundTripper) }{ "Should fail if HEAD fails": { - wantErr: "failed checking token scope permission: failed getting current user: Head \"https://bitbucket.org/api/2.0/user\": some error", + wantErr: "failed checking token scope permission: failed getting current user: Head \"https://api.bitbucket.org/2.0/user\": some error", beforeFn: func(c *mocks.MockRoundTripper) { c.EXPECT().RoundTrip(gomock.AssignableToTypeOf(&http.Request{})).Return(nil, errors.New("some error")) }, diff --git a/internal/git/provider_github.go b/internal/git/provider_github.go index b0780495..d22373dd 100644 --- a/internal/git/provider_github.go +++ b/internal/git/provider_github.go @@ -51,7 +51,7 @@ func NewGithubProvider(baseURL string, client *http.Client) (Provider, error) { if strings.Contains(u.Hostname(), GITHUB_CLOUD_DOMAIN) { u, _ = url.Parse(GITHUB_CLOUD_API_URL) - } else if u.Path == "" { + } else { u.Path = GITHUB_REST_ENDPOINT } diff --git a/internal/git/provider_github_test.go b/internal/git/provider_github_test.go index 2cd43105..56b01018 100644 --- a/internal/git/provider_github_test.go +++ b/internal/git/provider_github_test.go @@ -56,9 +56,9 @@ func TestNewGithubProvider(t *testing.T) { wantApiURL: "https://some.server/api/v3", wantCloud: false, }, - "should use baseUrl as apiUrl if it on-prem and has path": { + "should ignore on-prem baseUrl path and use standard api path": { baseURL: "https://some.server/some/api/v-whatever", - wantApiURL: "https://some.server/some/api/v-whatever", + wantApiURL: "https://some.server/api/v3", wantCloud: false, }, "should fail when base is not a valid url": { diff --git a/internal/git/provider_gitlab.go b/internal/git/provider_gitlab.go index 9faf48c7..9dc42db5 100644 --- a/internal/git/provider_gitlab.go +++ b/internal/git/provider_gitlab.go @@ -45,15 +45,22 @@ const ( GITLAB_CLOUD_DOMAIN = "gitlab.com" GITLAB_REST_ENDPOINT = "/api/v4" GITLAB ProviderType = "gitlab" + GITLAB_CLOUD_API_URL = "https://gitlab.com" ) func NewGitlabProvider(baseURL string, client *http.Client) (Provider, error) { - u, err := url.Parse(baseURL) + u, err := url.Parse(baseURL) // baseURL is the isc url if err != nil { return nil, err } - if u.Host == GITLAB_CLOUD_DOMAIN || u.Path == "" { + // if u.Host == GITLAB_CLOUD_DOMAIN || u.Path == "" { + // u.Path = GITLAB_REST_ENDPOINT + // } + + if strings.Contains(u.Hostname(), GITLAB_CLOUD_DOMAIN) { + u, _ = url.Parse(GITLAB_CLOUD_API_URL) + } else { u.Path = GITLAB_REST_ENDPOINT } diff --git a/internal/git/provider_gitlab_test.go b/internal/git/provider_gitlab_test.go index 512975a8..558f9068 100644 --- a/internal/git/provider_gitlab_test.go +++ b/internal/git/provider_gitlab_test.go @@ -46,12 +46,12 @@ func TestNewGitlabProvider(t *testing.T) { }{ "should use standard api path when base is cloud host": { baseURL: "https://gitlab.com", - wantApiURL: "https://gitlab.com/api/v4", + wantApiURL: "https://gitlab.com", wantCloud: true, }, "should use standard api path when base is cloud host with path": { baseURL: "https://gitlab.com/org/repo", - wantApiURL: "https://gitlab.com/api/v4", + wantApiURL: "https://gitlab.com", wantCloud: true, }, "should use standard api path when base is host only": { @@ -59,9 +59,9 @@ func TestNewGitlabProvider(t *testing.T) { wantApiURL: "https://some.server/api/v4", wantCloud: false, }, - "should use baseUrl as apiUrl if it on-prem and has path": { + "should ignore on-prem baseUrl path and use standard api path": { baseURL: "https://some.server/some/api/v-whatever", - wantApiURL: "https://some.server/some/api/v-whatever", + wantApiURL: "https://some.server/api/v4", wantCloud: false, }, "should fail when base is not a valid url": { diff --git a/internal/git/provider_test.go b/internal/git/provider_test.go index 0b08d2e3..1dd4bd3d 100644 --- a/internal/git/provider_test.go +++ b/internal/git/provider_test.go @@ -36,18 +36,18 @@ func TestGetProvider(t *testing.T) { "should return gitlab when url is in gitlab.com": { baseURL: "https://gitlab.com/org/repo", wantType: GITLAB, - wantApiURL: "https://gitlab.com/api/v4", + wantApiURL: "https://gitlab.com", }, "should return bitbucket when url is in bitbucket.org": { baseURL: "https://bitbucket.org/org/repo", wantType: BITBUCKET, - wantApiURL: "https://bitbucket.org/api/2.0", + wantApiURL: "https://api.bitbucket.org/2.0", }, "should use providedType when domain doesn't match known cloud providers": { providerType: BITBUCKET_SERVER, baseURL: "https://some.on-prem-provider.com/org/repo", wantType: BITBUCKET_SERVER, - wantApiURL: "https://some.on-prem-provider.com/org/repo", + wantApiURL: "https://some.on-prem-provider.com/rest/api/1.0", }, "should fail if provider does not match known cloud url, and no providerType was supplied": { providerType: GITLAB,