-
Notifications
You must be signed in to change notification settings - Fork 523
/
connection.go
161 lines (145 loc) · 5.53 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"context"
"github.com/apache/incubator-devlake/errors"
"net/http"
"net/url"
"time"
"github.com/apache/incubator-devlake/plugins/gitee/models"
"github.com/apache/incubator-devlake/plugins/helper"
"github.com/mitchellh/mapstructure"
"github.com/apache/incubator-devlake/plugins/core"
)
// @Summary test gitee connection
// @Description Test gitee Connection
// @Tags plugins/gitee
// @Param body body models.TestConnectionRequest true "json body"
// @Success 200 {object} shared.ApiBody "Success"
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internel Error"
// @Router /plugins/gitee/test [POST]
func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
var connection models.TestConnectionRequest
if err := mapstructure.Decode(input.Body, &connection); err != nil {
return nil, errors.BadInput.Wrap(err, "could not decode request parameters")
}
if err := vld.Struct(connection); err != nil {
return nil, errors.BadInput.Wrap(err, "could not validate request parameters")
}
// test connection
apiClient, err := helper.NewApiClient(
context.TODO(),
connection.Endpoint,
nil,
3*time.Second,
connection.Proxy,
basicRes,
)
if err != nil {
return nil, err
}
query := url.Values{}
query.Set("access_token", connection.Token)
res, err := apiClient.Get("user", query, nil)
if err != nil {
return nil, err
}
resBody := &models.ApiUserResponse{}
err = helper.UnmarshalResponse(res, resBody)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, errors.HttpStatus(res.StatusCode).New("unexpected status code when testing connection")
}
return nil, nil
}
// @Summary create gitee connection
// @Description Create gitee connection
// @Tags plugins/gitee
// @Param body body models.GithubConnection true "json body"
// @Success 200 {object} models.GiteeConnection
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internel Error"
// @Router /plugins/gitee/connections [POST]
func PostConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
connection := &models.GiteeConnection{}
err := connectionHelper.Create(connection, input)
if err != nil {
return nil, err
}
return &core.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil
}
// @Summary patch gitee connection
// @Description Patch gitee connection
// @Tags plugins/gitee
// @Param body body models.GithubConnection true "json body"
// @Success 200 {object} models.GiteeConnection
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internel Error"
// @Router /plugins/gitee/connections/{connectionId} [PATCH]
func PatchConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
connection := &models.GiteeConnection{}
err := connectionHelper.Patch(connection, input)
if err != nil {
return nil, err
}
return &core.ApiResourceOutput{Body: connection, Status: http.StatusOK}, nil
}
// @Summary delete a gitee connection
// @Description Delete a gitee connection
// @Tags plugins/gitee
// @Success 200 {object} models.GiteeConnection
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internel Error"
// @Router /plugins/gitee/connections/{connectionId} [DELETE]
func DeleteConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
connection := &models.GiteeConnection{}
err := connectionHelper.First(connection, input.Params)
if err != nil {
return nil, err
}
err = connectionHelper.Delete(connection)
return &core.ApiResourceOutput{Body: connection}, err
}
// @Summary get all gitee connections
// @Description Get all gitee connections
// @Tags plugins/gitee
// @Success 200 {object} models.GiteeConnection
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internel Error"
// @Router /plugins/gitee/connections [GET]
func ListConnections(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
var connections []models.GiteeConnection
err := connectionHelper.List(&connections)
if err != nil {
return nil, err
}
return &core.ApiResourceOutput{Body: connections}, nil
}
// @Summary get gitee connection detail
// @Description Get gitee connection detail
// @Tags plugins/gitee
// @Success 200 {object} models.GiteeConnection
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internel Error"
// @Router /plugins/gitee/connections/{connectionId} [GET]
func GetConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
connection := &models.GiteeConnection{}
err := connectionHelper.First(connection, input.Params)
return &core.ApiResourceOutput{Body: connection}, err
}