Skip to content

Commit 78f4993

Browse files
fix: bitbucket server and cloud provider selection (#801)
## What <!-- What is changing in this PR? --> ## Why <!-- Why are these changes being made? --> ## Notes <!-- Add any additional notes here -->
1 parent edbdd05 commit 78f4993

12 files changed

Lines changed: 35 additions & 28 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.2.16
1+
VERSION=v0.2.17
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/helm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ global:
539539
username: some-username`,
540540
gitProvider: platmodel.GitProvidersGithub,
541541
gitApiUrl: "some-api-url",
542-
wantErr: "failed verifying runtime git token with git server \"some-api-url\": invalid git-token: Head \"some-api-url\": some error",
542+
wantErr: "failed verifying runtime git token with git server \"some-api-url\": invalid git-token: Head \"/api/v3\": some error",
543543
beforeFn: func(rt *gitmocks.MockRoundTripper) {
544544
rt.EXPECT().RoundTrip(gomock.AssignableToTypeOf(&http.Request{})).Times(1).Return(nil, errors.New("some error"))
545545
},

internal/git/provider.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,10 @@ func getProvider(providerType ProviderType, cloneURL, certFile string) (Provider
184184

185185
func getGitProviderFromUserSelect(baseURL string, client *http.Client) Provider {
186186
var providers = map[string]func(string, *http.Client) (Provider, error){
187-
"Bitbucket": NewBitbucketServerProvider,
188-
"GitHub": NewGithubProvider,
189-
"GitLab": NewGitlabProvider,
187+
"Bitbucket": NewBitbucketProvider,
188+
"GitHub": NewGithubProvider,
189+
"GitLab": NewGitlabProvider,
190+
"Bitbucket Server": NewBitbucketServerProvider,
190191
}
191192

192193
templates := &promptui.SelectTemplates{

internal/git/provider_bitbucket-server.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ func NewBitbucketServerProvider(baseURL string, client *http.Client) (Provider,
5252
return nil, fmt.Errorf("wrong domain for bitbucket-server provider: \"%s\"\n maybe you meant to use \"bitbucket\" for the cloud git provider?", baseURL)
5353
}
5454

55-
if u.Path == "" {
56-
u.Path = BITBUCKET_SERVER_REST_ENDPOINT
57-
}
55+
u.Path = BITBUCKET_SERVER_REST_ENDPOINT
5856

5957
return &bitbucketServer{
6058
providerType: BITBUCKET_SERVER,

internal/git/provider_bitbucket-server_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ func TestNewBitbucketServerProvider(t *testing.T) {
4444
baseURL: "https://some.server",
4545
wantApiURL: "https://some.server/rest/api/1.0",
4646
},
47-
"should use baseUrl as apiUrl if it has path": {
47+
"should ignore baseUrl path and use standard api path": {
4848
baseURL: "https://some.server/some/api/v-whatever",
49-
wantApiURL: "https://some.server/some/api/v-whatever",
49+
wantApiURL: "https://some.server/rest/api/1.0",
5050
},
5151
"should fail when base is not a valid url": {
5252
baseURL: "https://contains-bad-\x7f",

internal/git/provider_bitbucket.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type (
3737

3838
const (
3939
BITBUCKET_CLOUD_DOMAIN = "bitbucket.org"
40-
BITBUCKET_REST_ENDPOINT = "/api/2.0"
40+
BITBUCKET_CLOUD_API_URL = "https://api.bitbucket.org/2.0"
4141
BITBUCKET ProviderType = "bitbucket"
4242
)
4343

@@ -68,10 +68,15 @@ func NewBitbucketProvider(baseURL string, client *http.Client) (Provider, error)
6868
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)
6969
}
7070

71-
u.Path = BITBUCKET_REST_ENDPOINT
71+
// new api token working only with api.bitbucket.org/2.0
72+
apiUrl, err := url.Parse(BITBUCKET_CLOUD_API_URL)
73+
if err != nil {
74+
return nil, err
75+
}
76+
7277
return &bitbucket{
7378
providerType: BITBUCKET,
74-
apiURL: u,
79+
apiURL: apiUrl,
7580
c: client,
7681
}, nil
7782
}

internal/git/provider_bitbucket_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func TestNewBitbucketProvider(t *testing.T) {
4242
}{
4343
"should use standard api path when base is host only": {
4444
baseURL: "https://bitbucket.org",
45-
wantApiURL: "https://bitbucket.org/api/2.0",
45+
wantApiURL: "https://api.bitbucket.org/2.0",
4646
},
4747
"should ignore baseUrl path if it contains it": {
4848
baseURL: "https://bitbucket.org/some/api/v-whatever",
49-
wantApiURL: "https://bitbucket.org/api/2.0",
49+
wantApiURL: "https://api.bitbucket.org/2.0",
5050
},
5151
"should fail when base is not a valid url": {
5252
baseURL: "https://bitbucket.org/\x7f",
@@ -78,7 +78,7 @@ func Test_bitbucket_verifyToken(t *testing.T) {
7878
beforeFn func(c *mocks.MockRoundTripper)
7979
}{
8080
"Should fail if HEAD fails": {
81-
wantErr: "failed checking token scope permission: failed getting current user: Head \"https://bitbucket.org/api/2.0/user\": some error",
81+
wantErr: "failed checking token scope permission: failed getting current user: Head \"https://api.bitbucket.org/2.0/user\": some error",
8282
beforeFn: func(c *mocks.MockRoundTripper) {
8383
c.EXPECT().RoundTrip(gomock.AssignableToTypeOf(&http.Request{})).Return(nil, errors.New("some error"))
8484
},

internal/git/provider_github.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func NewGithubProvider(baseURL string, client *http.Client) (Provider, error) {
5151

5252
if strings.Contains(u.Hostname(), GITHUB_CLOUD_DOMAIN) {
5353
u, _ = url.Parse(GITHUB_CLOUD_API_URL)
54-
} else if u.Path == "" {
54+
} else {
5555
u.Path = GITHUB_REST_ENDPOINT
5656
}
5757

internal/git/provider_github_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ func TestNewGithubProvider(t *testing.T) {
5656
wantApiURL: "https://some.server/api/v3",
5757
wantCloud: false,
5858
},
59-
"should use baseUrl as apiUrl if it on-prem and has path": {
59+
"should ignore on-prem baseUrl path and use standard api path": {
6060
baseURL: "https://some.server/some/api/v-whatever",
61-
wantApiURL: "https://some.server/some/api/v-whatever",
61+
wantApiURL: "https://some.server/api/v3",
6262
wantCloud: false,
6363
},
6464
"should fail when base is not a valid url": {

internal/git/provider_gitlab.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,18 @@ const (
4545
GITLAB_CLOUD_DOMAIN = "gitlab.com"
4646
GITLAB_REST_ENDPOINT = "/api/v4"
4747
GITLAB ProviderType = "gitlab"
48+
GITLAB_CLOUD_API_URL = "https://gitlab.com"
4849
)
4950

5051
func NewGitlabProvider(baseURL string, client *http.Client) (Provider, error) {
51-
u, err := url.Parse(baseURL)
52+
u, err := url.Parse(baseURL) // baseURL is the isc url
5253
if err != nil {
5354
return nil, err
5455
}
5556

56-
if u.Host == GITLAB_CLOUD_DOMAIN || u.Path == "" {
57+
if strings.Contains(u.Hostname(), GITLAB_CLOUD_DOMAIN) {
58+
u, _ = url.Parse(GITLAB_CLOUD_API_URL)
59+
} else {
5760
u.Path = GITLAB_REST_ENDPOINT
5861
}
5962

0 commit comments

Comments
 (0)