-
Notifications
You must be signed in to change notification settings - Fork 535
/
pr_collector.go
185 lines (170 loc) · 5.38 KB
/
pr_collector.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 (
"strings"
"time"
"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/github/tasks"
"github.com/merico-dev/graphql"
)
const RAW_PRS_TABLE = "github_graphql_prs"
type GraphqlQueryPrWrapper struct {
RateLimit struct {
Cost int
}
// now it orderBy UPDATED_AT and use cursor pagination
// It may miss some PRs updated when collection.
// Because these missed PRs will be collected on next, But it's not enough.
// So Next Millstone(0.17) we should change it to filter by CREATE_AT + collect detail
Repository struct {
PullRequests struct {
PageInfo *api.GraphqlQueryPageInfo
Prs []GraphqlQueryPr `graphql:"nodes"`
TotalCount graphql.Int
} `graphql:"pullRequests(first: $pageSize, after: $skipCursor, orderBy: {field: CREATED_AT, direction: DESC})"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
type GraphqlQueryPr struct {
DatabaseId int
Number int
State string
Title string
Body string
Url string
Labels struct {
Nodes []struct {
Id string
Name string
}
} `graphql:"labels(first: 100)"`
Author *GraphqlInlineAccountQuery
Assignees struct {
// FIXME now domain layer just support one assignee
Assignees []GraphqlInlineAccountQuery `graphql:"nodes"`
} `graphql:"assignees(first: 1)"`
ClosedAt *time.Time
MergedAt *time.Time
UpdatedAt time.Time
CreatedAt time.Time
MergeCommit *struct {
Oid string
}
HeadRefName string
HeadRefOid string
BaseRefName string
BaseRefOid string
Commits struct {
PageInfo *api.GraphqlQueryPageInfo
Nodes []GraphqlQueryCommit `graphql:"nodes"`
TotalCount graphql.Int
} `graphql:"commits(first: 100)"`
Reviews struct {
TotalCount graphql.Int
Nodes []GraphqlQueryReview `graphql:"nodes"`
} `graphql:"reviews(first: 100)"`
}
type GraphqlQueryReview struct {
Body string
Author *GraphqlInlineAccountQuery
State string `json:"state"`
DatabaseId int `json:"databaseId"`
Commit struct {
Oid string
}
SubmittedAt *time.Time `json:"submittedAt"`
}
type GraphqlQueryCommit struct {
Commit struct {
Oid string
Message string
Author struct {
Name string
Email string
Date time.Time
User *GraphqlInlineAccountQuery
}
Committer struct {
Date time.Time
Email string
Name string
}
}
Url string
}
var CollectPrsMeta = plugin.SubTaskMeta{
Name: "CollectPrs",
EntryPoint: CollectPrs,
EnabledByDefault: true,
Description: "Collect Pr data from GithubGraphql api, supports both timeFilter and diffSync.",
DomainTypes: []string{plugin.DOMAIN_TYPE_CODE_REVIEW},
}
var _ plugin.SubTaskEntryPoint = CollectPrs
func CollectPrs(taskCtx plugin.SubTaskContext) errors.Error {
data := taskCtx.GetData().(*tasks.GithubTaskData)
var err errors.Error
collectorWithState, err := api.NewStatefulApiCollector(api.RawDataSubTaskArgs{
Ctx: taskCtx,
Params: tasks.GithubApiParams{
ConnectionId: data.Options.ConnectionId,
Name: data.Options.Name,
},
Table: RAW_PRS_TABLE,
})
if err != nil {
return err
}
err = collectorWithState.InitGraphQLCollector(api.GraphqlCollectorArgs{
GraphqlClient: data.GraphqlClient,
PageSize: 10,
/*
(Optional) Return query string for request, or you can plug them into UrlTemplate directly
*/
BuildQuery: func(reqData *api.GraphqlRequestData) (interface{}, map[string]interface{}, error) {
query := &GraphqlQueryPrWrapper{}
if reqData == nil {
return query, map[string]interface{}{}, nil
}
ownerName := strings.Split(data.Options.Name, "/")
variables := map[string]interface{}{
"pageSize": graphql.Int(reqData.Pager.Size),
"skipCursor": (*graphql.String)(reqData.Pager.SkipCursor),
"owner": graphql.String(ownerName[0]),
"name": graphql.String(ownerName[1]),
}
return query, variables, nil
},
GetPageInfo: func(iQuery interface{}, args *api.GraphqlCollectorArgs) (*api.GraphqlQueryPageInfo, error) {
query := iQuery.(*GraphqlQueryPrWrapper)
return query.Repository.PullRequests.PageInfo, nil
},
ResponseParser: func(iQuery interface{}, variables map[string]interface{}) ([]interface{}, error) {
query := iQuery.(*GraphqlQueryPrWrapper)
prs := query.Repository.PullRequests.Prs
for _, rawL := range prs {
if collectorWithState.Since != nil && !collectorWithState.Since.Before(rawL.CreatedAt) {
return nil, api.ErrFinishCollect
}
}
return nil, nil
},
})
if err != nil {
return err
}
return collectorWithState.Execute()
}