-
Notifications
You must be signed in to change notification settings - Fork 492
/
mr_extractor.go
185 lines (171 loc) · 6.15 KB
/
mr_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
/*
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"
"github.com/apache/incubator-devlake/errors"
"github.com/apache/incubator-devlake/plugins/core"
"github.com/apache/incubator-devlake/plugins/gitlab/models"
"github.com/apache/incubator-devlake/plugins/helper"
"regexp"
)
type MergeRequestRes struct {
GitlabId int `json:"id"`
Iid int
ProjectId int `json:"project_id"`
SourceProjectId int `json:"source_project_id"`
TargetProjectId int `json:"target_project_id"`
State string
Title string
Description string
WebUrl string `json:"web_url"`
UserNotesCount int `json:"user_notes_count"`
WorkInProgress bool `json:"work_in_progress"`
SourceBranch string `json:"source_branch"`
TargetBranch string `json:"target_branch"`
GitlabCreatedAt helper.Iso8601Time `json:"created_at"`
MergedAt *helper.Iso8601Time `json:"merged_at"`
ClosedAt *helper.Iso8601Time `json:"closed_at"`
MergeCommitSha string `json:"merge_commit_sha"`
MergedBy struct {
Username string `json:"username"`
} `json:"merged_by"`
Author struct {
Id int `json:"id"`
Username string `json:"username"`
}
Reviewers []Reviewer
FirstCommentTime helper.Iso8601Time
Labels []string `json:"labels"`
}
type Reviewer struct {
GitlabId int `json:"id"`
MergeRequestId int
Name string
Username string
State string
AvatarUrl string `json:"avatar_url"`
WebUrl string `json:"web_url"`
}
var ExtractApiMergeRequestsMeta = core.SubTaskMeta{
Name: "extractApiMergeRequests",
EntryPoint: ExtractApiMergeRequests,
EnabledByDefault: true,
Description: "Extract raw merge requests data into tool layer table GitlabMergeRequest and GitlabReviewer",
DomainTypes: []string{core.DOMAIN_TYPE_CODE_REVIEW},
}
func ExtractApiMergeRequests(taskCtx core.SubTaskContext) errors.Error {
rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, RAW_MERGE_REQUEST_TABLE)
config := data.Options.TransformationRules
var labelTypeRegex *regexp.Regexp
var labelComponentRegex *regexp.Regexp
var prType = config.PrType
var err error
if len(prType) > 0 {
labelTypeRegex, err = regexp.Compile(prType)
if err != nil {
return errors.Default.Wrap(err, "regexp Compile prType failed")
}
}
var prComponent = config.PrComponent
if len(prComponent) > 0 {
labelComponentRegex, err = regexp.Compile(prComponent)
if err != nil {
return errors.Default.Wrap(err, "regexp Compile prComponent failed")
}
}
extractor, err := helper.NewApiExtractor(helper.ApiExtractorArgs{
RawDataSubTaskArgs: *rawDataSubTaskArgs,
Extract: func(row *helper.RawData) ([]interface{}, errors.Error) {
mr := &MergeRequestRes{}
err := errors.Convert(json.Unmarshal(row.Data, mr))
if err != nil {
return nil, err
}
gitlabMergeRequest, err := convertMergeRequest(mr)
if err != nil {
return nil, err
}
results := make([]interface{}, 0, len(mr.Reviewers)+1)
gitlabMergeRequest.ConnectionId = data.Options.ConnectionId
results = append(results, gitlabMergeRequest)
for _, label := range mr.Labels {
results = append(results, &models.GitlabMrLabel{
MrId: gitlabMergeRequest.GitlabId,
LabelName: label,
ConnectionId: data.Options.ConnectionId,
})
// if pr.Type has not been set and prType is set in .env, process the below
if labelTypeRegex != nil {
groups := labelTypeRegex.FindStringSubmatch(label)
if len(groups) > 0 {
gitlabMergeRequest.Type = groups[1]
}
}
// if pr.Component has not been set and prComponent is set in .env, process
if labelComponentRegex != nil {
groups := labelComponentRegex.FindStringSubmatch(label)
if len(groups) > 0 {
gitlabMergeRequest.Component = groups[1]
}
}
}
for _, reviewer := range mr.Reviewers {
gitlabReviewer := &models.GitlabReviewer{
ConnectionId: data.Options.ConnectionId,
GitlabId: reviewer.GitlabId,
MergeRequestId: mr.GitlabId,
ProjectId: data.Options.ProjectId,
Username: reviewer.Username,
Name: reviewer.Name,
State: reviewer.State,
AvatarUrl: reviewer.AvatarUrl,
WebUrl: reviewer.WebUrl,
}
results = append(results, gitlabReviewer)
}
return results, nil
},
})
if err != nil {
return errors.Convert(err)
}
return extractor.Execute()
}
func convertMergeRequest(mr *MergeRequestRes) (*models.GitlabMergeRequest, errors.Error) {
gitlabMergeRequest := &models.GitlabMergeRequest{
GitlabId: mr.GitlabId,
Iid: mr.Iid,
ProjectId: mr.ProjectId,
SourceProjectId: mr.SourceProjectId,
TargetProjectId: mr.TargetProjectId,
State: mr.State,
Title: mr.Title,
Description: mr.Description,
WebUrl: mr.WebUrl,
UserNotesCount: mr.UserNotesCount,
WorkInProgress: mr.WorkInProgress,
SourceBranch: mr.SourceBranch,
TargetBranch: mr.TargetBranch,
MergeCommitSha: mr.MergeCommitSha,
MergedAt: helper.Iso8601TimeToTime(mr.MergedAt),
GitlabCreatedAt: mr.GitlabCreatedAt.ToTime(),
ClosedAt: helper.Iso8601TimeToTime(mr.ClosedAt),
MergedByUsername: mr.MergedBy.Username,
AuthorUsername: mr.Author.Username,
AuthorUserId: mr.Author.Id,
}
return gitlabMergeRequest, nil
}