Skip to content

Commit

Permalink
fix: jira test connection error message for wrong user/pass not worki…
Browse files Browse the repository at this point in the history
…ng (#7031)
  • Loading branch information
klesh authored Feb 27, 2024
1 parent ca5164e commit dc22bb3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
24 changes: 7 additions & 17 deletions backend/helpers/pluginhelper/api/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion backend/plugins/github/api/remote_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 14 additions & 14 deletions backend/plugins/jira/api/connection_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit dc22bb3

Please sign in to comment.