forked from apache/incubator-devlake
/
pipeline_extractor.go
152 lines (130 loc) · 4.71 KB
/
pipeline_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
package tasks
import (
"encoding/json"
"github.com/merico-dev/lake/plugins/core"
"github.com/merico-dev/lake/plugins/gitlab/models"
"github.com/merico-dev/lake/plugins/helper"
)
type ApiPipeline struct {
GitlabId int `json:"id"`
ProjectId int `json:"project_id"`
GitlabCreatedAt *core.Iso8601Time `json:"created_at"`
UpdatedAt *core.Iso8601Time `json:"updated_at"`
Ref string
Sha string
Duration int
WebUrl string `json:"web_url"`
Status string
}
type ApiSinglePipelineResponse struct {
GitlabId int `json:"id"`
ProjectId int `json:"project_id"`
GitlabCreatedAt *core.Iso8601Time `json:"created_at"`
Ref string
Sha string
WebUrl string `json:"web_url"`
Duration int
UpdatedAt *core.Iso8601Time `json:"updated_at"`
Coverage string
Status string
}
var ExtractApiPipelinesMeta = core.SubTaskMeta{
Name: "extractApiPipelines",
EntryPoint: ExtractApiPipelines,
EnabledByDefault: true,
Description: "Extract raw pipelines data into tool layer table GitlabPipeline",
}
var ExtractApiChildrenOnPipelinesMeta = core.SubTaskMeta{
Name: "extractApiChildrenOnPipelines",
EntryPoint: ExtractApiChildrenOnPipelines,
EnabledByDefault: true,
Description: "Extract raw pipelines data into tool layer table GitlabPipeline",
}
func ExtractApiPipelines(taskCtx core.SubTaskContext) error {
rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, RAW_PIPELINE_TABLE)
extractor, err := helper.NewApiExtractor(helper.ApiExtractorArgs{
RawDataSubTaskArgs: *rawDataSubTaskArgs,
Extract: func(row *helper.RawData) ([]interface{}, error) {
// create gitlab commit
gitlabApiPipeline := &ApiPipeline{}
err := json.Unmarshal(row.Data, gitlabApiPipeline)
if err != nil {
return nil, err
}
duration := int(gitlabApiPipeline.UpdatedAt.ToTime().Sub(gitlabApiPipeline.GitlabCreatedAt.ToTime()).Seconds())
gitlabApiPipeline.Duration = duration
gitlabPipeline, err := convertPipeline(gitlabApiPipeline)
if err != nil {
return nil, err
}
// use data.Options.ProjectId to set the value of ProjectId for it
gitlabPipeline.ProjectId = data.Options.ProjectId
results := make([]interface{}, 0, 1)
results = append(results, gitlabPipeline)
return results, nil
},
})
if err != nil {
return err
}
return extractor.Execute()
}
func ExtractApiChildrenOnPipelines(taskCtx core.SubTaskContext) error {
rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, RAW_CHILDREN_ON_PIPELINE_TABLE)
extractor, err := helper.NewApiExtractor(helper.ApiExtractorArgs{
RawDataSubTaskArgs: *rawDataSubTaskArgs,
Extract: func(row *helper.RawData) ([]interface{}, error) {
pipelineRes := &ApiSinglePipelineResponse{}
err := json.Unmarshal(row.Data, pipelineRes)
if err != nil {
return nil, err
}
duration := int(pipelineRes.UpdatedAt.ToTime().Sub(pipelineRes.GitlabCreatedAt.ToTime()).Seconds())
pipelineRes.Duration = duration
gitlabPipeline, err := convertSinglePipeline(pipelineRes)
if err != nil {
return nil, err
}
// use data.Options.ProjectId to set the value of ProjectId for it
gitlabPipeline.ProjectId = data.Options.ProjectId
results := make([]interface{}, 0, 1)
results = append(results, gitlabPipeline)
return results, nil
},
})
if err != nil {
return err
}
return extractor.Execute()
}
func convertSinglePipeline(pipeline *ApiSinglePipelineResponse) (*models.GitlabPipeline, error) {
gitlabPipeline := &models.GitlabPipeline{
GitlabId: pipeline.GitlabId,
ProjectId: pipeline.ProjectId,
GitlabCreatedAt: pipeline.GitlabCreatedAt.ToTime(),
Ref: pipeline.Ref,
Sha: pipeline.Sha,
WebUrl: pipeline.WebUrl,
Duration: pipeline.Duration,
StartedAt: core.Iso8601TimeToTime(pipeline.GitlabCreatedAt),
FinishedAt: core.Iso8601TimeToTime(pipeline.UpdatedAt),
Coverage: pipeline.Coverage,
Status: pipeline.Status,
}
return gitlabPipeline, nil
}
func convertPipeline(pipeline *ApiPipeline) (*models.GitlabPipeline, error) {
gitlabPipeline := &models.GitlabPipeline{
GitlabId: pipeline.GitlabId,
ProjectId: pipeline.ProjectId,
GitlabCreatedAt: pipeline.GitlabCreatedAt.ToTime(),
Ref: pipeline.Ref,
Sha: pipeline.Sha,
WebUrl: pipeline.WebUrl,
Status: pipeline.Status,
StartedAt: core.Iso8601TimeToTime(pipeline.GitlabCreatedAt),
FinishedAt: core.Iso8601TimeToTime(pipeline.UpdatedAt),
Duration: pipeline.Duration,
}
return gitlabPipeline, nil
}