From 0a8e3e86c9af15f1bf3a078eb9a254c1fef5dffb Mon Sep 17 00:00:00 2001 From: Luke Kysow <1034429+lkysow@users.noreply.github.com> Date: Fri, 1 Mar 2019 09:12:26 -0500 Subject: [PATCH] Support basepath for bitbucket server Previously we were stripping the basepath from the passed in URL. i.e. if I passed in --bitbucket-base-url=https://me.com/basepath then we would just strip off /basepath. This change fixes that bug (#508). --- server/events/vcs/bitbucketserver/client.go | 4 +- .../events/vcs/bitbucketserver/client_test.go | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/server/events/vcs/bitbucketserver/client.go b/server/events/vcs/bitbucketserver/client.go index a0924e6e0..7abfd2da7 100644 --- a/server/events/vcs/bitbucketserver/client.go +++ b/server/events/vcs/bitbucketserver/client.go @@ -42,7 +42,6 @@ func NewClient(httpClient *http.Client, username string, password string, baseUR if httpClient == nil { httpClient = http.DefaultClient } - // Remove the trailing '/' from the URL. parsedURL, err := url.Parse(baseURL) if err != nil { return nil, errors.Wrapf(err, "parsing %s", baseURL) @@ -50,12 +49,11 @@ func NewClient(httpClient *http.Client, username string, password string, baseUR if parsedURL.Scheme == "" { return nil, fmt.Errorf("must have 'http://' or 'https://' in base url %q", baseURL) } - urlWithoutPath := fmt.Sprintf("%s://%s", parsedURL.Scheme, parsedURL.Host) return &Client{ HTTPClient: httpClient, Username: username, Password: password, - BaseURL: urlWithoutPath, + BaseURL: strings.TrimRight(parsedURL.String(), "/"), AtlantisURL: atlantisURL, }, nil } diff --git a/server/events/vcs/bitbucketserver/client_test.go b/server/events/vcs/bitbucketserver/client_test.go index f2651eb95..b92885e99 100644 --- a/server/events/vcs/bitbucketserver/client_test.go +++ b/server/events/vcs/bitbucketserver/client_test.go @@ -14,6 +14,60 @@ import ( . "github.com/runatlantis/atlantis/testing" ) +// Test that we include the base path in our base url. +func TestClient_BasePath(t *testing.T) { + cases := []struct { + inputURL string + expURL string + expErr string + }{ + { + inputURL: "mycompany.com", + expErr: `must have 'http://' or 'https://' in base url "mycompany.com"`, + }, + { + inputURL: "https://mycompany.com", + expURL: "https://mycompany.com", + }, + { + inputURL: "http://mycompany.com", + expURL: "http://mycompany.com", + }, + { + inputURL: "http://mycompany.com:7990", + expURL: "http://mycompany.com:7990", + }, + { + inputURL: "http://mycompany.com/", + expURL: "http://mycompany.com", + }, + { + inputURL: "http://mycompany.com:7990/", + expURL: "http://mycompany.com:7990", + }, + { + inputURL: "http://mycompany.com/basepath/", + expURL: "http://mycompany.com/basepath", + }, + { + inputURL: "http://mycompany.com:7990/basepath/", + expURL: "http://mycompany.com:7990/basepath", + }, + } + + for _, c := range cases { + t.Run(c.inputURL, func(t *testing.T) { + client, err := bitbucketserver.NewClient(nil, "u", "p", c.inputURL, "atlantis-url") + if c.expErr != "" { + ErrEquals(t, c.expErr, err) + } else { + Ok(t, err) + Equals(t, c.expURL, client.BaseURL) + } + }) + } +} + // Should follow pagination properly. func TestClient_GetModifiedFilesPagination(t *testing.T) { respTemplate := `