| /* |
| 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" |
| "fmt" |
| "net/http" |
| "net/url" |
| "time" |
| |
| "github.com/apache/incubator-devlake/core/errors" |
| "github.com/apache/incubator-devlake/core/plugin" |
| helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| ) |
| |
| const RAW_ISSUE_TABLE = "gitlab_api_issues" |
| |
| var CollectApiIssuesMeta = plugin.SubTaskMeta{ |
| Name: "collectApiIssues", |
| EntryPoint: CollectApiIssues, |
| EnabledByDefault: true, |
| Description: "Collect issues data from Gitlab api, supports both timeFilter and diffSync.", |
| DomainTypes: []string{plugin.DOMAIN_TYPE_TICKET}, |
| } |
| |
| func CollectApiIssues(taskCtx plugin.SubTaskContext) errors.Error { |
| rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, RAW_ISSUE_TABLE) |
| collectorWithState, err := helper.NewStatefulApiCollector(*rawDataSubTaskArgs, data.TimeAfter) |
| if err != nil { |
| return err |
| } |
| |
| incremental := collectorWithState.IsIncremental() |
| err = collectorWithState.InitCollector(helper.ApiCollectorArgs{ |
| ApiClient: data.ApiClient, |
| PageSize: 100, |
| Incremental: incremental, |
| |
| UrlTemplate: "projects/{{ .Params.ProjectId }}/issues", |
| /* |
| (Optional) Return query string for request, or you can plug them into UrlTemplate directly |
| */ |
| Query: func(reqData *helper.RequestData) (url.Values, errors.Error) { |
| query := url.Values{} |
| if collectorWithState.TimeAfter != nil { |
| query.Set("updated_after", collectorWithState.TimeAfter.Format(time.RFC3339)) |
| } |
| if incremental { |
| query.Set("updated_after", collectorWithState.LatestState.LatestSuccessStart.Format(time.RFC3339)) |
| } |
| query.Set("sort", "asc") |
| query.Set("page", fmt.Sprintf("%v", reqData.Pager.Page)) |
| query.Set("per_page", fmt.Sprintf("%v", reqData.Pager.Size)) |
| |
| return query, nil |
| }, |
| |
| GetTotalPages: GetTotalPagesFromResponse, |
| ResponseParser: func(res *http.Response) ([]json.RawMessage, errors.Error) { |
| var items []json.RawMessage |
| err := helper.UnmarshalResponse(res, &items) |
| if err != nil { |
| return nil, err |
| } |
| return items, nil |
| }, |
| }) |
| |
| if err != nil { |
| return err |
| } |
| |
| return collectorWithState.Execute() |
| } |