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/09/19 04:14:34 UTC

[incubator-devlake] branch main updated: feat: add deployTagPattern for gitlab

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


The following commit(s) were added to refs/heads/main by this push:
     new 89a0343e feat: add deployTagPattern for gitlab
89a0343e is described below

commit 89a0343e25b6cb60c1ca32e6893a7408144ca337
Author: abeizn <zi...@merico.dev>
AuthorDate: Mon Sep 19 10:30:07 2022 +0800

    feat: add deployTagPattern for gitlab
---
 plugins/gitlab/gitlab.go              | 39 +++++++++++++++++++++++++++--------
 plugins/gitlab/models/connection.go   |  1 +
 plugins/gitlab/tasks/job_convertor.go | 22 +++++++++++++++++---
 plugins/gitlab/tasks/task_data.go     |  3 +++
 4 files changed, 53 insertions(+), 12 deletions(-)

diff --git a/plugins/gitlab/gitlab.go b/plugins/gitlab/gitlab.go
index 7f85f7c7..ece1faa6 100644
--- a/plugins/gitlab/gitlab.go
+++ b/plugins/gitlab/gitlab.go
@@ -28,18 +28,39 @@ var PluginEntry impl.Gitlab //nolint
 
 // standalone mode for debugging
 func main() {
-	gitlabCmd := &cobra.Command{Use: "gitlab"}
-	projectId := gitlabCmd.Flags().IntP("project-id", "p", 0, "gitlab project id")
-	connectionId := gitlabCmd.Flags().Uint64P("connection-id", "c", 0, "gitlab connection id")
+	cmd := &cobra.Command{Use: "gitlab"}
+	projectId := cmd.Flags().IntP("project-id", "p", 0, "gitlab project id")
+	connectionId := cmd.Flags().Uint64P("connection-id", "c", 0, "gitlab connection id")
+	_ = cmd.MarkFlagRequired("project-id")
+	_ = cmd.MarkFlagRequired("connection-id")
 
-	_ = gitlabCmd.MarkFlagRequired("project-id")
-	_ = gitlabCmd.MarkFlagRequired("connection-id")
-	gitlabCmd.Run = func(cmd *cobra.Command, args []string) {
+	prType := cmd.Flags().String("prType", "type/(.*)$", "pr type")
+	prComponent := cmd.Flags().String("prComponent", "component/(.*)$", "pr component")
+	prBodyClosePattern := cmd.Flags().String("prBodyClosePattern", "(?mi)(fix|close|resolve|fixes|closes|resolves|fixed|closed|resolved)[\\s]*.*(((and )?(#|https:\\/\\/github.com\\/%s\\/%s\\/issues\\/)\\d+[ ]*)+)", "pr body close pattern")
+	issueSeverity := cmd.Flags().String("issueSeverity", "severity/(.*)$", "issue severity")
+	issuePriority := cmd.Flags().String("issuePriority", "^(highest|high|medium|low)$", "issue priority")
+	issueComponent := cmd.Flags().String("issueComponent", "component/(.*)$", "issue component")
+	issueTypeBug := cmd.Flags().String("issueTypeBug", "^(bug|failure|error)$", "issue type bug")
+	issueTypeIncident := cmd.Flags().String("issueTypeIncident", "", "issue type incident")
+	issueTypeRequirement := cmd.Flags().String("issueTypeRequirement", "^(feat|feature|proposal|requirement)$", "issue type requirement")
+	deployTagPattern := cmd.Flags().String("deployTagPattern", "(?i)deploy", "deploy tag name")
+
+	cmd.Run = func(cmd *cobra.Command, args []string) {
 		runner.DirectRun(cmd, args, PluginEntry, map[string]interface{}{
-			"projectId":    *projectId,
-			"connectionId": *connectionId,
+			"projectId":            *projectId,
+			"connectionId":         *connectionId,
+			"prType":               *prType,
+			"prComponent":          *prComponent,
+			"prBodyClosePattern":   *prBodyClosePattern,
+			"issueSeverity":        *issueSeverity,
+			"issuePriority":        *issuePriority,
+			"issueComponent":       *issueComponent,
+			"issueTypeBug":         *issueTypeBug,
+			"issueTypeIncident":    *issueTypeIncident,
+			"issueTypeRequirement": *issueTypeRequirement,
+			"deployTagPattern":     *deployTagPattern,
 		})
 	}
 
-	runner.RunCmd(gitlabCmd)
+	runner.RunCmd(cmd)
 }
diff --git a/plugins/gitlab/models/connection.go b/plugins/gitlab/models/connection.go
index 5f79e16f..8fbac80f 100644
--- a/plugins/gitlab/models/connection.go
+++ b/plugins/gitlab/models/connection.go
@@ -54,6 +54,7 @@ type TransformationRules struct {
 	IssueTypeBug         string `mapstructure:"issueTypeBug" json:"issueTypeBug"`
 	IssueTypeIncident    string `mapstructure:"issueTypeIncident" json:"issueTypeIncident"`
 	IssueTypeRequirement string `mapstructure:"issueTypeRequirement" json:"issueTypeRequirement"`
+	DeployTagPattern     string `mapstructure:"deployTagPattern" json:"deployTagPattern"`
 }
 
 func (GitlabConnection) TableName() string {
diff --git a/plugins/gitlab/tasks/job_convertor.go b/plugins/gitlab/tasks/job_convertor.go
index 1ecc1576..10dd2124 100644
--- a/plugins/gitlab/tasks/job_convertor.go
+++ b/plugins/gitlab/tasks/job_convertor.go
@@ -18,6 +18,9 @@ limitations under the License.
 package tasks
 
 import (
+	"reflect"
+	"regexp"
+
 	"github.com/apache/incubator-devlake/errors"
 	"github.com/apache/incubator-devlake/models/domainlayer"
 	"github.com/apache/incubator-devlake/models/domainlayer/devops"
@@ -26,7 +29,6 @@ import (
 	"github.com/apache/incubator-devlake/plugins/core/dal"
 	gitlabModels "github.com/apache/incubator-devlake/plugins/gitlab/models"
 	"github.com/apache/incubator-devlake/plugins/helper"
-	"reflect"
 )
 
 var ConvertJobMeta = core.SubTaskMeta{
@@ -37,10 +39,19 @@ var ConvertJobMeta = core.SubTaskMeta{
 	DomainTypes:      []string{core.DOMAIN_TYPE_CROSS},
 }
 
-func ConvertJobs(taskCtx core.SubTaskContext) errors.Error {
+func ConvertJobs(taskCtx core.SubTaskContext) (err errors.Error) {
 	db := taskCtx.GetDal()
 	data := taskCtx.GetData().(*GitlabTaskData)
 
+	var deployTagRegexp *regexp.Regexp
+	deployTagPattern := data.Options.DeployTagPattern
+	if len(deployTagPattern) > 0 {
+		deployTagRegexp, err = errors.Convert01(regexp.Compile(deployTagPattern))
+		if err != nil {
+			return errors.Default.Wrap(err, "regexp compile deployTagPattern failed")
+		}
+	}
+
 	cursor, err := db.Cursor(dal.From(gitlabModels.GitlabJob{}))
 	if err != nil {
 		return err
@@ -58,7 +69,7 @@ func ConvertJobs(taskCtx core.SubTaskContext) errors.Error {
 				ConnectionId: data.Options.ConnectionId,
 				ProjectId:    data.Options.ProjectId,
 			},
-			Table: RAW_USER_TABLE,
+			Table: RAW_JOB_TABLE,
 		},
 		Convert: func(inputRow interface{}) ([]interface{}, errors.Error) {
 			gitlabJob := inputRow.(*gitlabModels.GitlabJob)
@@ -91,6 +102,11 @@ func ConvertJobs(taskCtx core.SubTaskContext) errors.Error {
 				StartedDate:  *startedAt,
 				FinishedDate: gitlabJob.FinishedAt,
 			}
+			if deployTagRegexp != nil {
+				if deployFlag := deployTagRegexp.FindString(gitlabJob.Name); deployFlag != "" {
+					domainJob.Type = devops.DEPLOYMENT
+				}
+			}
 
 			return []interface{}{
 				domainJob,
diff --git a/plugins/gitlab/tasks/task_data.go b/plugins/gitlab/tasks/task_data.go
index 2e66fc94..0095507a 100644
--- a/plugins/gitlab/tasks/task_data.go
+++ b/plugins/gitlab/tasks/task_data.go
@@ -76,6 +76,9 @@ func DecodeAndValidateTaskOptions(options map[string]interface{}) (*GitlabOption
 	if op.IssueTypeRequirement == "" {
 		op.IssueTypeRequirement = "^(feat|feature|proposal|requirement)$"
 	}
+	if op.DeployTagPattern == "" {
+		op.DeployTagPattern = "(?i)deploy"
+	}
 
 	// find the needed GitHub now
 	if op.ConnectionId == 0 {