-
Notifications
You must be signed in to change notification settings - Fork 496
/
issue_extractor.go
291 lines (269 loc) · 8.26 KB
/
issue_extractor.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
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
/*
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 tasks
import (
"encoding/json"
"regexp"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models/common"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/gitlab/models"
)
func init() {
RegisterSubtaskMeta(&ExtractApiIssuesMeta)
}
var ExtractApiIssuesMeta = plugin.SubTaskMeta{
Name: "Extract Issues",
EntryPoint: ExtractApiIssues,
EnabledByDefault: true,
Description: "Extract raw Issues data into tool layer table gitlab_issues",
DomainTypes: []string{plugin.DOMAIN_TYPE_TICKET},
Dependencies: []*plugin.SubTaskMeta{&CollectApiIssuesMeta},
}
type IssuesResponse struct {
ProjectId int `json:"project_id"`
Milestone struct {
Due_date string
Project_id int
State string
Description string
Iid int
Id int
Title string
CreatedAt common.Iso8601Time
UpdatedAt common.Iso8601Time
}
Author *struct {
State string
WebUrl string `json:"web_url"`
AvatarUrl string `json:"avatar_url"`
Username string
Id int `json:"id"`
Name string `json:"name"`
}
Description string
State string
Iid int
Assignees []struct {
AvatarUrl string `json:"avatar_url"`
WebUrl string `json:"web_url"`
State string
Username string
Id int
Name string
}
Assignee *struct {
AvatarUrl string
WebUrl string
State string
Username string
Id int
Name string
}
Type string
Labels []string `json:"labels"`
UpVotes int
DownVotes int
MergeRequestsCount int
Id int `json:"id"`
Title string
GitlabUpdatedAt common.Iso8601Time `json:"updated_at"`
GitlabCreatedAt common.Iso8601Time `json:"created_at"`
GitlabClosedAt *common.Iso8601Time `json:"closed_at"`
ClosedBy struct {
State string
WebUrl string
AvatarUrl string
Username string
Id int
Name string
}
UserNotesCount int
DueDate common.Iso8601Time
WebUrl string `json:"web_url"`
References struct {
Short string
Relative string
Full string
}
TimeStats struct {
TimeEstimate *int64
TotalTimeSpent *int64
HumanTimeEstimate string
HumanTotalTimeSpent string
}
HasTasks bool
TaskStatus string
Confidential bool
DiscussionLocked bool
IssueType string
Severity string
Component string
Priority string
Links struct {
Self string `json:"url"`
Notes string
AwardEmoji string
Project string
}
TaskCompletionStatus struct {
Count int
CompletedCount int
}
}
func ExtractApiIssues(taskCtx plugin.SubTaskContext) errors.Error {
rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, RAW_ISSUE_TABLE)
config := data.Options.ScopeConfig
var issueSeverityRegex *regexp.Regexp
var issueComponentRegex *regexp.Regexp
var issuePriorityRegex *regexp.Regexp
var issueSeverity = config.IssueSeverity
var err error
if len(issueSeverity) > 0 {
issueSeverityRegex, err = regexp.Compile(issueSeverity)
if err != nil {
return errors.Default.Wrap(err, "regexp Compile issueSeverity failed")
}
}
var issueComponent = config.IssueComponent
if len(issueComponent) > 0 {
issueComponentRegex, err = regexp.Compile(issueComponent)
if err != nil {
return errors.Default.Wrap(err, "regexp Compile issueComponent failed")
}
}
var issuePriority = config.IssuePriority
if len(issuePriority) > 0 {
issuePriorityRegex, err = regexp.Compile(issuePriority)
if err != nil {
return errors.Default.Wrap(err, "regexp Compile issuePriority failed")
}
}
extractor, err := api.NewApiExtractor(api.ApiExtractorArgs{
RawDataSubTaskArgs: *rawDataSubTaskArgs,
Extract: func(row *api.RawData) ([]interface{}, errors.Error) {
body := &IssuesResponse{}
err := errors.Convert(json.Unmarshal(row.Data, body))
if err != nil {
return nil, err
}
if body.ProjectId == 0 {
return nil, nil
}
results := make([]interface{}, 0, 2)
gitlabIssue, err := convertGitlabIssue(body, data.Options.ProjectId)
if err != nil {
return nil, err
}
for _, label := range body.Labels {
results = append(results, &models.GitlabIssueLabel{
IssueId: gitlabIssue.GitlabId,
LabelName: label,
ConnectionId: data.Options.ConnectionId,
})
if issueSeverityRegex != nil && issueSeverityRegex.MatchString(label) {
gitlabIssue.Severity = label
}
if issueComponentRegex != nil && issueComponentRegex.MatchString(label) {
gitlabIssue.Component = label
}
if issuePriorityRegex != nil && issuePriorityRegex.MatchString(label) {
gitlabIssue.Priority = label
}
}
gitlabIssue.ConnectionId = data.Options.ConnectionId
if body.Author != nil {
gitlabAuthor, err := convertGitlabAuthor(body, data.Options.ConnectionId)
if err != nil {
return nil, err
}
results = append(results, gitlabAuthor)
}
results = append(results, gitlabIssue)
for _, v := range body.Assignees {
assignee := &models.GitlabAccount{
ConnectionId: data.Options.ConnectionId,
Username: v.Username,
Name: v.Name,
State: v.State,
AvatarUrl: v.AvatarUrl,
WebUrl: v.WebUrl,
}
issueAssignee := &models.GitlabIssueAssignee{
ConnectionId: data.Options.ConnectionId,
GitlabId: gitlabIssue.GitlabId,
ProjectId: gitlabIssue.ProjectId,
AssigneeId: v.Id,
AssigneeName: v.Username,
}
results = append(results, assignee, issueAssignee)
}
return results, nil
},
})
if err != nil {
return errors.Convert(err)
}
return extractor.Execute()
}
func convertGitlabIssue(issue *IssuesResponse, projectId int) (*models.GitlabIssue, errors.Error) {
gitlabIssue := &models.GitlabIssue{
GitlabId: issue.Id,
ProjectId: projectId,
Number: issue.Iid,
State: issue.State,
Type: issue.Type,
StdType: issue.Type,
Severity: issue.Severity,
Component: issue.Component,
Priority: issue.Priority,
Title: issue.Title,
Body: issue.Description,
Url: issue.Links.Self,
ClosedAt: common.Iso8601TimeToTime(issue.GitlabClosedAt),
GitlabCreatedAt: issue.GitlabCreatedAt.ToTime(),
GitlabUpdatedAt: issue.GitlabUpdatedAt.ToTime(),
TimeEstimate: issue.TimeStats.TimeEstimate,
TotalTimeSpent: issue.TimeStats.TotalTimeSpent,
CreatorId: issue.Author.Id,
CreatorName: issue.Author.Username,
}
if issue.Assignee != nil {
gitlabIssue.AssigneeId = issue.Assignee.Id
gitlabIssue.AssigneeName = issue.Assignee.Username
}
if issue.Author != nil {
gitlabIssue.CreatorId = issue.Author.Id
gitlabIssue.CreatorName = issue.Author.Username
}
if issue.GitlabClosedAt != nil {
temp := uint(issue.GitlabClosedAt.ToTime().Sub(issue.GitlabCreatedAt.ToTime()).Minutes())
gitlabIssue.LeadTimeMinutes = &temp
}
return gitlabIssue, nil
}
func convertGitlabAuthor(issue *IssuesResponse, connectionId uint64) (*models.GitlabAccount, errors.Error) {
gitlabAuthor := &models.GitlabAccount{
ConnectionId: connectionId,
GitlabId: issue.Author.Id,
Username: issue.Author.Username,
Name: issue.Author.Name,
State: issue.Author.State,
AvatarUrl: issue.Author.AvatarUrl,
WebUrl: issue.Author.WebUrl,
}
return gitlabAuthor, nil
}