From 8caba608ce6372bbe3b9bd039bc942106bd9152a Mon Sep 17 00:00:00 2001 From: Christian Groschupp Date: Tue, 22 Jun 2021 07:49:59 +0200 Subject: [PATCH 1/2] readd "validate bitbucket api url (#138)" --- client.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index 949911d..bd0b7b7 100644 --- a/client.go +++ b/client.go @@ -25,13 +25,13 @@ const DEFAULT_PAGE_LENGTH = 10 const DEFAULT_MAX_DEPTH = 1 const DEFAULT_BITBUCKET_API_BASE_URL = "https://api.bitbucket.org/2.0" -func apiBaseUrl() string { +func apiBaseUrl() (*url.URL, error) { ev := os.Getenv("BITBUCKET_API_BASE_URL") - if ev != "" { - return ev + if ev == "" { + ev = DEFAULT_BITBUCKET_API_BASE_URL } - return DEFAULT_BITBUCKET_API_BASE_URL + return url.Parse(ev) } type Client struct { @@ -43,7 +43,7 @@ type Client struct { Workspaces *Workspace Pagelen uint64 MaxDepth uint64 - apiBaseURL string + apiBaseURL *url.URL HttpClient *http.Client } @@ -135,7 +135,11 @@ func NewBasicAuth(u, p string) *Client { } func injectClient(a *auth) *Client { - c := &Client{Auth: a, Pagelen: DEFAULT_PAGE_LENGTH, MaxDepth: DEFAULT_MAX_DEPTH, apiBaseURL: apiBaseUrl()} + bitbucketUrl, err := apiBaseUrl() + if err != nil { + log.Fatalf("invalid bitbucket url") + } + c := &Client{Auth: a, Pagelen: DEFAULT_PAGE_LENGTH, MaxDepth: DEFAULT_MAX_DEPTH, apiBaseURL: bitbucketUrl} c.Repositories = &Repositories{ c: c, PullRequests: &PullRequests{c: c}, @@ -157,11 +161,15 @@ func injectClient(a *auth) *Client { } func (c *Client) GetApiBaseURL() string { - return c.apiBaseURL + return fmt.Sprintf("%s%s", c.apiBaseURL.GetApiHostnameURL(), c.apiBaseURL.Path) +} + +func (c *Client) GetApiHostnameURL() string { + return fmt.Sprintf("%s://%s", c.apiBaseURL.Scheme, c.apiBaseURL.Host) } -func (c *Client) SetApiBaseURL(urlStr string) { - c.apiBaseURL = urlStr +func (c *Client) SetApiBaseURL(urlStr url.URL) { + c.apiBaseURL = &urlStr } func (c *Client) executeRaw(method string, urlStr string, text string) (io.ReadCloser, error) { @@ -379,7 +387,7 @@ func unexpectedHttpStatusCode(statusCode int) bool { func (c *Client) requestUrl(template string, args ...interface{}) string { if len(args) == 1 && args[0] == "" { - return c.apiBaseURL + template + return c.GetApiBaseURL() + template } - return c.apiBaseURL + fmt.Sprintf(template, args...) + return c.GetApiBaseURL() + fmt.Sprintf(template, args...) } From b53e6a3125dc8664904bfaae1a2644722db81673 Mon Sep 17 00:00:00 2001 From: Christian Groschupp Date: Tue, 22 Jun 2021 07:51:10 +0200 Subject: [PATCH 2/2] fix GetApiBaseURL --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index bd0b7b7..ec1cc54 100644 --- a/client.go +++ b/client.go @@ -161,7 +161,7 @@ func injectClient(a *auth) *Client { } func (c *Client) GetApiBaseURL() string { - return fmt.Sprintf("%s%s", c.apiBaseURL.GetApiHostnameURL(), c.apiBaseURL.Path) + return fmt.Sprintf("%s%s", c.GetApiHostnameURL(), c.apiBaseURL.Path) } func (c *Client) GetApiHostnameURL() string {