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/07/19 07:50:58 UTC

[incubator-devlake] 04/06: fix: jira verify enckey and jira encode password

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 a5b3e1a09e233b4cc9c324fb993e96e5b2765fc8
Author: abeizn <zi...@merico.dev>
AuthorDate: Tue Jul 19 14:41:39 2022 +0800

    fix: jira verify enckey and jira encode password
---
 generator/cmd/init_migration.go                    |  2 +-
 .../archived/connection_history.go                 | 56 ----------------
 .../models/migrationscripts/archived/source.go     | 55 ++++++++++++++++
 .../jira/models/migrationscripts/init_schema.go    | 74 +++++++---------------
 plugins/jira/models/migrationscripts/register.go   |  1 +
 .../migrationscripts/updataSchemas20220505.go      | 65 +++++++++++++++++++
 6 files changed, 145 insertions(+), 108 deletions(-)

diff --git a/generator/cmd/init_migration.go b/generator/cmd/init_migration.go
index 58e2a462..ebc4f17a 100644
--- a/generator/cmd/init_migration.go
+++ b/generator/cmd/init_migration.go
@@ -63,7 +63,7 @@ Type in which plugin do you want init migrations in, then generator will create
 
 		// read template
 		templates := map[string]string{
-			`initSchema.go`:     util.ReadTemplate("generator/template/migrationscripts/init_domain_schemas.go-template"),
+			`initSchema.go`:     util.ReadTemplate("generator/template/migrationscripts/init_schemas.go-template"),
 			`register.go`:       util.ReadTemplate("generator/template/migrationscripts/register.go-template"),
 			`archived/.gitkeep`: ``,
 		}
