| /* |
| 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" |
| helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| githubTasks "github.com/apache/incubator-devlake/plugins/github/tasks" |
| "github.com/merico-dev/graphql" |
| ) |
| |
| const RAW_ISSUES_TABLE = "github_graphql_issues" |
| |
| type GraphqlQueryIssueWrapper struct { |
| RateLimit struct { |
| Cost int |
| } |
| Repository struct { |
| IssueList struct { |
| TotalCount graphql.Int |
| Issues []GraphqlQueryIssue `graphql:"nodes"` |
| PageInfo *helper.GraphqlQueryPageInfo |
| } `graphql:"issues(first: $pageSize, after: $skipCursor, orderBy: {field: UPDATED_AT, direction: DESC})"` |
| } `graphql:"repository(owner: $owner, name: $name)"` |
| } |
| |
| type GraphqlQueryIssue struct { |
| DatabaseId int |
| Number int |
| State string |
| StateReason string |
| Title string |
| Body string |
| Author *GraphqlInlineAccountQuery |
| Url string |
| ClosedAt *time.Time |
| CreatedAt time.Time |
| UpdatedAt time.Time |
| AssigneeList struct { |
| // FIXME now domain layer just support one assignee |
| Assignees []GraphqlInlineAccountQuery `graphql:"nodes"` |
| } `graphql:"assignees(first: 100)"` |
| Milestone *struct { |
| Number int |
| } `json:"milestone"` |
| Labels struct { |
| Nodes []struct { |
| Id string |
| Name string |
| } |
| } `graphql:"labels(first: 100)"` |
| } |
| |
| var CollectIssuesMeta = plugin.SubTaskMeta{ |
| Name: "CollectIssues", |
| EntryPoint: CollectIssues, |
| EnabledByDefault: true, |
| Description: "Collect Issue data from GithubGraphql api, supports both timeFilter and diffSync.", |
| DomainTypes: []string{plugin.DOMAIN_TYPE_TICKET}, |
| } |
| |
| var _ plugin.SubTaskEntryPoint = CollectIssues |
| |
| func CollectIssues(taskCtx plugin.SubTaskContext) errors.Error { |
| data := taskCtx.GetData().(*githubTasks.GithubTaskData) |
| collectorWithState, err := helper.NewStatefulApiCollector(helper.RawDataSubTaskArgs{ |
| Ctx: taskCtx, |
| Params: githubTasks.GithubApiParams{ |
| ConnectionId: data.Options.ConnectionId, |
| Name: data.Options.Name, |
| }, |
| Table: RAW_ISSUES_TABLE, |
| }) |
| if err != nil { |
| return err |
| } |
| |
| err = collectorWithState.InitGraphQLCollector(helper.GraphqlCollectorArgs{ |
| GraphqlClient: data.GraphqlClient, |
| PageSize: 10, |
| BuildQuery: func(reqData *helper.GraphqlRequestData) (interface{}, map[string]interface{}, error) { |
| query := &GraphqlQueryIssueWrapper{} |
| 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 *helper.GraphqlCollectorArgs) (*helper.GraphqlQueryPageInfo, error) { |
| query := iQuery.(*GraphqlQueryIssueWrapper) |
| return query.Repository.IssueList.PageInfo, nil |
| }, |
| ResponseParser: func(iQuery interface{}, variables map[string]interface{}) ([]interface{}, error) { |
| query := iQuery.(*GraphqlQueryIssueWrapper) |
| issues := query.Repository.IssueList.Issues |
| for _, rawL := range issues { |
| if collectorWithState.Since != nil && !collectorWithState.Since.Before(rawL.UpdatedAt) { |
| return nil, helper.ErrFinishCollect |
| } |
| } |
| return nil, nil |
| }, |
| }) |
| if err != nil { |
| return err |
| } |
| |
| return collectorWithState.Execute() |
| } |