You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by wa...@apache.org on 2022/06/02 09:21:29 UTC

[incubator-devlake] 01/02: fix: fix changelogs.from_value and changelogs.to_values to comply naming convention

This is an automated email from the ASF dual-hosted git repository.

warren pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit 56ae0cb9ae69f9d95ee2ec15fd236932bc1ac811
Author: zhangliang <li...@merico.dev>
AuthorDate: Thu Jun 2 16:03:09 2022 +0800

    fix: fix changelogs.from_value and changelogs.to_values to comply naming convention
---
 models/domainlayer/ticket/issue_history.go         | 60 ----------------------
 models/migrationscripts/register.go                |  1 +
 .../{register.go => updateSchemas20220602.go}      | 30 +++++++----
 plugins/jira/tasks/changelog_convertor.go          | 58 ++++++++++++++-------
 4 files changed, 59 insertions(+), 90 deletions(-)

diff --git a/models/domainlayer/ticket/issue_history.go b/models/domainlayer/ticket/issue_history.go
deleted file mode 100644
index 056762ec..00000000
--- a/models/domainlayer/ticket/issue_history.go
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
-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 ticket
-
-import (
-	"time"
-
-	"github.com/apache/incubator-devlake/models/common"
-)
-
-type IssueStatusHistory struct {
-	common.NoPKModel
-	IssueId        string    `gorm:"primaryKey;type:varchar(255)"`
-	OriginalStatus string    `gorm:"primaryKey;type:varchar(255)"`
-	StartDate      time.Time `gorm:"primaryKey"`
-	EndDate        *time.Time
-}
-
-func (IssueStatusHistory) TableName() string {
-	return "issue_status_history"
-}
-
-type IssueAssigneeHistory struct {
-	common.NoPKModel
-	IssueId   string    `gorm:"primaryKey;type:varchar(255)"`
-	Assignee  string    `gorm:"primaryKey;type:varchar(255)"`
-	StartDate time.Time `gorm:"primaryKey"`
-	EndDate   *time.Time
-}
-
-func (IssueAssigneeHistory) TableName() string {
-	return "issue_assignee_history"
-}
-
-type IssueSprintsHistory struct {
-	common.NoPKModel
-	IssueId   string    `gorm:"primaryKey;type:varchar(255)"`
-	SprintId  string    `gorm:"primaryKey;type:varchar(255)"`
-	StartDate time.Time `gorm:"primaryKey"`
-	EndDate   *time.Time
-}
-
-func (IssueSprintsHistory) TableName() string {
-	return "issue_sprints_history"
-}
diff --git a/models/migrationscripts/register.go b/models/migrationscripts/register.go
index d7ee8e38..e014cc4e 100644
--- a/models/migrationscripts/register.go
+++ b/models/migrationscripts/register.go
@@ -27,5 +27,6 @@ func RegisterAll() {
 		new(updateSchemas20220513), new(updateSchemas20220524), new(updateSchemas20220526),
 		new(updateSchemas20220527),
 		new(updateSchemas20220528),
+		new(updateSchemas20220602),
 	}, "Framework")
 }
diff --git a/models/migrationscripts/register.go b/models/migrationscripts/updateSchemas20220602.go
similarity index 58%
copy from models/migrationscripts/register.go
copy to models/migrationscripts/updateSchemas20220602.go
index d7ee8e38..fed7eca3 100644
--- a/models/migrationscripts/register.go
+++ b/models/migrationscripts/updateSchemas20220602.go
@@ -17,15 +17,23 @@ limitations under the License.
 
 package migrationscripts
 
