From ad9aabadc24af5ac7abe7a0bfaef57b48023a3c3 Mon Sep 17 00:00:00 2001 From: Klesh Wong Date: Tue, 27 Feb 2024 15:17:17 +0800 Subject: [PATCH 1/2] Revert "fix: invalid url detection not working correctly (#7016)" This reverts commit 5ec63adb1f2fd894153a5ab8931a3bb8bb098dac. --- backend/plugins/jira/api/connection_api.go | 26 +++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/backend/plugins/jira/api/connection_api.go b/backend/plugins/jira/api/connection_api.go index 2a0fe3e32a5..aee973ef8ba 100644 --- a/backend/plugins/jira/api/connection_api.go +++ b/backend/plugins/jira/api/connection_api.go @@ -54,22 +54,22 @@ func testConnection(ctx context.Context, connection models.JiraConn) (*JiraTestC // serverInfo checking res, err := apiClient.Get("api/2/serverInfo", nil, nil) if err != nil { - // check if `/rest/` was missing - if strings.Contains(err.Error(), "status code 404") && !strings.HasSuffix(connection.Endpoint, "/rest/") { - endpointUrl, err := url.Parse(connection.Endpoint) - if err != nil { - return nil, errors.Convert(err) - } - refUrl, err := url.Parse("/rest/") - if err != nil { - return nil, errors.Convert(err) - } - restUrl := endpointUrl.ResolveReference(refUrl) - return nil, errors.NotFound.New(fmt.Sprintf("Seems like an invalid Endpoint URL, please try %s", restUrl.String())) - } return nil, err } serverInfoFail := "Failed testing the serverInfo: [ " + res.Request.URL.String() + " ]" + // check if `/rest/` was missing + if res.StatusCode == http.StatusNotFound && !strings.HasSuffix(connection.Endpoint, "/rest/") { + endpointUrl, err := url.Parse(connection.Endpoint) + if err != nil { + return nil, errors.Convert(err) + } + refUrl, err := url.Parse("/rest/") + if err != nil { + return nil, errors.Convert(err) + } + restUrl := endpointUrl.ResolveReference(refUrl) + return nil, errors.NotFound.New(fmt.Sprintf("Seems like an invalid Endpoint URL, please try %s", restUrl.String())) + } if res.StatusCode == http.StatusUnauthorized { return nil, errors.HttpStatus(http.StatusBadRequest).New("Error username/password") } From 6cbee5852701dd8ed0090b619d595cdc77c829b5 Mon Sep 17 00:00:00 2001 From: Klesh Wong Date: Tue, 27 Feb 2024 16:48:51 +0800 Subject: [PATCH 2/2] fix: jira test connection user/pass incorrect message not working --- .../helpers/pluginhelper/api/api_client.go | 24 ++++++------------- backend/plugins/github/api/remote_api.go | 4 +++- backend/plugins/jira/api/connection_api.go | 2 +- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/backend/helpers/pluginhelper/api/api_client.go b/backend/helpers/pluginhelper/api/api_client.go index 570dc876fb7..e69a8868034 100644 --- a/backend/helpers/pluginhelper/api/api_client.go +++ b/backend/helpers/pluginhelper/api/api_client.go @@ -91,17 +91,6 @@ func NewApiClientFromConnection( }) } - apiClient.SetAfterFunction(func(res *http.Response) errors.Error { - if res.StatusCode >= 400 { - bytes, err := io.ReadAll(res.Body) - if err != nil { - return errors.BadInput.Wrap(err, fmt.Sprintf("request failed with status code %d", res.StatusCode)) - } - return errors.BadInput.New(fmt.Sprintf("request failed with status code %d, body: %s", res.StatusCode, string(bytes))) - } - return nil - }) - return apiClient, nil } @@ -124,16 +113,13 @@ func NewApiClient( apiClient.client.Transport = &http.Transport{} // set insecureSkipVerify - insecureSkipVerify, err := utils.StrToBoolOr(br.GetConfig("IN_SECURE_SKIP_VERIFY"), false) - if err != nil { - return nil, errors.Default.Wrap(err, "failed to parse IN_SECURE_SKIP_VERIFY") - } + insecureSkipVerify := br.GetConfigReader().GetBool("IN_SECURE_SKIP_VERIFY") if insecureSkipVerify { apiClient.client.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} } if proxy != "" { - err = apiClient.SetProxy(proxy) + err := apiClient.SetProxy(proxy) if err != nil { return nil, errors.Convert(err) } @@ -394,7 +380,11 @@ func UnmarshalResponse(res *http.Response, v interface{}) errors.Error { } err = errors.Convert(json.Unmarshal(resBody, &v)) if err != nil { - return errors.Default.New(fmt.Sprintf("error decoding response from %s: raw response: %s", res.Request.URL.String(), string(resBody))) + statusCode := res.StatusCode + if statusCode == http.StatusUnauthorized || statusCode == http.StatusForbidden { + statusCode = http.StatusBadRequest // to avoid Basic Auth Dialog poping up + } + return errors.HttpStatus(statusCode).Wrap(err, fmt.Sprintf("error decoding response from %s: raw response: %s", res.Request.URL.String(), string(resBody))) } return nil } diff --git a/backend/plugins/github/api/remote_api.go b/backend/plugins/github/api/remote_api.go index dce870da3c3..437b0caa551 100644 --- a/backend/plugins/github/api/remote_api.go +++ b/backend/plugins/github/api/remote_api.go @@ -91,7 +91,9 @@ func listGithubUserOrgs( return nil, nil, err } var orgs []org - errors.Must(api.UnmarshalResponse(orgsBody, &orgs)) + if err := api.UnmarshalResponse(orgsBody, &orgs); err != nil { + return nil, nil, err + } for _, o := range orgs { children = append(children, dsmodels.DsRemoteApiScopeListEntry[models.GithubRepo]{ Type: api.RAS_ENTRY_TYPE_GROUP, diff --git a/backend/plugins/jira/api/connection_api.go b/backend/plugins/jira/api/connection_api.go index aee973ef8ba..08692a72777 100644 --- a/backend/plugins/jira/api/connection_api.go +++ b/backend/plugins/jira/api/connection_api.go @@ -100,7 +100,7 @@ func testConnection(ctx context.Context, connection models.JiraConn) (*JiraTestC errMsg := "" if res.StatusCode == http.StatusUnauthorized { - return nil, errors.HttpStatus(http.StatusBadRequest).New("it might you use the right token(password) but with the wrong username.please check your username/password") + return nil, errors.HttpStatus(http.StatusBadRequest).New("Please check your username/password") } if res.StatusCode != http.StatusOK {