|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package api |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "net/http" |
| 23 | + |
| 24 | + "github.com/apache/incubator-devlake/core/errors" |
| 25 | + "github.com/apache/incubator-devlake/core/plugin" |
| 26 | + helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| 27 | + "github.com/apache/incubator-devlake/plugins/clickup/models" |
| 28 | + "github.com/apache/incubator-devlake/plugins/clickup/tasks" |
| 29 | + "github.com/apache/incubator-devlake/server/api/shared" |
| 30 | +) |
| 31 | + |
| 32 | +type ClickUpTestConnResponse struct { |
| 33 | + shared.ApiBody |
| 34 | + Connection *models.ClickUpConn |
| 35 | +} |
| 36 | + |
| 37 | +func testConnection(ctx context.Context, connection models.ClickUpConn) (*ClickUpTestConnResponse, errors.Error) { |
| 38 | + if vld != nil { |
| 39 | + if err := vld.Struct(connection); err != nil { |
| 40 | + return nil, errors.Default.Wrap(err, "error validating target") |
| 41 | + } |
| 42 | + } |
| 43 | + if connection.Endpoint == "" { |
| 44 | + connection.Endpoint = tasks.DefaultEndpoint |
| 45 | + } |
| 46 | + apiClient, err := helper.NewApiClientFromConnection(ctx, basicRes, &connection) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + // GET /team lists the authenticated user's workspaces; it verifies the token. |
| 51 | + res, err := apiClient.Get("team", nil, nil) |
| 52 | + if err != nil { |
| 53 | + return nil, errors.BadInput.Wrap(err, "verify token failed") |
| 54 | + } |
| 55 | + if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden { |
| 56 | + return nil, errors.HttpStatus(http.StatusBadRequest).New("authentication failed, please check your API token") |
| 57 | + } |
| 58 | + if res.StatusCode != http.StatusOK { |
| 59 | + return nil, errors.HttpStatus(res.StatusCode).New("unexpected status code while testing connection") |
| 60 | + } |
| 61 | + connection = connection.Sanitize() |
| 62 | + body := ClickUpTestConnResponse{} |
| 63 | + body.Success = true |
| 64 | + body.Message = "success" |
| 65 | + body.Connection = &connection |
| 66 | + return &body, nil |
| 67 | +} |
| 68 | + |
| 69 | +// TestConnection test clickup connection |
| 70 | +// @Summary test clickup connection |
| 71 | +// @Description Test clickup Connection |
| 72 | +// @Tags plugins/clickup |
| 73 | +// @Param body body models.ClickUpConn true "json body" |
| 74 | +// @Success 200 {object} ClickUpTestConnResponse "Success" |
| 75 | +// @Failure 400 {string} errcode.Error "Bad Request" |
| 76 | +// @Failure 500 {string} errcode.Error "Internal Error" |
| 77 | +// @Router /plugins/clickup/test [POST] |
| 78 | +func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 79 | + var connection models.ClickUpConn |
| 80 | + if err := helper.Decode(input.Body, &connection, vld); err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + result, err := testConnection(context.TODO(), connection) |
| 84 | + if err != nil { |
| 85 | + return nil, plugin.WrapTestConnectionErrResp(basicRes, err) |
| 86 | + } |
| 87 | + return &plugin.ApiResourceOutput{Body: result, Status: http.StatusOK}, nil |
| 88 | +} |
| 89 | + |
| 90 | +// TestExistingConnection test clickup connection by ID |
| 91 | +// @Summary test clickup connection |
| 92 | +// @Description Test clickup Connection |
| 93 | +// @Tags plugins/clickup |
| 94 | +// @Param connectionId path int true "connection ID" |
| 95 | +// @Success 200 {object} ClickUpTestConnResponse "Success" |
| 96 | +// @Failure 400 {string} errcode.Error "Bad Request" |
| 97 | +// @Failure 500 {string} errcode.Error "Internal Error" |
| 98 | +// @Router /plugins/clickup/connections/{connectionId}/test [POST] |
| 99 | +func TestExistingConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 100 | + connection, err := dsHelper.ConnApi.GetMergedConnection(input) |
| 101 | + if err != nil { |
| 102 | + return nil, errors.BadInput.Wrap(err, "find connection from db") |
| 103 | + } |
| 104 | + if err := helper.DecodeMapStruct(input.Body, connection, false); err != nil { |
| 105 | + return nil, err |
| 106 | + } |
| 107 | + result, testErr := testConnection(context.TODO(), connection.ClickUpConn) |
| 108 | + if testErr != nil { |
| 109 | + return nil, plugin.WrapTestConnectionErrResp(basicRes, testErr) |
| 110 | + } |
| 111 | + return &plugin.ApiResourceOutput{Body: result, Status: http.StatusOK}, nil |
| 112 | +} |
| 113 | + |
| 114 | +// PostConnections create clickup connection |
| 115 | +// @Summary create clickup connection |
| 116 | +// @Description Create clickup connection |
| 117 | +// @Tags plugins/clickup |
| 118 | +// @Param body body models.ClickUpConnection true "json body" |
| 119 | +// @Success 200 {object} models.ClickUpConnection |
| 120 | +// @Failure 400 {string} errcode.Error "Bad Request" |
| 121 | +// @Failure 500 {string} errcode.Error "Internal Error" |
| 122 | +// @Router /plugins/clickup/connections [POST] |
| 123 | +func PostConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 124 | + return dsHelper.ConnApi.Post(input) |
| 125 | +} |
| 126 | + |
| 127 | +// PatchConnection patch clickup connection |
| 128 | +// @Summary patch clickup connection |
| 129 | +// @Description Patch clickup connection |
| 130 | +// @Tags plugins/clickup |
| 131 | +// @Param body body models.ClickUpConnection true "json body" |
| 132 | +// @Success 200 {object} models.ClickUpConnection |
| 133 | +// @Failure 400 {string} errcode.Error "Bad Request" |
| 134 | +// @Failure 500 {string} errcode.Error "Internal Error" |
| 135 | +// @Router /plugins/clickup/connections/{connectionId} [PATCH] |
| 136 | +func PatchConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 137 | + return dsHelper.ConnApi.Patch(input) |
| 138 | +} |
| 139 | + |
| 140 | +// DeleteConnection delete a clickup connection |
| 141 | +// @Summary delete a clickup connection |
| 142 | +// @Description Delete a clickup connection |
| 143 | +// @Tags plugins/clickup |
| 144 | +// @Success 200 {object} models.ClickUpConnection |
| 145 | +// @Failure 400 {string} errcode.Error "Bad Request" |
| 146 | +// @Failure 409 {object} services.BlueprintProjectPairs "References exist to this connection" |
| 147 | +// @Failure 500 {string} errcode.Error "Internal Error" |
| 148 | +// @Router /plugins/clickup/connections/{connectionId} [DELETE] |
| 149 | +func DeleteConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 150 | + return dsHelper.ConnApi.Delete(input) |
| 151 | +} |
| 152 | + |
| 153 | +// ListConnections get all clickup connections |
| 154 | +// @Summary get all clickup connections |
| 155 | +// @Description Get all clickup connections |
| 156 | +// @Tags plugins/clickup |
| 157 | +// @Success 200 {object} []models.ClickUpConnection |
| 158 | +// @Failure 400 {string} errcode.Error "Bad Request" |
| 159 | +// @Failure 500 {string} errcode.Error "Internal Error" |
| 160 | +// @Router /plugins/clickup/connections [GET] |
| 161 | +func ListConnections(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 162 | + return dsHelper.ConnApi.GetAll(input) |
| 163 | +} |
| 164 | + |
| 165 | +// GetConnection get clickup connection detail |
| 166 | +// @Summary get clickup connection detail |
| 167 | +// @Description Get clickup connection detail |
| 168 | +// @Tags plugins/clickup |
| 169 | +// @Success 200 {object} models.ClickUpConnection |
| 170 | +// @Failure 400 {string} errcode.Error "Bad Request" |
| 171 | +// @Failure 500 {string} errcode.Error "Internal Error" |
| 172 | +// @Router /plugins/clickup/connections/{connectionId} [GET] |
| 173 | +func GetConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) { |
| 174 | + return dsHelper.ConnApi.GetDetail(input) |
| 175 | +} |
0 commit comments