You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by GitBox <gi...@apache.org> on 2022/07/22 06:48:54 UTC

[GitHub] [incubator-devlake] klesh commented on a diff in pull request #2401: [feat-1681][jira]: Collect the epic issue in an issue's epic link

klesh commented on code in PR #2401:
URL: https://github.com/apache/incubator-devlake/pull/2401#discussion_r927252440


##########
api/docs/docs.go:
##########
@@ -0,0 +1,1233 @@
+// Package docs GENERATED BY SWAG; DO NOT EDIT

Review Comment:
   Why is this file gets committed? it should be removed and ignored.



##########
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 {
+				sprintIssue := &models.JiraSprintIssue{
+					ConnectionId:     data.Options.ConnectionId,
+					SprintId:         sprintId,
+					IssueId:          issue.IssueId,
+					IssueCreatedDate: &issue.Created,
+					ResolutionDate:   issue.ResolutionDate,
+				}
+				results = append(results, sprintIssue)
+			}
+			if issue.ResolutionDate != nil {
+				issue.LeadTimeMinutes = uint(issue.ResolutionDate.Unix()-issue.Created.Unix()) / 60
+			}
+			if data.Options.TransformationRules.StoryPointField != "" {
+				strStoryPoint, _ := apiIssue.Fields.AllFields[data.Options.TransformationRules.StoryPointField].(string)
+				if strStoryPoint != "" {
+					issue.StoryPoint, _ = strconv.ParseFloat(strStoryPoint, 32)
+				}
+			}
+			issue.Type = mappings.typeIdMappings[issue.Type]
+			issue.StdStoryPoint = int64(issue.StoryPoint)
+			issue.StdType = mappings.stdTypeMappings[issue.Type]

Review Comment:
   I think it should be hardcoded as "EPIC"



##########
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(`
+			SELECT tji.epic_key as epicKey FROM _tool_jira_issues tji

Review Comment:
   1. the `connection_id` is missing
   2. second, why union? shouldn't it be 
   ```sql
   select
   	distinct epic_key
   from
   	_tool_jira_issues i
   left join `_tool_jira_board_issues` bi on
   	(i.connection_id = bi.connection_id
   		and i.issue_id = bi.issue_id)
   where
   	i.epic_key != ''
   	and bi.board_id = 8
   ```



##########
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:
   Can we share these code with jira_issue_collector?



-- 
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: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org