-
Notifications
You must be signed in to change notification settings - Fork 501
/
issue_extractor.go
246 lines (231 loc) · 8.02 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
/*
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"
"strconv"
"strings"
"time"
"github.com/apache/incubator-devlake/core/dal"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/plugin"
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/jira/models"
"github.com/apache/incubator-devlake/plugins/jira/tasks/apiv2models"
)
var _ plugin.SubTaskEntryPoint = ExtractIssues
var ExtractIssuesMeta = plugin.SubTaskMeta{
Name: "extractIssues",
EntryPoint: ExtractIssues,
EnabledByDefault: true,
Description: "extract Jira issues",
DomainTypes: []string{plugin.DOMAIN_TYPE_TICKET, plugin.DOMAIN_TYPE_CROSS},
}
type typeMappings struct {
typeIdMappings map[string]string
stdTypeMappings map[string]string
standardStatusMappings map[string]models.StatusMappings
}
func ExtractIssues(subtaskCtx plugin.SubTaskContext) errors.Error {
data := subtaskCtx.GetData().(*JiraTaskData)
db := subtaskCtx.GetDal()
connectionId := data.Options.ConnectionId
boardId := data.Options.BoardId
logger := subtaskCtx.GetLogger()
logger.Info("extract Issues, connection_id=%d, board_id=%d", connectionId, boardId)
mappings, err := getTypeMappings(data, db)
if err != nil {
return err
}
userFieldMap, err := getUserFieldMap(db, connectionId, logger)
if err != nil {
return err
}
extractor, err := api.NewStatefulApiExtractor(&api.StatefulApiExtractorArgs{
SubtaskCommonArgs: &api.SubtaskCommonArgs{
SubTaskContext: subtaskCtx,
Table: RAW_ISSUE_TABLE,
Params: JiraApiParams{
ConnectionId: data.Options.ConnectionId,
BoardId: data.Options.BoardId,
},
SubtaskConfig: mappings,
},
Extract: func(row *api.RawData) ([]interface{}, errors.Error) {
return extractIssues(data, mappings, row, userFieldMap)
},
})
if err != nil {
return err
}
return extractor.Execute()
}
func extractIssues(data *JiraTaskData, mappings *typeMappings, row *api.RawData, userFieldMaps map[string]struct{}) ([]interface{}, errors.Error) {
var apiIssue apiv2models.Issue
err := errors.Convert(json.Unmarshal(row.Data, &apiIssue))
if err != nil {
return nil, err
}
err = apiIssue.SetAllFields(row.Data)
if err != nil {
return nil, err
}
var results []interface{}
// if the field `created` is nil, ignore it
if apiIssue.Fields.Created == nil {
return results, nil
}
sprints, issue, comments, worklogs, changelogs, changelogItems, users := apiIssue.ExtractEntities(data.Options.ConnectionId, userFieldMaps)
for _, sprintId := range sprints {
sprintIssue := &models.JiraSprintIssue{
ConnectionId: data.Options.ConnectionId,
SprintId: sprintId,
IssueId: issue.IssueId,
IssueCreatedDate: &issue.Created,
ResolutionDate: issue.ResolutionDate,
}
results = append(results, sprintIssue)
}
if issue.ResolutionDate != nil {
temp := uint(issue.ResolutionDate.Unix()-issue.Created.Unix()) / 60
issue.LeadTimeMinutes = &temp
}
if data.Options.ScopeConfig != nil && data.Options.ScopeConfig.StoryPointField != "" {
unknownStoryPoint := apiIssue.Fields.AllFields[data.Options.ScopeConfig.StoryPointField]
switch sp := unknownStoryPoint.(type) {
case string:
// string, try to parse
temp, _ := strconv.ParseFloat(sp, 32)
issue.StoryPoint = &temp
case nil:
default:
// not string, convert to float64, ignore it if failed
temp, _ := unknownStoryPoint.(float64)
issue.StoryPoint = &temp
}
}
// code in next line will set issue.Type to issueType.Name
issue.Type = mappings.typeIdMappings[issue.Type]
issue.StdType = mappings.stdTypeMappings[issue.Type]
if issue.StdType == "" {
issue.StdType = strings.ToUpper(issue.Type)
}
issue.StdStatus = getStdStatus(issue.StatusKey)
if value, ok := mappings.standardStatusMappings[issue.Type][issue.StatusKey]; ok {
issue.StdStatus = value.StandardStatus
}
// issue commments
results = append(results, issue)
for _, comment := range comments {
results = append(results, comment)
}
// worklogs
for _, worklog := range worklogs {
results = append(results, worklog)
}
var issueUpdated *time.Time
// likely this issue has more changelogs to be collected
if len(changelogs) == 100 {
issueUpdated = nil
} else {
issueUpdated = &issue.Updated
}
// changelogs
for _, changelog := range changelogs {
changelog.IssueUpdated = issueUpdated
results = append(results, changelog)
}
// changelog items
for _, changelogItem := range changelogItems {
results = append(results, changelogItem)
}
// users
for _, user := range users {
if user.AccountId != "" {
results = append(results, user)
}
}
results = append(results, &models.JiraBoardIssue{
ConnectionId: data.Options.ConnectionId,
BoardId: data.Options.BoardId,
IssueId: issue.IssueId,
})
// labels
labels := apiIssue.Fields.Labels
for _, v := range labels {
issueLabel := &models.JiraIssueLabel{
IssueId: issue.IssueId,
LabelName: v,
ConnectionId: data.Options.ConnectionId,
}
results = append(results, issueLabel)
}
// components
components := apiIssue.Fields.Components
var componentNames []string
for _, v := range components {
componentNames = append(componentNames, v.Name)
}
issue.Components = strings.Join(componentNames, ",")
// issuelinks
issuelinks := apiIssue.Fields.Issuelinks
for _, v := range issuelinks {
issueLink := &models.JiraIssueRelationship{
ConnectionId: data.Options.ConnectionId,
IssueId: issue.IssueId,
IssueKey: issue.IssueKey,
TypeId: v.Type.ID, // Extracting the TypeId from the issuelink
TypeName: v.Type.Name, // Extracting the TypeName from the issuelink
Inward: v.Type.Inward, // Extracting the Inward from the issuelink
Outward: v.Type.Outward, // Extracting the Outward from the issuelink
InwardIssueId: v.InwardIssue.ID, // Extracting the InwardIssueId from the issuelink
InwardIssueKey: v.InwardIssue.Key, // Extracting the InwardIssueKey from the issuelink
OutwardIssueId: v.OutwardIssue.ID, // Extracting the OutwardIssueId from the issuelink
OutwardIssueKey: v.OutwardIssue.Key, // Extracting the OutwardIssueKey from the issuelink
}
results = append(results, issueLink)
}
// is subtask
issue.Subtask = apiIssue.Fields.Issuetype.Subtask
return results, nil
}
func getTypeMappings(data *JiraTaskData, db dal.Dal) (*typeMappings, errors.Error) {
typeIdMapping := make(map[string]string)
issueTypes := make([]models.JiraIssueType, 0)
clauses := []dal.Clause{
dal.From(&models.JiraIssueType{}),
dal.Where("connection_id = ?", data.Options.ConnectionId),
}
err := db.All(&issueTypes, clauses...)
if err != nil {
return nil, err
}
for _, issueType := range issueTypes {
typeIdMapping[issueType.Id] = issueType.Name
}
stdTypeMappings := make(map[string]string)
standardStatusMappings := make(map[string]models.StatusMappings)
if data.Options.ScopeConfig != nil {
for userType, stdType := range data.Options.ScopeConfig.TypeMappings {
stdTypeMappings[userType] = strings.ToUpper(stdType.StandardType)
standardStatusMappings[userType] = stdType.StatusMappings
}
}
return &typeMappings{
typeIdMappings: typeIdMapping,
stdTypeMappings: stdTypeMappings,
standardStatusMappings: standardStatusMappings,
}, nil
}