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/16 11:23:37 UTC

[incubator-devlake] 01/02: feat: add deployTagPattern for github

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

commit 8058a90cf703935fb63c1bc74a939a10bc40c8f9
Author: abeizn <zi...@merico.dev>
AuthorDate: Fri Sep 16 17:25:25 2022 +0800

    feat: add deployTagPattern for github
    
    feat: add deployTagPattern for github
---
 models/domainlayer/devops/cicd_task.go     | 13 +++++++++++++
 plugins/github/github.go                   |  2 ++
 plugins/github/models/connection.go        |  1 +
 plugins/github/tasks/cicd_job_convertor.go | 20 +++++++++++++++++---
 plugins/github/tasks/task_data.go          |  6 +++++-
 5 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/models/domainlayer/devops/cicd_task.go b/models/domainlayer/devops/cicd_task.go
index 666f617f..2a786e13 100644
--- a/models/domainlayer/devops/cicd_task.go
+++ b/models/domainlayer/devops/cicd_task.go
@@ -23,6 +23,19 @@ import (
 	"github.com/apache/incubator-devlake/models/domainlayer"
 )
 
+const (
+	TEST       = "TEST"
+	LINT       = "LINT"
+	BUILD      = "BUILD"
+	DEPLOYMENT = "DEPLOYMENT"
+)
+
+const (
+	PRODUCTION = "PRODUCTION"
+	STAGING    = "STAGING"
+	TESTING    = "TESTING"
+)
+
 type CICDTask struct {
 	domainlayer.DomainEntity
 	Name         string `gorm:"type:varchar(255)"`
diff --git a/plugins/github/github.go b/plugins/github/github.go
index c050a628..aeb637f5 100644
--- a/plugins/github/github.go
+++ b/plugins/github/github.go
@@ -45,6 +45,7 @@ func main() {
 	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{}{
@@ -60,6 +61,7 @@ func main() {
 			"issueTypeBug":         *issueTypeBug,
 			"issueTypeIncident":    *issueTypeIncident,
 			"issueTypeRequirement": *issueTypeRequirement,
+			"deployTagPattern":     *deployTagPattern,
 		})
 	}
 	runner.RunCmd(cmd)
diff --git a/plugins/github/models/connection.go b/plugins/github/models/connection.go
index ab71acb4..d182dce8 100644
--- a/plugins/github/models/connection.go
+++ b/plugins/github/models/connection.go
@@ -42,6 +42,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 (GithubConnection) TableName() string {
diff --git a/plugins/github/tasks/cicd_job_convertor.go b/plugins/github/tasks/cicd_job_convertor.go
index dd2122ca..83c7683b 100644
--- a/plugins/github/tasks/cicd_job_convertor.go
+++ b/plugins/github/tasks/cicd_job_convertor.go
@@ -19,8 +19,10 @@ package tasks
 
 import (
 	"fmt"
-	"github.com/apache/incubator-devlake/errors"
 	"reflect"
+	"regexp"
+
+	"github.com/apache/incubator-devlake/errors"
 
 	"github.com/apache/incubator-devlake/plugins/core/dal"
 
@@ -44,11 +46,20 @@ type SimpleBranch struct {
 	HeadBranch string `json:"head_branch" gorm:"type:varchar(255)"`
 }
 
-func ConvertTasks(taskCtx core.SubTaskContext) errors.Error {
+func ConvertTasks(taskCtx core.SubTaskContext) (err errors.Error) {
 	db := taskCtx.GetDal()
 	data := taskCtx.GetData().(*GithubTaskData)
 	repoId := data.Repo.GithubId
 
+	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")
+		}
+	}
+
 	job := &githubModels.GithubJob{}
 	cursor, err := db.Cursor(
 		dal.From(job),
@@ -88,10 +99,13 @@ func ConvertTasks(taskCtx core.SubTaskContext) errors.Error {
 			domainjob := &devops.CICDTask{
 				DomainEntity: domainlayer.DomainEntity{Id: fmt.Sprintf("%s:%s:%d:%d", "github", "GithubJob", data.Options.ConnectionId, line.ID)},
 				Name:         line.Name,
-				Type:         line.Type,
 				StartedDate:  *line.StartedAt,
 				FinishedDate: line.CompletedAt,
 			}
+
+			if deployFlag := deployTagRegexp.FindString(line.Name); deployFlag != "" {
+				domainjob.Type = devops.DEPLOYMENT
+			}
 			if len(tmp) > 0 {
 				domainjob.PipelineId = fmt.Sprintf("%s:%s:%d:%d", "github", "GithubRun", data.Options.ConnectionId, line.RunID)
 			}
diff --git a/plugins/github/tasks/task_data.go b/plugins/github/tasks/task_data.go
index 397ceb4c..60350d1c 100644
--- a/plugins/github/tasks/task_data.go
+++ b/plugins/github/tasks/task_data.go
@@ -18,9 +18,10 @@ limitations under the License.
 package tasks
 
 import (
-	"github.com/apache/incubator-devlake/errors"
 	"time"
 
+	"github.com/apache/incubator-devlake/errors"
+
 	"github.com/apache/incubator-devlake/plugins/github/models"
 	"github.com/apache/incubator-devlake/plugins/helper"
 )
@@ -81,6 +82,9 @@ func DecodeAndValidateTaskOptions(options map[string]interface{}) (*GithubOption
 	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 {