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/10/26 08:25:40 UTC

[incubator-devlake] branch main updated: fix(github): add envNamePattern (#6340)

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 ca4396378 fix(github): add envNamePattern (#6340)
ca4396378 is described below

commit ca4396378aecd3674f11b98464746119eb56142d
Author: Lynwee <li...@merico.dev>
AuthorDate: Thu Oct 26 16:25:36 2023 +0800

    fix(github): add envNamePattern (#6340)
---
 .../core/models/domainlayer/devops/cicd_task.go    |  2 ++
 backend/plugins/bamboo/impl/impl.go                |  2 +-
 .../20230807_add_plan_result_key.go                |  2 +-
 backend/plugins/bamboo/models/scope_config.go      |  2 --
 .../plugins/bamboo/tasks/deploy_build_convertor.go |  2 +-
 backend/plugins/github/impl/impl.go                |  3 ++
 ...v_name_pattern_to_tool_github_scope_configs.go} | 32 ++++++++--------------
 .../github/models/migrationscripts/register.go     |  1 +
 backend/plugins/github/models/scope_config.go      |  1 +
 backend/plugins/github_graphql/impl/impl.go        |  3 ++
 .../github_graphql/tasks/deployment_convertor.go   |  4 ++-
 11 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/backend/core/models/domainlayer/devops/cicd_task.go b/backend/core/models/domainlayer/devops/cicd_task.go
index 86847c146..6c925355f 100644
--- a/backend/core/models/domainlayer/devops/cicd_task.go
+++ b/backend/core/models/domainlayer/devops/cicd_task.go
@@ -35,6 +35,8 @@ const (
 	TESTING    = "TESTING"
 )
 
+const ENV_NAME_PATTERN = "ENV_NAME_PATTERN"
+
 type CICDTask struct {
 	domainlayer.DomainEntity
 	Name         string `gorm:"type:varchar(255)"`
diff --git a/backend/plugins/bamboo/impl/impl.go b/backend/plugins/bamboo/impl/impl.go
index a5e16d592..05965c456 100644
--- a/backend/plugins/bamboo/impl/impl.go
+++ b/backend/plugins/bamboo/impl/impl.go
@@ -182,7 +182,7 @@ func (p Bamboo) PrepareTaskData(taskCtx plugin.TaskContext, options map[string]i
 	if err := regexEnricher.TryAdd(devops.PRODUCTION, op.ProductionPattern); err != nil {
 		return nil, errors.BadInput.Wrap(err, "invalid value for `productionPattern`")
 	}
-	if err := regexEnricher.TryAdd(models.ENV_NAME_PATTERN, op.EnvNamePattern); err != nil {
+	if err := regexEnricher.TryAdd(devops.ENV_NAME_PATTERN, op.EnvNamePattern); err != nil {
 		return nil, errors.BadInput.Wrap(err, "invalid value for `envNamePattern`")
 	}
 	return &tasks.BambooTaskData{
diff --git a/backend/plugins/bamboo/models/migrationscripts/20230807_add_plan_result_key.go b/backend/plugins/bamboo/models/migrationscripts/20230807_add_plan_result_key.go
index e2c3241fe..545c39dd9 100644
--- a/backend/plugins/bamboo/models/migrationscripts/20230807_add_plan_result_key.go
+++ b/backend/plugins/bamboo/models/migrationscripts/20230807_add_plan_result_key.go
@@ -24,7 +24,7 @@ import (
 	"github.com/apache/incubator-devlake/helpers/migrationhelper"
 )
 
-var _ plugin.MigrationScript = (*addEnvNamePattern)(nil)
+var _ plugin.MigrationScript = (*addPlanResultKey)(nil)
 
 type deployBuild20220807 struct {
 	PlanResultKey  string `gorm:"primaryKey"`
diff --git a/backend/plugins/bamboo/models/scope_config.go b/backend/plugins/bamboo/models/scope_config.go
index 35c816a44..abbb008fb 100644
--- a/backend/plugins/bamboo/models/scope_config.go
+++ b/backend/plugins/bamboo/models/scope_config.go
@@ -21,8 +21,6 @@ import (
 	"github.com/apache/incubator-devlake/core/models/common"
 )
 
-const ENV_NAME_PATTERN = "ENV_NAME_PATTERN"
-
 type BambooScopeConfig struct {
 	common.ScopeConfig `mapstructure:",squash" json:",inline" gorm:"embedded"`
 	ConnectionId       uint64           `mapstructure:"connectionId" json:"connectionId"`
diff --git a/backend/plugins/bamboo/tasks/deploy_build_convertor.go b/backend/plugins/bamboo/tasks/deploy_build_convertor.go
index 052c197e3..cad5c5b2f 100644
--- a/backend/plugins/bamboo/tasks/deploy_build_convertor.go
+++ b/backend/plugins/bamboo/tasks/deploy_build_convertor.go
@@ -115,7 +115,7 @@ func ConvertDeployBuilds(taskCtx plugin.SubTaskContext) errors.Error {
 			if input.QueuedDate != nil {
 				deploymentCommit.CreatedDate = *input.QueuedDate
 			}
-			if data.RegexEnricher.ReturnNameIfMatched(models.ENV_NAME_PATTERN, input.Environment) != "" {
+			if data.RegexEnricher.ReturnNameIfMatched(devops.ENV_NAME_PATTERN, input.Environment) != "" {
 				deploymentCommit.Environment = devops.PRODUCTION
 			}
 			if input.FinishedDate != nil && input.StartedDate != nil {
diff --git a/backend/plugins/github/impl/impl.go b/backend/plugins/github/impl/impl.go
index 77ac52f1d..51ed55dcd 100644
--- a/backend/plugins/github/impl/impl.go
+++ b/backend/plugins/github/impl/impl.go
@@ -152,6 +152,9 @@ func (p Github) PrepareTaskData(taskCtx plugin.TaskContext, options map[string]i
 	if err = regexEnricher.TryAdd(devops.PRODUCTION, op.ScopeConfig.ProductionPattern); err != nil {
 		return nil, errors.BadInput.Wrap(err, "invalid value for `productionPattern`")
 	}
+	if err = regexEnricher.TryAdd(devops.ENV_NAME_PATTERN, op.ScopeConfig.EnvNamePattern); err != nil {
+		return nil, errors.BadInput.Wrap(err, "invalid value for `envNamePattern`")
+	}
 
 	taskData := &tasks.GithubTaskData{
 		Options:       op,
diff --git a/backend/plugins/bamboo/models/migrationscripts/20230807_add_plan_result_key.go b/backend/plugins/github/models/migrationscripts/20231026_add_env_name_pattern_to_tool_github_scope_configs.go
similarity index 59%
copy from backend/plugins/bamboo/models/migrationscripts/20230807_add_plan_result_key.go
copy to backend/plugins/github/models/migrationscripts/20231026_add_env_name_pattern_to_tool_github_scope_configs.go
index e2c3241fe..43bc2efc8 100644
--- a/backend/plugins/bamboo/models/migrationscripts/20230807_add_plan_result_key.go
+++ b/backend/plugins/github/models/migrationscripts/20231026_add_env_name_pattern_to_tool_github_scope_configs.go
@@ -26,37 +26,27 @@ import (
 
 var _ plugin.MigrationScript = (*addEnvNamePattern)(nil)
 
-type deployBuild20220807 struct {
-	PlanResultKey  string `gorm:"primaryKey"`
-	PlanBranchName string `gorm:"type:varchar(255)"`
+type scopeConfig20231026 struct {
+	EnvNamePattern string `mapstructure:"envNamePattern,omitempty" json:"envNamePattern" gorm:"type:varchar(255)"`
 }
 
-func (deployBuild20220807) TableName() string {
-	return "_tool_bamboo_deploy_build"
+func (scopeConfig20231026) TableName() string {
+	return "_tool_github_scope_configs"
 }
 
-type planBuildVcsRevision20220807 struct {
-	PlanResultKey string `gorm:"primaryKey"`
-}
-
-func (planBuildVcsRevision20220807) TableName() string {
-	return "_tool_bamboo_plan_build_commits"
-}
-
-type addPlanResultKey struct{}
+type addEnvNamePattern struct{}
 
-func (script *addPlanResultKey) Up(basicRes context.BasicRes) errors.Error {
+func (script *addEnvNamePattern) Up(basicRes context.BasicRes) errors.Error {
 	return migrationhelper.AutoMigrateTables(
 		basicRes,
-		&deployBuild20220807{},
-		&planBuildVcsRevision20220807{},
+		&scopeConfig20231026{},
 	)
 }
 
-func (*addPlanResultKey) Version() uint64 {
-	return 20230807165119
+func (*addEnvNamePattern) Version() uint64 {
+	return 20231026094400
 }
 
-func (script *addPlanResultKey) Name() string {
-	return "add plan_result_key to _tool_bamboo_deploy_build and _tool_bamboo_plan_build_commits"
+func (script *addEnvNamePattern) Name() string {
+	return "add env_name_pattern to _tool_github_scope_configs"
 }
diff --git a/backend/plugins/github/models/migrationscripts/register.go b/backend/plugins/github/models/migrationscripts/register.go
index c101043bd..deac0d93d 100644
--- a/backend/plugins/github/models/migrationscripts/register.go
+++ b/backend/plugins/github/models/migrationscripts/register.go
@@ -45,5 +45,6 @@ func All() []plugin.MigrationScript {
 		new(addRawParamTableForScope),
 		new(addDeploymentTable),
 		new(modifyGithubMilestone),
+		new(addEnvNamePattern),
 	}
 }
diff --git a/backend/plugins/github/models/scope_config.go b/backend/plugins/github/models/scope_config.go
index 625b094e1..16e1ad875 100644
--- a/backend/plugins/github/models/scope_config.go
+++ b/backend/plugins/github/models/scope_config.go
@@ -39,6 +39,7 @@ type GithubScopeConfig struct {
 	IssueTypeRequirement string            `mapstructure:"issueTypeRequirement,omitempty" json:"issueTypeRequirement" gorm:"type:varchar(255)"`
 	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)"`
 	Refdiff              datatypes.JSONMap `mapstructure:"refdiff,omitempty" json:"refdiff" swaggertype:"object" format:"json"`
 }
 
diff --git a/backend/plugins/github_graphql/impl/impl.go b/backend/plugins/github_graphql/impl/impl.go
index a1c46d626..0b1fef529 100644
--- a/backend/plugins/github_graphql/impl/impl.go
+++ b/backend/plugins/github_graphql/impl/impl.go
@@ -231,6 +231,9 @@ func (p GithubGraphql) PrepareTaskData(taskCtx plugin.TaskContext, options map[s
 	if err = regexEnricher.TryAdd(devops.PRODUCTION, op.ScopeConfig.ProductionPattern); err != nil {
 		return nil, errors.BadInput.Wrap(err, "invalid value for `productionPattern`")
 	}
+	if err = regexEnricher.TryAdd(devops.ENV_NAME_PATTERN, op.ScopeConfig.EnvNamePattern); err != nil {
+		return nil, errors.BadInput.Wrap(err, "invalid value for `envNamePattern`")
+	}
 
 	taskData := &githubTasks.GithubTaskData{
 		Options:       &op,
diff --git a/backend/plugins/github_graphql/tasks/deployment_convertor.go b/backend/plugins/github_graphql/tasks/deployment_convertor.go
index fbfd33277..118f20ce8 100644
--- a/backend/plugins/github_graphql/tasks/deployment_convertor.go
+++ b/backend/plugins/github_graphql/tasks/deployment_convertor.go
@@ -96,7 +96,9 @@ func ConvertDeployment(taskCtx plugin.SubTaskContext) errors.Error {
 			deploymentCommit.DurationSec = &durationSec
 
 			if data.RegexEnricher != nil {
-				deploymentCommit.Environment = data.RegexEnricher.ReturnNameIfMatched(devops.PRODUCTION, githubDeployment.Environment)
+				if data.RegexEnricher.ReturnNameIfMatched(devops.ENV_NAME_PATTERN, githubDeployment.Environment) != "" {
+					deploymentCommit.Environment = devops.PRODUCTION
+				}
 			}
 
 			deploymentCommit.CicdDeploymentId = deploymentCommit.Id