forked from apache/incubator-devlake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpl.go
More file actions
292 lines (264 loc) · 8.58 KB
/
impl.go
File metadata and controls
292 lines (264 loc) · 8.58 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
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 impl
import (
"fmt"
"github.com/apache/incubator-devlake/helpers/pluginhelper/subtaskmeta/sorter"
"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/dal"
"github.com/apache/incubator-devlake/core/errors"
coreModels "github.com/apache/incubator-devlake/core/models"
"github.com/apache/incubator-devlake/core/models/domainlayer/devops"
"github.com/apache/incubator-devlake/core/plugin"
helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/gitlab/api"
"github.com/apache/incubator-devlake/plugins/gitlab/models"
"github.com/apache/incubator-devlake/plugins/gitlab/models/migrationscripts"
"github.com/apache/incubator-devlake/plugins/gitlab/tasks"
)
var _ interface {
plugin.PluginMeta
plugin.PluginInit
plugin.PluginTask
plugin.PluginModel
plugin.PluginMigration
plugin.PluginSource
plugin.DataSourcePluginBlueprintV200
plugin.CloseablePluginTask
} = (*Gitlab)(nil)
type Gitlab struct{}
func init() {
// check subtask meta loop when init subtask meta
if _, err := sorter.NewDependencySorter(tasks.SubTaskMetaList).Sort(); err != nil {
panic(err)
}
}
func (p Gitlab) Init(basicRes context.BasicRes) errors.Error {
api.Init(basicRes, p)
return nil
}
func (p Gitlab) Connection() dal.Tabler {
return &models.GitlabConnection{}
}
func (p Gitlab) Scope() plugin.ToolLayerScope {
return &models.GitlabProject{}
}
func (p Gitlab) ScopeConfig() dal.Tabler {
return &models.GitlabScopeConfig{}
}
func (p Gitlab) MakeDataSourcePipelinePlanV200(
connectionId uint64,
scopes []*coreModels.BlueprintScope,
) (coreModels.PipelinePlan, []plugin.Scope, errors.Error) {
return api.MakePipelinePlanV200(p.SubTaskMetas(), connectionId, scopes)
}
func (p Gitlab) GetTablesInfo() []dal.Tabler {
return []dal.Tabler{
&models.GitlabConnection{},
&models.GitlabAccount{},
&models.GitlabCommit{},
&models.GitlabIssue{},
&models.GitlabIssueLabel{},
&models.GitlabJob{},
&models.GitlabMergeRequest{},
&models.GitlabMrComment{},
&models.GitlabMrCommit{},
&models.GitlabMrLabel{},
&models.GitlabMrNote{},
&models.GitlabPipeline{},
&models.GitlabPipelineProject{},
&models.GitlabProject{},
&models.GitlabProjectCommit{},
&models.GitlabReviewer{},
&models.GitlabAssignee{},
&models.GitlabTag{},
&models.GitlabIssueAssignee{},
&models.GitlabScopeConfig{},
&models.GitlabDeployment{},
}
}
func (p Gitlab) Description() string {
return "To collect and enrich data from Gitlab"
}
func (p Gitlab) Name() string {
return "gitlab"
}
func (p Gitlab) SubTaskMetas() []plugin.SubTaskMeta {
list, err := sorter.NewDependencySorter(tasks.SubTaskMetaList).Sort()
if err != nil {
panic(err)
}
return list
}
func (p Gitlab) PrepareTaskData(taskCtx plugin.TaskContext, options map[string]interface{}) (interface{}, errors.Error) {
logger := taskCtx.GetLogger()
logger.Debug("%v", options)
op, err := tasks.DecodeAndValidateTaskOptions(options)
if err != nil {
return nil, err
}
if op.ConnectionId == 0 {
return nil, errors.BadInput.New("connectionId is invalid")
}
connection := &models.GitlabConnection{}
connectionHelper := helper.NewConnectionHelper(
taskCtx,
nil,
p.Name(),
)
if err != nil {
return nil, err
}
err = connectionHelper.FirstById(connection, op.ConnectionId)
if err != nil {
return nil, errors.BadInput.Wrap(err, "connection not found")
}
apiClient, err := tasks.NewGitlabApiClient(taskCtx, connection)
if err != nil {
return nil, err
}
if op.ProjectId != 0 {
var scope *models.GitlabProject
// support v100 & advance mode
// If we still cannot find the record in db, we have to request from remote server and save it to db
db := taskCtx.GetDal()
err = db.First(&scope, dal.Where("connection_id = ? AND gitlab_id = ?", op.ConnectionId, op.ProjectId))
if err == nil {
if op.ScopeConfigId == 0 && scope.ScopeConfigId != 0 {
op.ScopeConfigId = scope.ScopeConfigId
}
}
if err != nil && db.IsErrorNotFound(err) {
var project *models.GitlabApiProject
project, err = api.GetApiProject(op, apiClient)
if err != nil {
return nil, err
}
logger.Debug(fmt.Sprintf("Current project: %d", project.GitlabId))
scope := project.ConvertApiScope()
scope.ConnectionId = op.ConnectionId
err = taskCtx.GetDal().CreateIfNotExist(scope)
if err != nil {
return nil, err
}
}
if err != nil {
return nil, errors.Default.Wrap(err, fmt.Sprintf("fail to find project: %d", op.ProjectId))
}
}
if op.ScopeConfig == nil {
if op.ScopeConfigId != 0 {
var scopeConfig models.GitlabScopeConfig
db := taskCtx.GetDal()
err = db.First(&scopeConfig, dal.Where("id = ?", op.ScopeConfigId))
if err != nil {
if db.IsErrorNotFound(err) {
return nil, errors.Default.Wrap(err, fmt.Sprintf("can not find scopeConfigs by scopeConfigId [%d]", op.ScopeConfigId))
}
return nil, errors.Default.Wrap(err, fmt.Sprintf("fail to find scopeConfigs by scopeConfigId [%d]", op.ScopeConfigId))
}
op.ScopeConfig = &scopeConfig
} else {
op.ScopeConfig = &models.GitlabScopeConfig{}
}
}
regexEnricher := helper.NewRegexEnricher()
if err := regexEnricher.TryAdd(devops.DEPLOYMENT, op.ScopeConfig.DeploymentPattern); err != nil {
return nil, errors.BadInput.Wrap(err, "invalid value for `deploymentPattern`")
}
if err := regexEnricher.TryAdd(devops.PRODUCTION, op.ScopeConfig.ProductionPattern); err != nil {
return nil, errors.BadInput.Wrap(err, "invalid value for `productionPattern`")
}
if err := regexEnricher.TryAdd(devops.ENV_NAME_PATTERN, op.ScopeConfig.EnvNamePattern); err != nil {
return nil, errors.BadInput.Wrap(err, "invalid value for `envNamePattern`")
}
cfg := taskCtx.GetConfigReader()
op.CollectAllUsers = true
if cfg.IsSet("GITLAB_SERVER_COLLECT_ALL_USERS") {
op.CollectAllUsers = cfg.GetBool("GITLAB_SERVER_COLLECT_ALL_USERS")
}
taskData := tasks.GitlabTaskData{
Options: op,
ApiClient: apiClient,
RegexEnricher: regexEnricher,
}
return &taskData, nil
}
func (p Gitlab) RootPkgPath() string {
return "github.com/apache/incubator-devlake/plugins/gitlab"
}
func (p Gitlab) MigrationScripts() []plugin.MigrationScript {
return migrationscripts.All()
}
func (p Gitlab) ApiResources() map[string]map[string]plugin.ApiResourceHandler {
return map[string]map[string]plugin.ApiResourceHandler{
"test": {
"POST": api.TestConnection,
},
"connections": {
"POST": api.PostConnections,
"GET": api.ListConnections,
},
"connections/:connectionId": {
"PATCH": api.PatchConnection,
"DELETE": api.DeleteConnection,
"GET": api.GetConnection,
},
"connections/:connectionId/test": {
"POST": api.TestExistingConnection,
},
"connections/:connectionId/scopes/:scopeId": {
"GET": api.GetScope,
"PATCH": api.PatchScope,
"DELETE": api.DeleteScope,
},
"connections/:connectionId/scopes/:scopeId/latest-sync-state": {
"GET": api.GetScopeLatestSyncState,
},
"connections/:connectionId/remote-scopes": {
"GET": api.RemoteScopes,
},
"connections/:connectionId/search-remote-scopes": {
"GET": api.SearchRemoteScopes,
},
"connections/:connectionId/scopes": {
"GET": api.GetScopeList,
"PUT": api.PutScopes,
},
"connections/:connectionId/scope-configs": {
"POST": api.CreateScopeConfig,
"GET": api.GetScopeConfigList,
},
"connections/:connectionId/scope-configs/:scopeConfigId": {
"PATCH": api.PatchScopeConfig,
"GET": api.GetScopeConfig,
"DELETE": api.DeleteScopeConfig,
},
"connections/:connectionId/proxy/rest/*path": {
"GET": api.Proxy,
},
"scope-config/:scopeConfigId/projects": {
"GET": api.GetProjectsByScopeConfig,
},
}
}
func (p Gitlab) Close(taskCtx plugin.TaskContext) errors.Error {
data, ok := taskCtx.GetData().(*tasks.GitlabTaskData)
if !ok {
return errors.Default.New(fmt.Sprintf("GetData failed when try to close %+v", taskCtx))
}
data.ApiClient.Release()
return nil
}