-
Notifications
You must be signed in to change notification settings - Fork 805
Expand file tree
/
Copy pathjob_collector.go
More file actions
229 lines (207 loc) · 7.1 KB
/
Copy pathjob_collector.go
File metadata and controls
229 lines (207 loc) · 7.1 KB
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
/*
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"
"reflect"
"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/core/utils"
helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/github/models"
githubTasks "github.com/apache/incubator-devlake/plugins/github/tasks"
"github.com/merico-dev/graphql"
)
const RAW_GRAPHQL_JOBS_TABLE = "github_graphql_jobs"
type GraphqlQueryCheckRunWrapper struct {
RateLimit struct {
Cost int
}
Node []GraphqlQueryCheckSuite `graphql:"node(id: $id)" graphql-extend:"true"`
}
type GraphqlQueryCheckSuite struct {
Id string
Typename string `graphql:"__typename"`
// equal to Run in rest
CheckSuite struct {
WorkflowRun struct {
DatabaseId int
}
// equal to Job in rest
CheckRuns struct {
TotalCount int
PageInfo struct {
EndCursor string `graphql:"endCursor"`
HasNextPage bool `graphql:"hasNextPage"`
}
Nodes []GraphqlQueryCheckRun
} `graphql:"checkRuns(first: $pageSize, after: $skipCursor)"`
} `graphql:"... on CheckSuite"`
}
type GraphqlQueryCheckRun struct {
Id string
Name string
DetailsUrl string
DatabaseId int
Status string
StartedAt *time.Time
Conclusion string
CompletedAt *time.Time
// ExternalId string
// Url string
// Title interface{}
// Text interface{}
// Summary interface{}
Steps struct {
TotalCount int
Nodes []struct {
CompletedAt *time.Time `json:"completed_at"`
Conclusion string `json:"conclusion"`
Name string `json:"name"`
Number int `json:"number"`
SecondsToCompletion int `json:"seconds_to_completion"`
StartedAt *time.Time `json:"started_at"`
Status string `json:"status"`
}
} `graphql:"steps(first: 50)"`
}
type SimpleWorkflowRun struct {
CheckSuiteNodeID string
}
// DbCheckRun is used to store additional fields (like RunId) required for database storage
// and application logic, while embedding the GraphqlQueryCheckRun struct for API data.
type DbCheckRun struct {
RunId int // WorkflowRunId, required for DORA calculation
*GraphqlQueryCheckRun
}
var CollectJobsMeta = plugin.SubTaskMeta{
Name: "Collect Job Runs",
EntryPoint: CollectJobs,
EnabledByDefault: true,
Description: "Collect Jobs(CheckRun) data from GithubGraphql api, supports both timeFilter and diffSync.",
DomainTypes: []string{plugin.DOMAIN_TYPE_CICD},
}
var _ plugin.SubTaskEntryPoint = CollectJobs
func getPageInfo(query interface{}, args *helper.GraphqlCollectorArgs) (*helper.GraphqlQueryPageInfo, error) {
queryWrapper := query.(*GraphqlQueryCheckRunWrapper)
hasNextPage := false
endCursor := ""
for _, node := range queryWrapper.Node {
if node.CheckSuite.CheckRuns.PageInfo.HasNextPage {
hasNextPage = true
endCursor = node.CheckSuite.CheckRuns.PageInfo.EndCursor
break
}
}
return &helper.GraphqlQueryPageInfo{
EndCursor: endCursor,
HasNextPage: hasNextPage,
}, nil
}
func buildQuery(reqData *helper.GraphqlRequestData) (interface{}, map[string]interface{}, error) {
query := &GraphqlQueryCheckRunWrapper{}
if reqData == nil {
return query, map[string]interface{}{}, nil
}
workflowRuns := reqData.Input.([]interface{})
checkSuiteIds := []map[string]interface{}{}
for _, iWorkflowRuns := range workflowRuns {
workflowRun := iWorkflowRuns.(*SimpleWorkflowRun)
checkSuiteIds = append(checkSuiteIds, map[string]interface{}{
`id`: graphql.ID(workflowRun.CheckSuiteNodeID),
})
}
variables := map[string]interface{}{
"node": checkSuiteIds,
"pageSize": graphql.Int(reqData.Pager.Size),
"skipCursor": (*graphql.String)(reqData.Pager.SkipCursor),
}
return query, variables, nil
}
func CollectJobs(taskCtx plugin.SubTaskContext) errors.Error {
db := taskCtx.GetDal()
data := taskCtx.GetData().(*githubTasks.GithubTaskData)
apiCollector, err := helper.NewStatefulApiCollector(helper.RawDataSubTaskArgs{
Ctx: taskCtx,
Params: githubTasks.GithubApiParams{
ConnectionId: data.Options.ConnectionId,
Name: data.Options.Name,
},
Table: RAW_GRAPHQL_JOBS_TABLE,
})
if err != nil {
return err
}
clauses := []dal.Clause{
dal.Select("check_suite_node_id"),
dal.From(models.GithubRun{}.TableName()),
dal.Where("repo_id = ? and connection_id=?", data.Options.GithubId, data.Options.ConnectionId),
dal.Orderby("github_updated_at DESC"),
}
if apiCollector.IsIncremental() && apiCollector.GetSince() != nil {
clauses = append(clauses, dal.Where("github_updated_at > ?", *apiCollector.GetSince()))
}
cursor, err := db.Cursor(
clauses...,
)
if err != nil {
return err
}
defer cursor.Close()
iterator, err := helper.NewDalCursorIterator(db, cursor, reflect.TypeOf(SimpleWorkflowRun{}))
if err != nil {
return err
}
err = apiCollector.InitGraphQLCollector(helper.GraphqlCollectorArgs{
Input: iterator,
InputStep: 10,
GraphqlClient: data.GraphqlClient,
BuildQuery: buildQuery,
GetPageInfo: getPageInfo,
ResponseParser: func(queryWrapper any) (messages []json.RawMessage, err errors.Error) {
query := queryWrapper.(*GraphqlQueryCheckRunWrapper)
for _, node := range query.Node {
runId := node.CheckSuite.WorkflowRun.DatabaseId
for _, checkRun := range node.CheckSuite.CheckRuns.Nodes {
dbCheckRun := &DbCheckRun{
RunId: runId,
GraphqlQueryCheckRun: &checkRun,
}
// A checkRun without a startedAt time is a run that was never started (skipped), GitHub returns
// a ZeroTime (Due to the GO implementation) for startedAt, so we need to check for that here.
dbCheckRun.StartedAt = utils.NilIfZeroTime(dbCheckRun.StartedAt)
dbCheckRun.CompletedAt = utils.NilIfZeroTime(dbCheckRun.CompletedAt)
updatedAt := dbCheckRun.StartedAt
if dbCheckRun.CompletedAt != nil {
updatedAt = dbCheckRun.CompletedAt
}
if apiCollector.GetSince() != nil && !apiCollector.GetSince().Before(*updatedAt) {
return messages, helper.ErrFinishCollect
}
messages = append(messages, errors.Must1(json.Marshal(dbCheckRun)))
}
}
return
},
IgnoreQueryErrors: true,
PageSize: 20,
})
if err != nil {
return err
}
return apiCollector.Execute()
}