-
Notifications
You must be signed in to change notification settings - Fork 502
/
blueprint.go
211 lines (200 loc) · 6.28 KB
/
blueprint.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
/*
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"
"encoding/json"
"fmt"
"github.com/apache/incubator-devlake/errors"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/apache/incubator-devlake/models/domainlayer/didgen"
"github.com/apache/incubator-devlake/plugins/core"
"github.com/apache/incubator-devlake/plugins/gitee/models"
"github.com/apache/incubator-devlake/plugins/gitee/tasks"
"github.com/apache/incubator-devlake/plugins/helper"
"github.com/apache/incubator-devlake/utils"
)
func MakePipelinePlan(subtaskMetas []core.SubTaskMeta, connectionId uint64, scope []*core.BlueprintScopeV100) (core.PipelinePlan, errors.Error) {
var err errors.Error
connection := new(models.GiteeConnection)
err = connectionHelper.FirstById(connection, connectionId)
if err != nil {
return nil, err
}
token := strings.Split(connection.Token, ",")[0]
apiClient, err := helper.NewApiClient(
context.TODO(),
connection.Endpoint,
map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", token),
},
10*time.Second,
connection.Proxy,
basicRes,
)
if err != nil {
return nil, err
}
plan, err := makePipelinePlan(subtaskMetas, scope, apiClient, connection)
if err != nil {
return nil, err
}
return plan, nil
}
func makePipelinePlan(subtaskMetas []core.SubTaskMeta, scope []*core.BlueprintScopeV100, apiClient helper.ApiClientGetter, connection *models.GiteeConnection) (core.PipelinePlan, errors.Error) {
var err errors.Error
var repo *tasks.GiteeApiRepoResponse
plan := make(core.PipelinePlan, len(scope))
for i, scopeElem := range scope {
// handle taskOptions and transformationRules, by dumping them to taskOptions
transformationRules := make(map[string]interface{})
if len(scopeElem.Transformation) > 0 {
err = errors.Convert(json.Unmarshal(scopeElem.Transformation, &transformationRules))
if err != nil {
return nil, err
}
}
// refdiff
if refdiffRules, ok := transformationRules["refdiff"]; ok && refdiffRules != nil {
// add a new task to next stage
j := i + 1
if j == len(plan) {
plan = append(plan, nil)
}
plan[j] = core.PipelineStage{
{
Plugin: "refdiff",
Options: refdiffRules.(map[string]interface{}),
},
}
// remove it from github transformationRules
delete(transformationRules, "refdiff")
}
// construct task options for github
options := make(map[string]interface{})
err = errors.Convert(json.Unmarshal(scopeElem.Options, &options))
if err != nil {
return nil, err
}
options["connectionId"] = connection.ID
options["transformationRules"] = transformationRules
// make sure task options is valid
op, err := tasks.DecodeAndValidateTaskOptions(options)
if err != nil {
return nil, err
}
memorizedGetApiRepo := func() (*tasks.GiteeApiRepoResponse, errors.Error) {
if repo == nil {
repo, err = getApiRepo(op, apiClient)
}
return repo, err
}
// construct subtasks
subtasks, err := helper.MakePipelinePlanSubtasks(subtaskMetas, scopeElem.Entities)
if err != nil {
return nil, err
}
stage := plan[i]
if stage == nil {
stage = core.PipelineStage{}
}
stage = append(stage, &core.PipelineTask{
Plugin: "gitee",
Subtasks: subtasks,
Options: options,
})
// collect git data by gitextractor if CODE was requested
if utils.StringsContains(scopeElem.Entities, core.DOMAIN_TYPE_CODE) {
// here is the tricky part, we have to obtain the repo id beforehand
token := strings.Split(connection.Token, ",")[0]
repo, err = memorizedGetApiRepo()
if err != nil {
return nil, err
}
cloneUrl, err := errors.Convert01(url.Parse(repo.HTMLUrl))
if err != nil {
return nil, err
}
cloneUrl.User = url.UserPassword("git", token)
stage = append(stage, &core.PipelineTask{
Plugin: "gitextractor",
Options: map[string]interface{}{
"url": cloneUrl.String(),
"repoId": didgen.NewDomainIdGenerator(&models.GiteeRepo{}).Generate(connection.ID, repo.GiteeId),
"proxy": connection.Proxy,
},
})
}
// dora
if productionPattern, ok := transformationRules["productionPattern"]; ok && productionPattern != nil {
j := i + 1
if j == len(plan) {
plan = append(plan, nil)
}
// add a new task to next stage
if plan[j] != nil {
j++
}
if j == len(plan) {
plan = append(plan, nil)
}
repo, err = memorizedGetApiRepo()
if err != nil {
return nil, err
}
plan[j] = core.PipelineStage{
{
Plugin: "dora",
Subtasks: []string{"EnrichTaskEnv"},
Options: map[string]interface{}{
"repoId": didgen.NewDomainIdGenerator(&models.GiteeRepo{}).Generate(connection.ID, repo.GiteeId),
"transformationRules": map[string]interface{}{
"productionPattern": productionPattern,
},
},
},
}
// remove it from github transformationRules
delete(transformationRules, "productionPattern")
}
plan[i] = stage
repo = nil
}
return plan, nil
}
func getApiRepo(op *tasks.GiteeOptions, apiClient helper.ApiClientGetter) (*tasks.GiteeApiRepoResponse, errors.Error) {
apiRepo := &tasks.GiteeApiRepoResponse{}
res, err := apiClient.Get(fmt.Sprintf("repos/%s/%s", op.Owner, op.Repo), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("unexpected status code when requesting repo detail from %s", res.Request.URL.String()))
}
body, err := errors.Convert01(io.ReadAll(res.Body))
if err != nil {
return nil, err
}
err = errors.Convert(json.Unmarshal(body, apiRepo))
if err != nil {
return nil, err
}
return apiRepo, nil
}