You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ab...@apache.org on 2024/01/16 07:27:26 UTC

(incubator-devlake) branch fix#6718 updated: fix: gitextractor fails when branch name is too long

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

abeizn pushed a commit to branch fix#6718
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/fix#6718 by this push:
     new 630102f83 fix: gitextractor fails when branch name is too long
630102f83 is described below

commit 630102f83be9b4544ea6d7e819796d282e2cb230
Author: abeizn <zi...@merico.dev>
AuthorDate: Tue Jan 16 15:27:13 2024 +0800

    fix: gitextractor fails when branch name is too long
---
 backend/core/models/domainlayer/code/ref.go        |   5 +-
 backend/core/models/domainlayer/domainlayer.go     |   5 +
 .../20240116_modify_refs_id_length.go              | 109 +++++++++++++++++++++
 .../core/models/migrationscripts/archived/base.go  |   5 +
 backend/core/models/migrationscripts/register.go   |   1 +
 backend/plugins/gitextractor/parser/repo.go        |  10 +-
 6 files changed, 128 insertions(+), 7 deletions(-)

diff --git a/backend/core/models/domainlayer/code/ref.go b/backend/core/models/domainlayer/code/ref.go
index c1d717d0f..c487f7eec 100644
--- a/backend/core/models/domainlayer/code/ref.go
+++ b/backend/core/models/domainlayer/code/ref.go
@@ -18,12 +18,13 @@ limitations under the License.
 package code
 
 import (
-	"github.com/apache/incubator-devlake/core/models/domainlayer"
 	"time"
+
+	"github.com/apache/incubator-devlake/core/models/domainlayer"
 )
 
 type Ref struct {
-	domainlayer.DomainEntity
+	domainlayer.DomainEntityExtension
 	RepoId      string `gorm:"type:varchar(255)"`
 	Name        string `gorm:"type:varchar(255)"`
 	CommitSha   string `gorm:"type:varchar(40)"`
diff --git a/backend/core/models/domainlayer/domainlayer.go b/backend/core/models/domainlayer/domainlayer.go
index 5ade4907a..a57dbfd65 100644
--- a/backend/core/models/domainlayer/domainlayer.go
+++ b/backend/core/models/domainlayer/domainlayer.go
@@ -26,6 +26,11 @@ type DomainEntity struct {
 	common.NoPKModel
 }
 
+type DomainEntityExtension struct {
+	Id string `json:"id" gorm:"primaryKey;type:varchar(500);comment:This key is generated based on details from the original plugin"` // format: <Plugin>:<Entity>:<PK0>:<PK1>
+	common.NoPKModel
+}
+
 func NewDomainEntity(id string) DomainEntity {
 	return DomainEntity{
 		Id:        id,
diff --git a/backend/core/models/migrationscripts/20240116_modify_refs_id_length.go b/backend/core/models/migrationscripts/20240116_modify_refs_id_length.go
new file mode 100644
index 000000000..7c8812d05
--- /dev/null
+++ b/backend/core/models/migrationscripts/20240116_modify_refs_id_length.go
@@ -0,0 +1,109 @@
+/*
+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 (
+	"time"
+
+	"github.com/apache/incubator-devlake/core/context"
+	"github.com/apache/incubator-devlake/core/dal"
+	"github.com/apache/incubator-devlake/core/errors"
+	"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
+	"github.com/apache/incubator-devlake/core/plugin"
+	"github.com/apache/incubator-devlake/helpers/migrationhelper"
+)
+
+var _ plugin.MigrationScript = (*modifyRefsIdLength)(nil)
+
+type modifyRefsIdLength struct{}
+
+type ref20240116_old struct {
+	archived.DomainEntity
+	RepoId      string `gorm:"type:varchar(255)"`
+	Name        string `gorm:"type:varchar(255)"`
+	CommitSha   string `gorm:"type:varchar(40)"`
+	IsDefault   bool
+	RefType     string `gorm:"type:varchar(255)"`
+	CreatedDate *time.Time
+}
+
+type ref20240116 struct {
+	archived.DomainEntityExtension
+	RepoId      string `gorm:"type:varchar(255)"`
+	Name        string `gorm:"type:varchar(255)"`
+	CommitSha   string `gorm:"type:varchar(40)"`
+	IsDefault   bool
+	RefType     string `gorm:"type:varchar(255)"`
+	CreatedDate *time.Time
+}
+
+func (ref20240116) TableName() string {
+	return "refs"
+}
+
+func (script *modifyRefsIdLength) Up(basicRes context.BasicRes) errors.Error {
+	db := basicRes.GetDal()
+	// dbUrl := basicRes.GetConfig("DB_URL")
+	// if dbUrl == "" {
+	// 	return errors.BadInput.New("DB_URL is required")
+	// }
+	// u, err1 := url.Parse(dbUrl)
+	// if err1 != nil {
+	// 	return errors.Convert(err1)
+	// }
+	// if u.Scheme == "mysql" {
+	// 	if err := db.Exec("ALTER TABLE refs DROP PRIMARY KEY"); err != nil {
+	// 		return err
+	// 	}
+	// } else {
+	// 	if err := db.Exec("ALTER TABLE _tool_sonarqube_file_metrics DROP CONSTRAINT _tool_sonarqube_file_metrics_pkey"); err != nil {
+	// 		return err
+	// 	}
+	// }
+
+	err := migrationhelper.ChangeColumnsType[ref20240116](
+		basicRes,
+		script,
+		ref20240116{}.TableName(),
+		[]string{"id"},
+		func(tmpColumnParams []interface{}) errors.Error {
+			return db.UpdateColumn(
+				&ref20240116{},
+				"id",
+				dal.DalClause{Expr: " ? ", Params: tmpColumnParams},
+				dal.Where("? IS NOT NULL", tmpColumnParams...),
+			)
+		},
+	)
+	if err != nil {
+		return err
+	}
+
+	if err := db.Exec("ALTER TABLE refs ADD PRIMARY KEY (id)"); err != nil {
+		return err
+	}
+	return nil
+}
+
+func (*modifyRefsIdLength) Version() uint64 {
+	return 20240116155126
+}
+
+func (*modifyRefsIdLength) Name() string {
+	return "modify refs id length from 255 to 500"
+}
diff --git a/backend/core/models/migrationscripts/archived/base.go b/backend/core/models/migrationscripts/archived/base.go
index 86bfeef06..02e7f69c6 100644
--- a/backend/core/models/migrationscripts/archived/base.go
+++ b/backend/core/models/migrationscripts/archived/base.go
@@ -28,6 +28,11 @@ type DomainEntity struct {
 	NoPKModel
 }
 
+type DomainEntityExtension struct {
+	Id string `json:"id" gorm:"primaryKey;type:varchar(500);comment:This key is generated based on details from the original plugin"` // format: <Plugin>:<Entity>:<PK0>:<PK1>
+	NoPKModel
+}
+
 type Model struct {
 	ID        uint64    `gorm:"primaryKey" json:"id"`
 	CreatedAt time.Time `json:"createdAt"`
diff --git a/backend/core/models/migrationscripts/register.go b/backend/core/models/migrationscripts/register.go
index 74377d7d0..7e2d70af8 100644
--- a/backend/core/models/migrationscripts/register.go
+++ b/backend/core/models/migrationscripts/register.go
@@ -105,5 +105,6 @@ func All() []plugin.MigrationScript {
 		new(addCommitMsgtoPipelineCommit),
 		new(modfiyFieldsSort),
 		new(modifyIssueLeadTimeMinutesToUint),
+		new(modifyRefsIdLength),
 	}
 }
diff --git a/backend/plugins/gitextractor/parser/repo.go b/backend/plugins/gitextractor/parser/repo.go
index 411c183e4..46db33b9e 100644
--- a/backend/plugins/gitextractor/parser/repo.go
+++ b/backend/plugins/gitextractor/parser/repo.go
@@ -199,11 +199,11 @@ func (r *GitRepo) CollectBranches(subtaskCtx plugin.SubTaskContext) errors.Error
 				sha = oid.String()
 			}
 			ref := &code.Ref{
-				DomainEntity: domainlayer.DomainEntity{Id: fmt.Sprintf("%s:%s", r.id, name)},
-				RepoId:       r.id,
-				Name:         name,
-				CommitSha:    sha,
-				RefType:      BRANCH,
+				DomainEntityExtension: domainlayer.DomainEntityExtension{Id: fmt.Sprintf("%s:%s", r.id, name)},
+				RepoId:                r.id,
+				Name:                  name,
+				CommitSha:             sha,
+				RefType:               BRANCH,
 			}
 			ref.IsDefault, err1 = branch.IsHead()
 			if err1 != nil && err1.Error() != TypeNotMatchError {