-import "github.com/apache/incubator-devlake/migration"
-
-// RegisterAll register all the migration scripts of framework
-func RegisterAll() {
-	migration.Register([]migration.Script{
-		new(initSchemas),
-		new(updateSchemas20220505), new(updateSchemas20220507), new(updateSchemas20220510),
-		new(updateSchemas20220513), new(updateSchemas20220524), new(updateSchemas20220526),
-		new(updateSchemas20220527),
-		new(updateSchemas20220528),
-	}, "Framework")
+import (
+	"context"
+
+	"github.com/apache/incubator-devlake/models/migrationscripts/archived"
+	"gorm.io/gorm"
+)
+
+type updateSchemas20220602 struct{}
+
+func (*updateSchemas20220602) Up(ctx context.Context, db *gorm.DB) error {
+	return db.Migrator().DropTable(&archived.IssueAssigneeHistory{}, &archived.IssueStatusHistory{}, &archived.IssueSprintsHistory{})
+}
+
+func (*updateSchemas20220602) Version() uint64 {
+	return 20220602154333
+}
+
+func (*updateSchemas20220602) Name() string {
+	return "drop tables: issue_assignee_history, issue_status_history, issue_sprints_history"
 }
diff --git a/plugins/jira/tasks/changelog_convertor.go b/plugins/jira/tasks/changelog_convertor.go
index 19693146..54f6ea8e 100644
--- a/plugins/jira/tasks/changelog_convertor.go
+++ b/plugins/jira/tasks/changelog_convertor.go
@@ -1,24 +1,9 @@
-/*
-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 (
 	"reflect"
+	"strconv"
+	"strings"
 	"time"
 
 	"github.com/apache/incubator-devlake/models/domainlayer"
@@ -63,8 +48,9 @@ func ConvertChangelogs(taskCtx core.SubTaskContext) error {
 	}
 	defer cursor.Close()
 	issueIdGenerator := didgen.NewDomainIdGenerator(&models.JiraIssue{})
+	sprintIdGenerator := didgen.NewDomainIdGenerator(&models.JiraSprint{})
 	changelogIdGenerator := didgen.NewDomainIdGenerator(&models.JiraChangelogItem{})
-
+	userIdGen := didgen.NewDomainIdGenerator(&models.JiraUser{})
 	converter, err := helper.NewDataConverter(helper.DataConverterArgs{
 		RawDataSubTaskArgs: helper.RawDataSubTaskArgs{
 			Ctx: taskCtx,
@@ -85,7 +71,7 @@ func ConvertChangelogs(taskCtx core.SubTaskContext) error {
 					row.Field,
 				)},
 				IssueId:     issueIdGenerator.Generate(row.ConnectionId, row.IssueId),
-				AuthorId:    row.AuthorAccountId,
+				AuthorId:    userIdGen.Generate(connectionId, row.AuthorAccountId),
 				AuthorName:  row.AuthorDisplayName,
 				FieldId:     row.FieldId,
 				FieldName:   row.Field,
@@ -93,6 +79,24 @@ func ConvertChangelogs(taskCtx core.SubTaskContext) error {
 				ToValue:     row.ToString,
 				CreatedDate: row.Created,
 			}
+			if row.Field == "assignee" {
+				if row.ToValue != "" {
+					changelog.ToValue = userIdGen.Generate(connectionId, row.ToValue)
+				}
+				if row.FromValue != "" {
+					changelog.FromValue = userIdGen.Generate(connectionId, row.FromValue)
+				}
+			}
+			if row.Field == "Sprint" {
+				changelog.FromValue, err = convertIds(row.FromValue, connectionId, sprintIdGenerator)
+				if err != nil {
+					return nil, err
+				}
+				changelog.ToValue, err = convertIds(row.ToValue, connectionId, sprintIdGenerator)
+				if err != nil {
+					return nil, err
+				}
+			}
 			return []interface{}{changelog}, nil
 		},
 	})
@@ -103,3 +107,19 @@ func ConvertChangelogs(taskCtx core.SubTaskContext) error {
 
 	return converter.Execute()
 }
+
+func convertIds(ids string, connectionId uint64, sprintIdGenerator *didgen.DomainIdGenerator) (string, error) {
+	ss := strings.Split(ids, ",")
+	var resultSlice []string
+	for _, item := range ss {
+		item = strings.TrimSpace(item)
+		if item != "" {
+			id, err := strconv.ParseUint(item, 10, 64)
+			if err != nil {
+				return "", err
+			}
+			resultSlice = append(resultSlice, sprintIdGenerator.Generate(connectionId, id))
+		}
+	}
+	return strings.Join(resultSlice, ","), nil
+}