You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ab...@apache.org on 2024/02/27 09:20:52 UTC

(incubator-devlake) branch release-v0.21 updated: fix: jira test connection error message for wrong user/pass not working (#7031) (#7034)

This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch release-v0.21
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/release-v0.21 by this push:
     new dbb3de9fd fix: jira test connection error message for wrong user/pass not working (#7031) (#7034)
dbb3de9fd is described below

commit dbb3de9fd462b02efede255d87e9055633573d8f
Author: github-actions[bot] <41...@users.noreply.github.com>
AuthorDate: Tue Feb 27 17:20:48 2024 +0800

    fix: jira test connection error message for wrong user/pass not working (#7031) (#7034)
    
    Co-authored-by: Klesh Wong <zh...@merico.dev>
---
 backend/helpers/pluginhelper/api/api_client.go | 24 +++++++---------------
 backend/plugins/github/api/remote_api.go       |  4 +++-
 backend/plugins/jira/api/connection_api.go     | 28 +++++++++++++-------------
 3 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/backend/helpers/pluginhelper/api/api_client.go b/backend/helpers/pluginhelper/api/api_client.go
index 570dc876f..e69a88680 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 dce870da3..437b0caa5 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 2a0fe3e32..08692a727 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")
 	}
@@ -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 {