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 2023/08/08 09:39:46 UTC

[incubator-devlake] branch main updated: refactor: replace datatypes.JSONMap for BambooScopeConfig.RepoMap (#5827)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 359f6ea8f refactor: replace datatypes.JSONMap for BambooScopeConfig.RepoMap (#5827)
359f6ea8f is described below

commit 359f6ea8f90d5934c6a374afb71a799bc8f22c5d
Author: Klesh Wong <zh...@merico.dev>
AuthorDate: Tue Aug 8 17:39:40 2023 +0800

    refactor: replace datatypes.JSONMap for BambooScopeConfig.RepoMap (#5827)
---
 backend/plugins/bamboo/models/scope_config.go      | 24 ++++++++++------
 .../shared_test.go => models/scope_config_test.go} | 33 ++++++++--------------
 .../plugins/bamboo/tasks/plan_commit_convertor.go  |  5 ++--
 backend/plugins/bamboo/tasks/shared.go             | 13 ---------
 4 files changed, 30 insertions(+), 45 deletions(-)

diff --git a/backend/plugins/bamboo/models/scope_config.go b/backend/plugins/bamboo/models/scope_config.go
index 6faf12693..5a14304ed 100644
--- a/backend/plugins/bamboo/models/scope_config.go
+++ b/backend/plugins/bamboo/models/scope_config.go
@@ -19,22 +19,30 @@ package models
 
 import (
 	"github.com/apache/incubator-devlake/core/models/common"
-	"gorm.io/datatypes"
 )
 
 const ENV_NAME_PATTERN = "ENV_NAME_PATTERN"
 
 type BambooScopeConfig struct {
 	common.ScopeConfig `mapstructure:",squash" json:",inline" gorm:"embedded"`
-	ConnectionId       uint64 `mapstructure:"connectionId" json:"connectionId"`
-	Name               string `gorm:"type:varchar(255);index:idx_name_gitlab,unique" validate:"required" mapstructure:"name" json:"name"`
-	// should be {realRepoName: [bamboo_repoId]}
-	RepoMap           datatypes.JSONMap
-	DeploymentPattern string `mapstructure:"deploymentPattern,omitempty" json:"deploymentPattern" gorm:"type:varchar(255)"`
-	ProductionPattern string `mapstructure:"productionPattern,omitempty" json:"productionPattern" gorm:"type:varchar(255)"`
-	EnvNamePattern    string `mapstructure:"envNamePattern,omitempty" json:"envNamePattern" gorm:"type:varchar(255)"`
+	ConnectionId       uint64           `mapstructure:"connectionId" json:"connectionId"`
+	Name               string           `gorm:"type:varchar(255);index:idx_name_gitlab,unique" validate:"required" mapstructure:"name" json:"name"`
+	RepoMap            map[string][]int `json:"repoMap" gorm:"type:json;serializer:json"` // should be {realRepoName: [bamboo_repoId, ...]}
+	DeploymentPattern  string           `mapstructure:"deploymentPattern,omitempty" json:"deploymentPattern" gorm:"type:varchar(255)"`
+	ProductionPattern  string           `mapstructure:"productionPattern,omitempty" json:"productionPattern" gorm:"type:varchar(255)"`
+	EnvNamePattern     string           `mapstructure:"envNamePattern,omitempty" json:"envNamePattern" gorm:"type:varchar(255)"`
 }
 
 func (BambooScopeConfig) TableName() string {
 	return "_tool_bamboo_scope_configs"
 }
+
+func (cfg *BambooScopeConfig) GetRepoIdMap() map[int]string {
+	repoMap := make(map[int]string)
+	for k, v := range cfg.RepoMap {
+		for _, id := range v {
+			repoMap[id] = k
+		}
+	}
+	return repoMap
+}
diff --git a/backend/plugins/bamboo/tasks/shared_test.go b/backend/plugins/bamboo/models/scope_config_test.go
similarity index 64%
rename from backend/plugins/bamboo/tasks/shared_test.go
rename to backend/plugins/bamboo/models/scope_config_test.go
index 420370340..3e85d8346 100644
--- a/backend/plugins/bamboo/tasks/shared_test.go
+++ b/backend/plugins/bamboo/models/scope_config_test.go
@@ -15,29 +15,22 @@ See the License for the specific language governing permissions and
 limitations under the License.
 */
 
-package tasks
+package models
 
 import (
-	"encoding/json"
-	"gorm.io/datatypes"
-	"reflect"
 	"testing"
+
+	"github.com/magiconair/properties/assert"
 )
 
-func TestGetRepoMap(t *testing.T) {
-	rawJSON := `
-	{
-		"repo1": [1, 2],
-		"repo2": [3],
-		"repo3": [4, 5, 6]
-	}
-	`
-	var rawRepoMap datatypes.JSONMap
-	err := json.Unmarshal([]byte(rawJSON), &rawRepoMap)
-	if err != nil {
-		t.Errorf("Failed to unmarshal rawJSON: %v", err)
+func TestGetRepoIdMap(t *testing.T) {
+	scopeConfig := &BambooScopeConfig{
+		RepoMap: map[string][]int{
+			"repo1": {1, 2},
+			"repo2": {3},
+			"repo3": {4, 5, 6},
+		},
 	}
-
 	expectedRepoMap := map[int]string{
 		1: "repo1",
 		2: "repo1",
@@ -47,9 +40,5 @@ func TestGetRepoMap(t *testing.T) {
 		6: "repo3",
 	}
 
-	repoMap := getRepoMap(rawRepoMap)
-
-	if !reflect.DeepEqual(repoMap, expectedRepoMap) {
-		t.Errorf("Unexpected result. Expected: %v, got: %v", expectedRepoMap, repoMap)
-	}
+	assert.Equal(t, scopeConfig.GetRepoIdMap(), expectedRepoMap, "Unexpected result")
 }
diff --git a/backend/plugins/bamboo/tasks/plan_commit_convertor.go b/backend/plugins/bamboo/tasks/plan_commit_convertor.go
index 6144b082f..a6d891eb9 100644
--- a/backend/plugins/bamboo/tasks/plan_commit_convertor.go
+++ b/backend/plugins/bamboo/tasks/plan_commit_convertor.go
@@ -18,9 +18,10 @@ limitations under the License.
 package tasks
 
 import (
-	"github.com/apache/incubator-devlake/core/models/domainlayer/devops"
 	"reflect"
 
+	"github.com/apache/incubator-devlake/core/models/domainlayer/devops"
+
 	"github.com/apache/incubator-devlake/core/dal"
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/models/domainlayer/didgen"
@@ -50,7 +51,7 @@ func ConvertPlanVcs(taskCtx plugin.SubTaskContext) errors.Error {
 	defer cursor.Close()
 
 	planBuildIdGen := didgen.NewDomainIdGenerator(&models.BambooPlanBuild{})
-	repoMap := getRepoMap(data.Options.RepoMap)
+	repoMap := data.Options.GetRepoIdMap()
 	converter, err := api.NewDataConverter(api.DataConverterArgs{
 		InputRowType:       reflect.TypeOf(models.BambooPlanBuildVcsRevision{}),
 		Input:              cursor,
diff --git a/backend/plugins/bamboo/tasks/shared.go b/backend/plugins/bamboo/tasks/shared.go
index 96efc1660..554a2c75e 100644
--- a/backend/plugins/bamboo/tasks/shared.go
+++ b/backend/plugins/bamboo/tasks/shared.go
@@ -27,7 +27,6 @@ import (
 	"github.com/apache/incubator-devlake/core/plugin"
 	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
 	"github.com/apache/incubator-devlake/plugins/bamboo/models"
-	"gorm.io/datatypes"
 )
 
 func CreateRawDataSubTaskArgs(taskCtx plugin.SubTaskContext, rawTable string) (*api.RawDataSubTaskArgs, *BambooTaskData) {
@@ -87,15 +86,3 @@ func GetResultsResult(res *http.Response) ([]json.RawMessage, errors.Error) {
 	}
 	return resData.Results.Result, nil
 }
-
-func getRepoMap(rawRepoMap datatypes.JSONMap) map[int]string {
-	repoMap := make(map[int]string)
-	for k, v := range rawRepoMap {
-		if list, ok := v.([]interface{}); ok {
-			for _, id := range list {
-				repoMap[int(id.(float64))] = k
-			}
-		}
-	}
-	return repoMap
-}