diff --git a/plugins/jira/models/migrationscripts/archived/connection_history.go b/plugins/jira/models/migrationscripts/archived/connection_history.go
deleted file mode 100644
index fae6f10f..00000000
--- a/plugins/jira/models/migrationscripts/archived/connection_history.go
+++ /dev/null
@@ -1,56 +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 archived
-
-import "time"
-
-type JiraConnectionV11 struct {
-	ID                         uint64    `gorm:"primaryKey" json:"id"`
-	CreatedAt                  time.Time `json:"createdAt"`
-	UpdatedAt                  time.Time `json:"updatedAt"`
-	Name                       string    `gorm:"type:varchar(100);uniqueIndex" json:"name" validate:"required"`
-	Endpoint                   string    `json:"endpoint" validate:"required"`
-	BasicAuthEncoded           string    `json:"basicAuthEncoded" validate:"required"`
-	EpicKeyField               string    `gorm:"type:varchar(50);" json:"epicKeyField"`
-	StoryPointField            string    `gorm:"type:varchar(50);" json:"storyPointField"`
-	RemotelinkCommitShaPattern string    `gorm:"type:varchar(255);comment='golang regexp, the first group will be recognized as commit sha, ref https://github.com/google/re2/wiki/Syntax'" json:"remotelinkCommitShaPattern"`
-	Proxy                      string    `json:"proxy"`
-	RateLimitPerHour           int       `comment:"api request rate limt per hour" json:"rateLimit"`
-}
-
-func (JiraConnectionV11) TableName() string {
-	return "_tool_jira_connections"
-}
-
-type JiraConnectionV10 struct {
-	ID                         uint64    `gorm:"primaryKey" json:"id"`
-	CreatedAt                  time.Time `json:"createdAt"`
-	UpdatedAt                  time.Time `json:"updatedAt"`
-	Name                       string    `gorm:"type:varchar(100);uniqueIndex" json:"name" validate:"required"`
-	Endpoint                   string    `json:"endpoint" validate:"required"`
-	BasicAuthEncoded           string    `json:"basicAuthEncoded" validate:"required"`
-	EpicKeyField               string    `gorm:"type:varchar(50);" json:"epicKeyField"`
-	StoryPointField            string    `gorm:"type:varchar(50);" json:"storyPointField"`
-	RemotelinkCommitShaPattern string    `gorm:"type:varchar(255);comment='golang regexp, the first group will be recognized as commit sha, ref https://github.com/google/re2/wiki/Syntax'" json:"remotelinkCommitShaPattern"`
-	Proxy                      string    `json:"proxy"`
-	RateLimitPerHour           int       `comment:"api request rate limt per hour" json:"rateLimit"`
-}
-
-func (JiraConnectionV10) TableName() string {
-	return "_tool_jira_source"
-}
diff --git a/plugins/jira/models/migrationscripts/archived/source.go b/plugins/jira/models/migrationscripts/archived/source.go
new file mode 100644
index 00000000..39244699
--- /dev/null
+++ b/plugins/jira/models/migrationscripts/archived/source.go
@@ -0,0 +1,55 @@
+/*
+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 archived
+
+import "github.com/apache/incubator-devlake/models/migrationscripts/archived"
+
+type JiraSource struct {
+	archived.Model
+	Name                       string `gorm:"type:varchar(100);uniqueIndex" json:"name" validate:"required"`
+	Endpoint                   string `json:"endpoint" validate:"required"`
+	BasicAuthEncoded           string `json:"basicAuthEncoded" validate:"required"`
+	EpicKeyField               string `gorm:"type:varchar(50);" json:"epicKeyField"`
+	StoryPointField            string `gorm:"type:varchar(50);" json:"storyPointField"`
+	RemotelinkCommitShaPattern string `gorm:"type:varchar(255);comment='golang regexp, the first group will be recognized as commit sha, ref https://github.com/google/re2/wiki/Syntax'" json:"remotelinkCommitShaPattern"`
+	Proxy                      string `json:"proxy"`
+	RateLimit                  int    `comment:"api request rate limt per second"`
+}
+
+type JiraIssueTypeMapping struct {
+	SourceID     uint64 `gorm:"primaryKey" json:"jiraSourceId" validate:"required"`
+	UserType     string `gorm:"type:varchar(50);primaryKey" json:"userType" validate:"required"`
+	StandardType string `gorm:"type:varchar(50)" json:"standardType" validate:"required"`
+}
+
+type JiraIssueStatusMapping struct {
+	SourceID       uint64 `gorm:"primaryKey" json:"jiraSourceId" validate:"required"`
+	UserType       string `gorm:"type:varchar(50);primaryKey" json:"userType" validate:"required"`
+	UserStatus     string `gorm:"type:varchar(50);primaryKey" json:"userStatus" validate:"required"`
+	StandardStatus string `gorm:"type:varchar(50)" json:"standardStatus" validate:"required"`
+}
+
+func (JiraSource) TableName() string {
+	return "_tool_jira_sources"
+}
+
+func (JiraIssueTypeMapping) TableName() string {
+	return "_tool_jira_issue_type_mappings"
+}
+
+func (JiraIssueStatusMapping) TableName() string {
+	return "_tool_jira_issue_status_mappings"
+}
diff --git a/plugins/jira/models/migrationscripts/init_schema.go b/plugins/jira/models/migrationscripts/init_schema.go
index b4d54e52..7c60c3b6 100644
--- a/plugins/jira/models/migrationscripts/init_schema.go
+++ b/plugins/jira/models/migrationscripts/init_schema.go
@@ -22,6 +22,7 @@ import (
 	"encoding/base64"
 	"fmt"
 	"strings"
+	"time"
 
 	"github.com/apache/incubator-devlake/config"
 	"github.com/apache/incubator-devlake/plugins/core"
@@ -29,6 +30,24 @@ import (
 	"gorm.io/gorm"
 )
 
+type JiraConnectionV11 struct {
+	ID                         uint64    `gorm:"primaryKey" json:"id"`
+	CreatedAt                  time.Time `json:"createdAt"`
+	UpdatedAt                  time.Time `json:"updatedAt"`
+	Name                       string    `gorm:"type:varchar(100);uniqueIndex" json:"name" validate:"required"`
+	Endpoint                   string    `json:"endpoint" validate:"required"`
+	BasicAuthEncoded           string    `json:"basicAuthEncoded" validate:"required"`
+	EpicKeyField               string    `gorm:"type:varchar(50);" json:"epicKeyField"`
+	StoryPointField            string    `gorm:"type:varchar(50);" json:"storyPointField"`
+	RemotelinkCommitShaPattern string    `gorm:"type:varchar(255);comment='golang regexp, the first group will be recognized as commit sha, ref https://github.com/google/re2/wiki/Syntax'" json:"remotelinkCommitShaPattern"`
+	Proxy                      string    `json:"proxy"`
+	RateLimit                  int       `comment:"api request rate limt per hour" json:"rateLimit"`
+}
+
+func (JiraConnectionV11) TableName() string {
+	return "_tool_jira_connections"
+}
+
 type InitSchemas struct{}
 
 func (*InitSchemas) Up(ctx context.Context, db *gorm.DB) error {
@@ -67,12 +86,12 @@ func (*InitSchemas) Up(ctx context.Context, db *gorm.DB) error {
 	var result *gorm.DB
 	m := db.Migrator()
 
-	if m.HasTable(&archived.JiraConnectionV11{}) {
-		var jiraConns []archived.JiraConnectionV11
+	if m.HasTable(&JiraConnectionV11{}) {
+		var jiraConns []JiraConnectionV11
 		result = db.Find(&jiraConns)
 
 		if result.Error == nil {
-			err := db.Migrator().DropTable(&archived.JiraConnectionV11{})
+			err := db.Migrator().DropTable(&JiraConnectionV11{})
 			if err != nil {
 				return err
 			}
@@ -87,7 +106,7 @@ func (*InitSchemas) Up(ctx context.Context, db *gorm.DB) error {
 				conn.Name = v.Name
 				conn.Endpoint = v.Endpoint
 				conn.Proxy = v.Proxy
-				conn.RateLimitPerHour = v.RateLimitPerHour
+				conn.RateLimitPerHour = v.RateLimit
 
 				c := config.GetConfig()
 				encKey := c.GetString("ENCODE_KEY")
@@ -114,53 +133,6 @@ func (*InitSchemas) Up(ctx context.Context, db *gorm.DB) error {
 				}
 			}
 		}
-	} else if m.HasTable(&archived.JiraConnectionV10{}) {
-		var jiraConns []archived.JiraConnectionV10
-		result = db.Find(&jiraConns)
-
-		if result.Error == nil {
-			err := db.Migrator().DropTable(&archived.JiraConnectionV10{})
-			if err != nil {
-				return err
-			}
-			err = db.Migrator().AutoMigrate(&archived.JiraConnection{})
-			if err != nil {
-				return err
-			}
-
-			for _, v := range jiraConns {
-				conn := &archived.JiraConnection{}
-				conn.ID = v.ID
-				conn.Name = v.Name
-				conn.Endpoint = v.Endpoint
-				conn.Proxy = v.Proxy
-				conn.RateLimitPerHour = v.RateLimitPerHour
-
-				c := config.GetConfig()
-				encKey := c.GetString("ENCODE_KEY")
-				if encKey == "" {
-					return fmt.Errorf("jira v0.10 invalid encKey")
-				}
-				auth, err := core.Decrypt(encKey, v.BasicAuthEncoded)
-				if err != nil {
-					return err
-				}
-				pk, err := base64.StdEncoding.DecodeString(auth)
-				if err != nil {
-					return err
-				}
-				originInfo := strings.Split(string(pk), ":")
-				if len(originInfo) == 2 {
-					conn.Username = originInfo[0]
-					conn.Password, err = core.Encrypt(encKey, originInfo[1])
-					if err != nil {
-						return err
-					}
-					// create
-					db.Create(&conn)
-				}
-			}
-		}
 	} else {
 		c := config.GetConfig()
 		encKey := c.GetString("ENCODE_KEY")
diff --git a/plugins/jira/models/migrationscripts/register.go b/plugins/jira/models/migrationscripts/register.go
index 06d924d0..e1634eae 100644
--- a/plugins/jira/models/migrationscripts/register.go
+++ b/plugins/jira/models/migrationscripts/register.go
@@ -24,6 +24,7 @@ import (
 // All return all the migration scripts
 func All() []migration.Script {
 	return []migration.Script{
+		new(UpdateSchemas20220505),
 		new(InitSchemas),
 	}
 }
diff --git a/plugins/jira/models/migrationscripts/updataSchemas20220505.go b/plugins/jira/models/migrationscripts/updataSchemas20220505.go
new file mode 100644
index 00000000..19f43991
--- /dev/null
+++ b/plugins/jira/models/migrationscripts/updataSchemas20220505.go
@@ -0,0 +1,65 @@
+/*
+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 migrationscripts
+
+import (
+	"context"
+
+	"github.com/apache/incubator-devlake/models/common"
+	"github.com/apache/incubator-devlake/plugins/jira/models/migrationscripts/archived"
+	"gorm.io/gorm"
+)
+
+type JiraConnection20220505 struct {
+	common.Model
+	Name                       string `gorm:"type:varchar(100);uniqueIndex" json:"name" validate:"required"`
+	Endpoint                   string `json:"endpoint" validate:"required"`
+	BasicAuthEncoded           string `json:"basicAuthEncoded" validate:"required"`
+	EpicKeyField               string `gorm:"type:varchar(50);" json:"epicKeyField"`
+	StoryPointField            string `gorm:"type:varchar(50);" json:"storyPointField"`
+	RemotelinkCommitShaPattern string `gorm:"type:varchar(255);comment='golang regexp, the first group will be recognized as commit sha, ref https://github.com/google/re2/wiki/Syntax'" json:"remotelinkCommitShaPattern"`
+	Proxy                      string `json:"proxy"`
+	RateLimit                  int    `comment:"api request rate limt per hour" json:"rateLimit"`
+}
+
+func (JiraConnection20220505) TableName() string {
+	return "_tool_jira_connections"
+}
+
+type UpdateSchemas20220505 struct{}
+
+func (*UpdateSchemas20220505) Up(ctx context.Context, db *gorm.DB) error {
+	m := db.Migrator()
+	if m.HasTable(&archived.JiraSource{}) && !m.HasTable(&archived.JiraConnection{}) {
+		err := db.Migrator().RenameTable(archived.JiraSource{}, JiraConnection20220505{})
+		if err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func (*UpdateSchemas20220505) Version() uint64 {
+	return 20220505212344
+}
+
+func (*UpdateSchemas20220505) Owner() string {
+	return "Jira"
+}
+
+func (*UpdateSchemas20220505) Name() string {
+	return "Rename source to connection "
+}