| 145 | } |
| 146 | |
| 147 | func testConnection(ctx context.Context, conn models.GithubConn) (*GithubTestConnResponse, errors.Error) { |
| 148 | if vld != nil { |
| 149 | if err := vld.StructExcept(conn, "GithubAppKey", "GithubAccessToken"); err != nil { |
| 150 | return nil, errors.Convert(err) |
| 151 | } |
| 152 | } |
| 153 | githubApiResponse := &GithubTestConnResponse{} |
| 154 | if conn.AuthMethod == models.AppKey { |
| 155 | if tokenTestResult, err := testGithubConnAppKeyAuth(ctx, conn); err != nil { |
| 156 | return nil, errors.Convert(err) |
| 157 | } else { |
| 158 | githubApiResponse.Success = tokenTestResult.Success |
| 159 | githubApiResponse.Message = tokenTestResult.Message |
| 160 | githubApiResponse.Login = tokenTestResult.Login |
| 161 | githubApiResponse.Installations = tokenTestResult.Installations |
| 162 | } |
| 163 | } else if conn.AuthMethod == models.AccessToken { |
| 164 | if tokenTestResult, err := testGithubConnAccessTokenAuth(ctx, conn); err != nil { |
| 165 | return nil, errors.Convert(err) |
| 166 | } else { |
| 167 | githubApiResponse.Success = tokenTestResult.Success |
| 168 | githubApiResponse.Warning = tokenTestResult.Warning |
| 169 | githubApiResponse.Message = tokenTestResult.Message |
| 170 | githubApiResponse.Login = tokenTestResult.Login |
| 171 | } |
| 172 | } else { |
| 173 | return nil, errors.BadInput.New("invalid authentication method") |
| 174 | } |
| 175 | |
| 176 | return githubApiResponse, nil |
| 177 | } |
| 178 | |
| 179 | type GitHubTestConnResult struct { |
| 180 | AuthMethod string `json:"auth_method"` |