| 36 | } |
| 37 | |
| 38 | func testConnection(ctx context.Context, connection models.GitlabConn) (*GitlabTestConnResponse, errors.Error) { |
| 39 | // validate |
| 40 | if vld != nil { |
| 41 | if err := vld.Struct(connection); err != nil { |
| 42 | return nil, errors.Default.Wrap(err, "error validating target") |
| 43 | } |
| 44 | } |
| 45 | apiClient, err := api.NewApiClientFromConnection(ctx, basicRes, &connection) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | |
| 50 | // check API/read_api permissions |
| 51 | query := url.Values{} |
| 52 | query.Set("page", fmt.Sprintf("%v", 1)) |
| 53 | query.Set("per_page", fmt.Sprintf("%v", 1)) |
| 54 | res, err := apiClient.Get("projects", query, nil) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | |
| 59 | if res.StatusCode == http.StatusUnauthorized { |
| 60 | return nil, errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error when testing api or read_api permissions") |
| 61 | } |
| 62 | |
| 63 | if res.StatusCode == http.StatusForbidden { |
| 64 | return nil, errors.BadInput.New("token need api or read_api permissions scope") |
| 65 | } |
| 66 | |
| 67 | connection = connection.Sanitize() |
| 68 | body := GitlabTestConnResponse{} |
| 69 | body.Success = true |
| 70 | body.Message = "success" |
| 71 | body.Connection = &connection |
| 72 | |
| 73 | return &body, nil |
| 74 | } |
| 75 | |
| 76 | // TestConnection test gitlab connection |
| 77 | // @Summary test gitlab connection |