| 39 | } |
| 40 | |
| 41 | func testConnection(ctx context.Context, connection models.JiraConn) (*JiraTestConnResponse, errors.Error) { |
| 42 | // validate |
| 43 | if vld != nil { |
| 44 | e := vld.StructExcept(connection, "BasicAuth", "AccessToken") |
| 45 | if e != nil { |
| 46 | return nil, errors.Convert(e) |
| 47 | } |
| 48 | } |
| 49 | // test connection |
| 50 | apiClient, err := api.NewApiClientFromConnection(ctx, basicRes, &connection) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | // serverInfo checking |
| 55 | res, err := apiClient.Get("api/2/serverInfo", nil, nil) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | serverInfoFail := "Failed testing the serverInfo: [ " + res.Request.URL.String() + " ]" |
| 60 | // check if `/rest/` was missing |
| 61 | if res.StatusCode == http.StatusNotFound && !strings.HasSuffix(connection.Endpoint, "/rest/") { |
| 62 | endpointUrl, err := url.Parse(connection.Endpoint) |
| 63 | if err != nil { |
| 64 | return nil, errors.Convert(err) |
| 65 | } |
| 66 | refUrl, err := url.Parse("/rest/") |
| 67 | if err != nil { |
| 68 | return nil, errors.Convert(err) |
| 69 | } |
| 70 | restUrl := endpointUrl.ResolveReference(refUrl) |
| 71 | return nil, errors.NotFound.New(fmt.Sprintf("Seems like an invalid Endpoint URL, please try %s", restUrl.String())) |
| 72 | } |
| 73 | if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden { |
| 74 | return nil, errors.HttpStatus(res.StatusCode).New("Please check your credential") |
| 75 | } |
| 76 | if res.StatusCode != http.StatusOK { |
| 77 | return nil, errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("%s unexpected status code: %d", serverInfoFail, res.StatusCode)) |
| 78 | } |
| 79 | |
| 80 | resBody := &models.JiraServerInfo{} |
| 81 | err = api.UnmarshalResponse(res, resBody) |
| 82 | if err != nil { |
| 83 | return nil, errors.Convert(err) |
| 84 | } |
| 85 | // check version |
| 86 | if resBody.DeploymentType == models.DeploymentServer { |
| 87 | // only support 8.x.x or higher |
| 88 | if versions := resBody.VersionNumbers; len(versions) == 3 && versions[0] < 7 { |
| 89 | return nil, errors.Default.New(fmt.Sprintf("%s Support JIRA Server 7+ only", serverInfoFail)) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // verify credential |
| 94 | getStatusFail := "an error occurred while making request to `/rest/agile/1.0/board`" |
| 95 | res, err = apiClient.Get("agile/1.0/board", nil, nil) |
| 96 | if err != nil { |
| 97 | return nil, errors.Default.Wrap(err, getStatusFail) |
| 98 | } |