in backend/plugins/github_graphql/tasks/pr_collector.go [132:316]
func CollectPr(taskCtx plugin.SubTaskContext) errors.Error {
data := taskCtx.GetData().(*tasks.GithubTaskData)
config := data.Options.ScopeConfig
var labelTypeRegex *regexp.Regexp
var labelComponentRegex *regexp.Regexp
var err errors.Error
if config != nil && len(config.PrType) > 0 {
labelTypeRegex, err = errors.Convert01(regexp.Compile(config.PrType))
if err != nil {
return errors.Default.Wrap(err, "regexp Compile prType failed")
}
}
if config != nil && len(config.PrComponent) > 0 {
labelComponentRegex, err = errors.Convert01(regexp.Compile(config.PrComponent))
if err != nil {
return errors.Default.Wrap(err, "regexp Compile prComponent failed")
}
}
collectorWithState, err := api.NewStatefulApiCollector(api.RawDataSubTaskArgs{
Ctx: taskCtx,
Params: tasks.GithubApiParams{
ConnectionId: data.Options.ConnectionId,
Name: data.Options.Name,
},
Table: RAW_PRS_TABLE,
}, data.TimeAfter)
if err != nil {
return err
}
incremental := collectorWithState.IsIncremental()
err = collectorWithState.InitGraphQLCollector(api.GraphqlCollectorArgs{
GraphqlClient: data.GraphqlClient,
PageSize: 10,
Incremental: incremental,
/*
(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
results := make([]interface{}, 0, 1)
isFinish := false
for _, rawL := range prs {
// collect data even though in increment mode because of updating existing data
if collectorWithState.TimeAfter != nil && !collectorWithState.TimeAfter.Before(rawL.UpdatedAt) {
isFinish = true
break
}
githubPr, err := convertGithubPullRequest(rawL, data.Options.ConnectionId, data.Options.GithubId)
if err != nil {
return nil, err
}
if rawL.Author != nil {
githubUser, err := convertGraphqlPreAccount(*rawL.Author, data.Options.GithubId, data.Options.ConnectionId)
if err != nil {
return nil, err
}
results = append(results, githubUser)
}
for _, label := range rawL.Labels.Nodes {
results = append(results, &models.GithubPrLabel{
ConnectionId: data.Options.ConnectionId,
PullId: githubPr.GithubId,
LabelName: label.Name,
})
// if pr.Type has not been set and prType is set in .env, process the below
if labelTypeRegex != nil {
groups := labelTypeRegex.FindStringSubmatch(label.Name)
if len(groups) > 0 {
githubPr.Type = groups[1]
}
}
// if pr.Component has not been set and prComponent is set in .env, process
if labelComponentRegex != nil {
groups := labelComponentRegex.FindStringSubmatch(label.Name)
if len(groups) > 0 {
githubPr.Component = groups[1]
}
}
}
results = append(results, githubPr)
for _, apiPullRequestReview := range rawL.Reviews.Nodes {
if apiPullRequestReview.State != "PENDING" {
githubReviewer := &models.GithubReviewer{
ConnectionId: data.Options.ConnectionId,
PullRequestId: githubPr.GithubId,
}
githubPrReview := &models.GithubPrReview{
ConnectionId: data.Options.ConnectionId,
GithubId: apiPullRequestReview.DatabaseId,
Body: apiPullRequestReview.Body,
State: apiPullRequestReview.State,
CommitSha: apiPullRequestReview.Commit.Oid,
GithubSubmitAt: apiPullRequestReview.SubmittedAt,
PullRequestId: githubPr.GithubId,
}
if apiPullRequestReview.Author != nil {
githubReviewer.GithubId = apiPullRequestReview.Author.Id
githubReviewer.Login = apiPullRequestReview.Author.Login
githubPrReview.AuthorUserId = apiPullRequestReview.Author.Id
githubPrReview.AuthorUsername = apiPullRequestReview.Author.Login
githubUser, err := convertGraphqlPreAccount(*apiPullRequestReview.Author, data.Options.GithubId, data.Options.ConnectionId)
if err != nil {
return nil, err
}
results = append(results, githubUser)
}
results = append(results, githubReviewer)
results = append(results, githubPrReview)
}
}
for _, apiPullRequestCommit := range rawL.Commits.Nodes {
githubCommit, err := convertPullRequestCommit(apiPullRequestCommit)
if err != nil {
return nil, err
}
results = append(results, githubCommit)
githubPullRequestCommit := &models.GithubPrCommit{
ConnectionId: data.Options.ConnectionId,
CommitSha: apiPullRequestCommit.Commit.Oid,
PullRequestId: githubPr.GithubId,
CommitAuthorName: githubCommit.AuthorName,
CommitAuthorEmail: githubCommit.AuthorEmail,
CommitAuthoredDate: githubCommit.AuthoredDate,
}
if err != nil {
return nil, err
}
results = append(results, githubPullRequestCommit)
if apiPullRequestCommit.Commit.Author.User != nil {
githubUser, err := convertGraphqlPreAccount(*apiPullRequestCommit.Commit.Author.User, data.Options.GithubId, data.Options.ConnectionId)
if err != nil {
return nil, err
}
results = append(results, githubUser)
}
}
}
if isFinish {
return results, api.ErrFinishCollect
} else {
return results, nil
}
},
})
if err != nil {
return err
}
return collectorWithState.Execute()
}