keon94 commented on code in PR #2401: URL: https://github.com/apache/incubator-devlake/pull/2401#discussion_r930611368
########## plugins/jira/tasks/external_epic_collector.go: ########## @@ -0,0 +1,152 @@ +/* +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 ( + "fmt" + "github.com/apache/incubator-devlake/plugins/core" + "github.com/apache/incubator-devlake/plugins/core/dal" + "strings" + + "github.com/apache/incubator-devlake/plugins/helper" + "net/http" + "net/url" +) + +import ( + "encoding/json" + "io/ioutil" +) + +const RAW_EXTERNAL_EPIC_TABLE = "jira_external_epics" + +// this struct should be moved to `jira_api_common.go` +type JiraEpicParams struct { + ConnectionId uint64 + BoardId uint64 +} + +var _ core.SubTaskEntryPoint = CollectIssues + +var CollectExternalEpicsMeta = core.SubTaskMeta{ + Name: "collectExternalEpics", + EntryPoint: CollectExternalEpics, + EnabledByDefault: true, + Description: "collect Jira epics from other boards", + DomainTypes: []string{core.DOMAIN_TYPE_TICKET, core.DOMAIN_TYPE_CROSS}, +} + +func CollectExternalEpics(taskCtx core.SubTaskContext) error { + db := taskCtx.GetDal() + data := taskCtx.GetData().(*JiraTaskData) + externalEpicKeys, err := GetExternalEpicKeys(db, data) + if err != nil { + return err + } + if len(externalEpicKeys) == 0 { + taskCtx.GetLogger().Info("no external epic keys found for Jira board %d", data.Options.BoardId) + return nil + } + since := data.Since + jql := "ORDER BY created ASC" + if since != nil { + // prepend a time range criteria if `since` was specified, either by user or from database + jql = fmt.Sprintf("updated >= '%s' %s", since.Format("2006/01/02 15:04"), jql) + } + jql = fmt.Sprintf("issue in (%s) %s", strings.Join(externalEpicKeys, ","), jql) + + collector, err := helper.NewApiCollector(helper.ApiCollectorArgs{ + RawDataSubTaskArgs: helper.RawDataSubTaskArgs{ + Ctx: taskCtx, + Params: JiraEpicParams{ + ConnectionId: data.Options.ConnectionId, + BoardId: data.Options.BoardId, + }, + Table: RAW_EXTERNAL_EPIC_TABLE, + }, + ApiClient: data.ApiClient, + PageSize: 100, + Incremental: false, + UrlTemplate: "api/2/search", + Query: func(reqData *helper.RequestData) (url.Values, error) { + query := url.Values{} + query.Set("jql", jql) + query.Set("issue in", fmt.Sprintf("(%s)", strings.Join(externalEpicKeys, ","))) + query.Set("startAt", fmt.Sprintf("%v", reqData.Pager.Skip)) + query.Set("maxResults", fmt.Sprintf("%v", reqData.Pager.Size)) + query.Set("expand", "changelog") + return query, nil + }, + GetTotalPages: GetTotalPagesFromResponse, + Concurrency: 10, + ResponseParser: func(res *http.Response) ([]json.RawMessage, error) { + var data struct { + Issues []json.RawMessage `json:"issues"` + } + blob, err := ioutil.ReadAll(res.Body) + if err != nil { + return nil, err + } + err = json.Unmarshal(blob, &data) + if err != nil { + return nil, err + } + return data.Issues, nil + }, + }) + if err != nil { + return err + } + return collector.Execute() +} + +func GetExternalEpicKeys(db dal.Dal, data *JiraTaskData) ([]string, error) { + // union of + // 1. issues with epics not from this board and not in the issues table + // 2. issues with epics not from this board that ARE already in the issues table (from previous runs) + // the above two selections are mutually exclusive + cursor, err := db.RawCursor(fmt.Sprintf(` Review Comment: done ########## plugins/jira/tasks/external_epic_extractor.go: ########## @@ -0,0 +1,136 @@ +/* +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" + "github.com/apache/incubator-devlake/plugins/core" + "github.com/apache/incubator-devlake/plugins/helper" + "github.com/apache/incubator-devlake/plugins/jira/models" + "github.com/apache/incubator-devlake/plugins/jira/tasks/apiv2models" + "strconv" + "strings" + "time" +) + +var _ core.SubTaskEntryPoint = ExtractIssues + +var ExtractExternalEpicsMeta = core.SubTaskMeta{ + Name: "extractExternalEpics", + EntryPoint: ExtractExternalEpics, + EnabledByDefault: true, + Description: "extract Jira epics from other boards", + DomainTypes: []string{core.DOMAIN_TYPE_TICKET, core.DOMAIN_TYPE_CROSS}, +} + +func ExtractExternalEpics(taskCtx core.SubTaskContext) error { + data := taskCtx.GetData().(*JiraTaskData) + db := taskCtx.GetDal() + connectionId := data.Options.ConnectionId + boardId := data.Options.BoardId + logger := taskCtx.GetLogger() + logger.Info("extract external epic Issues, connection_id=%d, board_id=%d", connectionId, boardId) + mappings, err := getTypeMappings(data, db) + if err != nil { + return err + } + extractor, err := helper.NewApiExtractor(helper.ApiExtractorArgs{ + RawDataSubTaskArgs: helper.RawDataSubTaskArgs{ + Ctx: taskCtx, + Params: JiraEpicParams{ + ConnectionId: data.Options.ConnectionId, + BoardId: data.Options.BoardId, + }, + Table: RAW_EXTERNAL_EPIC_TABLE, + }, + Extract: func(row *helper.RawData) ([]interface{}, error) { + var apiIssue apiv2models.Issue + err = json.Unmarshal(row.Data, &apiIssue) + if err != nil { + return nil, err + } + err = apiIssue.SetAllFields(row.Data) + if err != nil { + return nil, err + } + var results []interface{} + sprints, issue, worklogs, changelogs, changelogItems, users := apiIssue.ExtractEntities(data.Options.ConnectionId) + for _, sprintId := range sprints { Review Comment: done -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
