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 10:27:33 UTC

[incubator-devlake] branch main updated: fix(gitlab): add envNamePattern, and fix missing ScopeConfig (#6343)

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 aafac0332 fix(gitlab): add envNamePattern, and fix missing ScopeConfig (#6343)
aafac0332 is described below

commit aafac0332ee141e1e49429fe4c720ad9dd291fa5
Author: Lynwee <li...@merico.dev>
AuthorDate: Thu Oct 26 18:27:28 2023 +0800

    fix(gitlab): add envNamePattern, and fix missing ScopeConfig (#6343)
---
 .../helpers/pluginhelper/api/enrich_with_regex.go  |  2 +-
 backend/plugins/gitlab/impl/impl.go                |  8 ++++
 ...v_name_pattern_to_tool_gitlab_scope_configs.go} | 49 ++++++++++++----------
 .../gitlab/models/migrationscripts/register.go     |  1 +
 backend/plugins/gitlab/models/scope_config.go      |  1 +
 .../plugins/gitlab/tasks/deployment_convertor.go   |  4 +-
 backend/plugins/gitlab/tasks/task_data.go          |  3 --
 7 files changed, 42 insertions(+), 26 deletions(-)

diff --git a/backend/helpers/pluginhelper/api/enrich_with_regex.go b/backend/helpers/pluginhelper/api/enrich_with_regex.go
index 92e0c0b28..5e7bd2a3a 100644
--- a/backend/helpers/pluginhelper/api/enrich_with_regex.go
+++ b/backend/helpers/pluginhelper/api/enrich_with_regex.go
@@ -98,7 +98,7 @@ func (r *RegexEnricher) ReturnNameIfMatched(name string, targets ...string) stri
 	return ""
 }
 
-// ReturnNameIfMatchedOrOmitted returns the given name if regex of the given name is omitted or fallback to ReturnNameIfMatched
+// ReturnNameIfOmittedOrMatched returns the given name if regex of the given name is omitted or fallback to ReturnNameIfMatched
 func (r *RegexEnricher) ReturnNameIfOmittedOrMatched(name string, targets ...string) string {
 	if _, ok := r.regexpMap[name]; !ok {
 		return name
diff --git a/backend/plugins/gitlab/impl/impl.go b/backend/plugins/gitlab/impl/impl.go
index afa9e10c3..bf25186f9 100644
--- a/backend/plugins/gitlab/impl/impl.go
+++ b/backend/plugins/gitlab/impl/impl.go
@@ -156,6 +156,11 @@ func (p Gitlab) PrepareTaskData(taskCtx plugin.TaskContext, options map[string]i
 		// If we still cannot find the record in db, we have to request from remote server and save it to db
 		db := taskCtx.GetDal()
 		err = db.First(&scope, dal.Where("connection_id = ? AND gitlab_id = ?", op.ConnectionId, op.ProjectId))
+		if err == nil {
+			if op.ScopeConfigId == 0 && scope.ScopeConfigId != 0 {
+				op.ScopeConfigId = scope.ScopeConfigId
+			}
+		}
 		if err != nil && db.IsErrorNotFound(err) {
 			var project *models.GitlabApiProject
 			project, err = api.GetApiProject(op, apiClient)
@@ -196,6 +201,9 @@ func (p Gitlab) 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.GitlabTaskData{
 		Options:       op,
diff --git a/backend/plugins/gitlab/models/migrationscripts/register.go b/backend/plugins/gitlab/models/migrationscripts/20231026_add_env_name_pattern_to_tool_gitlab_scope_configs.go
similarity index 50%
copy from backend/plugins/gitlab/models/migrationscripts/register.go
copy to backend/plugins/gitlab/models/migrationscripts/20231026_add_env_name_pattern_to_tool_gitlab_scope_configs.go
index c6c547d2e..49e748779 100644
--- a/backend/plugins/gitlab/models/migrationscripts/register.go
+++ b/backend/plugins/gitlab/models/migrationscripts/20231026_add_env_name_pattern_to_tool_gitlab_scope_configs.go
@@ -18,28 +18,35 @@ limitations under the License.
 package migrationscripts
 
 import (
+	"github.com/apache/incubator-devlake/core/context"
+	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
+	"github.com/apache/incubator-devlake/helpers/migrationhelper"
 )
 
-// All return all the migration scripts
-func All() []plugin.MigrationScript {
-	return []plugin.MigrationScript{
-		new(addInitTables),
-		new(addGitlabCI),
-		new(addPipelineID),
-		new(addPipelineProjects),
-		new(fixDurationToFloat8),
-		new(addTransformationRule20221125),
-		new(addStdTypeToIssue221230),
-		new(addIsDetailRequired20230210),
-		new(addConnectionIdToTransformationRule),
-		new(addGitlabCommitAuthorInfo),
-		new(addTypeEnvToPipeline),
-		new(renameTr2ScopeConfig),
-		new(addGitlabIssueAssignee),
-		new(addMrCommitSha),
-		new(addRawParamTableForScope),
-		new(addProjectArchived),
-		new(addDeployment),
-	}
+var _ plugin.MigrationScript = (*addEnvNamePattern)(nil)
+
+type scopeConfig20231026 struct {
+	EnvNamePattern string `mapstructure:"envNamePattern,omitempty" json:"envNamePattern" gorm:"type:varchar(255)"`
+}
+
+func (scopeConfig20231026) TableName() string {
+	return "_tool_gitlab_scope_configs"
+}
+
+type addEnvNamePattern struct{}
+
+func (script *addEnvNamePattern) Up(basicRes context.BasicRes) errors.Error {
+	return migrationhelper.AutoMigrateTables(
+		basicRes,
+		&scopeConfig20231026{},
+	)
+}
+
+func (*addEnvNamePattern) Version() uint64 {
+	return 20231026165000
+}
+
+func (script *addEnvNamePattern) Name() string {
+	return "add env_name_pattern to _tool_gitlab_scope_configs"
 }
diff --git a/backend/plugins/gitlab/models/migrationscripts/register.go b/backend/plugins/gitlab/models/migrationscripts/register.go
index c6c547d2e..49dce25c2 100644
--- a/backend/plugins/gitlab/models/migrationscripts/register.go
+++ b/backend/plugins/gitlab/models/migrationscripts/register.go
@@ -41,5 +41,6 @@ func All() []plugin.MigrationScript {
 		new(addRawParamTableForScope),
 		new(addProjectArchived),
 		new(addDeployment),
+		new(addEnvNamePattern),
 	}
 }
diff --git a/backend/plugins/gitlab/models/scope_config.go b/backend/plugins/gitlab/models/scope_config.go
index 23bc35c34..d74d2b9a4 100644
--- a/backend/plugins/gitlab/models/scope_config.go
+++ b/backend/plugins/gitlab/models/scope_config.go
@@ -37,6 +37,7 @@ type GitlabScopeConfig struct {
 	IssueTypeRequirement string            `mapstructure:"issueTypeRequirement" json:"issueTypeRequirement"`
 	DeploymentPattern    string            `mapstructure:"deploymentPattern" json:"deploymentPattern"`
 	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/gitlab/tasks/deployment_convertor.go b/backend/plugins/gitlab/tasks/deployment_convertor.go
index 480806adc..e1f9b87df 100644
--- a/backend/plugins/gitlab/tasks/deployment_convertor.go
+++ b/backend/plugins/gitlab/tasks/deployment_convertor.go
@@ -120,7 +120,9 @@ func ConvertDeployment(taskCtx plugin.SubTaskContext) errors.Error {
 				domainDeployCommit.DurationSec = duration
 			}
 			if data.RegexEnricher != nil {
-				domainDeployCommit.Environment = data.RegexEnricher.ReturnNameIfOmittedOrMatched(devops.PRODUCTION, gitlabDeployment.Environment)
+				if data.RegexEnricher.ReturnNameIfMatched(devops.ENV_NAME_PATTERN, gitlabDeployment.Environment) != "" {
+					domainDeployCommit.Environment = devops.PRODUCTION
+				}
 			}
 
 			domainDeployCommit.CicdDeploymentId = domainDeployCommit.Id
diff --git a/backend/plugins/gitlab/tasks/task_data.go b/backend/plugins/gitlab/tasks/task_data.go
index 45e4abe3b..0eaa36e45 100644
--- a/backend/plugins/gitlab/tasks/task_data.go
+++ b/backend/plugins/gitlab/tasks/task_data.go
@@ -49,8 +49,5 @@ func DecodeAndValidateTaskOptions(options map[string]interface{}) (*GitlabOption
 	if op.ConnectionId == 0 {
 		return nil, errors.BadInput.New("connectionId is invalid")
 	}
-	if op.ScopeConfig == nil && op.ScopeConfigId == 0 {
-		op.ScopeConfig = new(models.GitlabScopeConfig)
-	}
 	return &op, nil
 }