MCPcopy
hub / github.com/apache/devlake / testConnection

Function testConnection

backend/plugins/gitlab/api/connection_api.go:38–74  ·  view source on GitHub ↗
(ctx context.Context, connection models.GitlabConn)

Source from the content-addressed store, hash-verified

36}
37
38func 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

Callers 2

TestConnectionFunction · 0.70
TestExistingConnectionFunction · 0.70

Calls 4

WrapMethod · 0.80
GetMethod · 0.65
NewMethod · 0.65
SanitizeMethod · 0.45

Tested by 2

TestConnectionFunction · 0.56
TestExistingConnectionFunction · 0.56