Skip to content
This repository was archived by the owner on Jul 15, 2024. It is now read-only.

Commit 216d0ee

Browse files
committed
fix: pr comments, refactor bitbucket BasePath
Signed-off-by: mlosicki <[email protected]>
1 parent 4690633 commit 216d0ee

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

pkg/services/pull_request/bitbucket_server.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"fmt"
66
"regexp"
7-
"strings"
87

8+
"github.com/argoproj/applicationset/pkg/utils"
99
bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
1010
)
1111

@@ -38,9 +38,7 @@ func NewBitbucketServiceNoAuth(ctx context.Context, url, projectKey, repositoryS
3838
}
3939

4040
func newBitbucketService(ctx context.Context, bitbucketConfig *bitbucketv1.Configuration, projectKey, repositorySlug string, branchMatch *string) (PullRequestService, error) {
41-
if !strings.HasSuffix(bitbucketConfig.BasePath, "/rest") {
42-
bitbucketConfig.BasePath = bitbucketConfig.BasePath + "/rest"
43-
}
41+
bitbucketConfig.BasePath = utils.NormalizeBitbucketBasePath(bitbucketConfig.BasePath)
4442
bitbucketClient := bitbucketv1.NewAPIClient(ctx, bitbucketConfig)
4543

4644
var branchMatchRegexp *regexp.Regexp

pkg/services/scm_provider/bitbucket_server.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package scm_provider
33
import (
44
"context"
55
"fmt"
6-
"strings"
76

7+
"github.com/argoproj/applicationset/pkg/utils"
88
bitbucketv1 "github.com/gfleury/go-bitbucket-v1"
99
)
1010

@@ -34,9 +34,7 @@ func NewBitbucketServerProviderNoAuth(ctx context.Context, url, projectKey strin
3434
}
3535

3636
func newBitbucketServerProvider(ctx context.Context, bitbucketConfig *bitbucketv1.Configuration, projectKey string, allBranches bool) (*BitbucketServerProvider, error) {
37-
if !strings.HasSuffix(bitbucketConfig.BasePath, "/rest") {
38-
bitbucketConfig.BasePath = bitbucketConfig.BasePath + "/rest"
39-
}
37+
bitbucketConfig.BasePath = utils.NormalizeBitbucketBasePath(bitbucketConfig.BasePath)
4038
bitbucketClient := bitbucketv1.NewAPIClient(ctx, bitbucketConfig)
4139

4240
return &BitbucketServerProvider{

pkg/utils/util.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,13 @@ func addInvalidGeneratorNames(names map[string]bool, applicationSetInfo *argopro
176176
break
177177
}
178178
}
179+
180+
func NormalizeBitbucketBasePath(basePath string) string {
181+
if strings.HasSuffix(basePath, "/rest/") {
182+
return strings.TrimSuffix(basePath, "/")
183+
}
184+
if !strings.HasSuffix(basePath, "/rest") {
185+
return basePath + "/rest"
186+
}
187+
return basePath
188+
}

pkg/utils/util_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,3 +649,30 @@ func TestInvalidGenerators(t *testing.T) {
649649
assert.Equal(t, c.expectedNames, names, c.testName)
650650
}
651651
}
652+
653+
func TestNormalizeBitbucketBasePath(t *testing.T) {
654+
for _, c := range []struct {
655+
testName string
656+
basePath string
657+
expectedBasePath string
658+
}{
659+
{
660+
testName: "default api url",
661+
basePath: "https://company.bitbucket.com",
662+
expectedBasePath: "https://company.bitbucket.com/rest",
663+
},
664+
{
665+
testName: "with /rest suffix",
666+
basePath: "https://company.bitbucket.com/rest",
667+
expectedBasePath: "https://company.bitbucket.com/rest",
668+
},
669+
{
670+
testName: "with /rest/ suffix",
671+
basePath: "https://company.bitbucket.com/rest/",
672+
expectedBasePath: "https://company.bitbucket.com/rest",
673+
},
674+
} {
675+
result := NormalizeBitbucketBasePath(c.basePath)
676+
assert.Equal(t, c.expectedBasePath, result, c.testName)
677+
}
678+
}

0 commit comments

Comments
 (0)