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 3b97c3ed feat: enrich tasks env for dora (#3020)
3b97c3ed is described below

commit 3b97c3edf75f1a2f9bd64c3703565a692aa893ee
Author: abeizn <[email protected]>
AuthorDate: Tue Sep 13 22:20:30 2022 +0800

    feat: enrich tasks env for dora (#3020)
    
    * feat: enrich tasks env for dora
    Co-authored-by: Warren Chen <[email protected]>
---
 plugins/dora/dora.go                         |  15 +++-
 plugins/dora/impl/impl.go                    |   3 +
 plugins/dora/tasks/cicd_task_env_enricher.go | 106 +++++++++++++++++++++++++++
 plugins/dora/tasks/task_data.go              |  12 ++-
 4 files changed, 129 insertions(+), 7 deletions(-)

diff --git a/plugins/dora/dora.go b/plugins/dora/dora.go
index fb1f8b0c..8b8b0fe8 100644
--- a/plugins/dora/dora.go
+++ b/plugins/dora/dora.go
@@ -30,13 +30,20 @@ var PluginEntry impl.Dora //nolint
 func main() {
        cmd := &cobra.Command{Use: "dora"}
 
-       // TODO add your cmd flag if necessary
-       // yourFlag := cmd.Flags().IntP("yourFlag", "y", 8, "TODO add 
description here")
-       // _ = cmd.MarkFlagRequired("yourFlag")
+       repoId := cmd.Flags().StringP("repoId", "r", "", "repo id")
+       _ = cmd.MarkFlagRequired("repoId")
+
+       // environment := cmd.Flags().StringP("environment", "e", "", 
"environment")
+       // _ = cmd.MarkFlagRequired("environment")
+
+       // environmentRegex := cmd.Flags().StringP("environmentRegex", "z", "", 
"environmentRegex")
+       // _ = cmd.MarkFlagRequired("environmentRegex")
 
        cmd.Run = func(cmd *cobra.Command, args []string) {
                runner.DirectRun(cmd, args, PluginEntry, map[string]interface{}{
-                       // TODO add more custom params here
+                       "repoId": *repoId,
+                       // "environment":      *environment,
+                       // "environmentRegex": *environmentRegex,
                })
        }
        runner.RunCmd(cmd)
diff --git a/plugins/dora/impl/impl.go b/plugins/dora/impl/impl.go
index bef74d6f..5432e3b8 100644
--- a/plugins/dora/impl/impl.go
+++ b/plugins/dora/impl/impl.go
@@ -19,6 +19,7 @@ package impl
 
 import (
        "fmt"
+
        "github.com/apache/incubator-devlake/errors"
        "github.com/apache/incubator-devlake/migration"
        "github.com/apache/incubator-devlake/plugins/core"
@@ -58,6 +59,8 @@ func (plugin Dora) Init(config *viper.Viper, logger 
core.Logger, db *gorm.DB) er
 func (plugin Dora) SubTaskMetas() []core.SubTaskMeta {
        // TODO add your sub task here
        return []core.SubTaskMeta{
+               tasks.EnrichTaskEnvMeta,
+               //tasks.ConvertChangeLeadTimeMeta,
                tasks.ConnectIssueDeployMeta,
        }
 }
diff --git a/plugins/dora/tasks/cicd_task_env_enricher.go 
b/plugins/dora/tasks/cicd_task_env_enricher.go
new file mode 100644
index 00000000..a418f299
--- /dev/null
+++ b/plugins/dora/tasks/cicd_task_env_enricher.go
@@ -0,0 +1,106 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package tasks
+
+import (
+       "fmt"
+       "reflect"
+       "regexp"
+       "runtime/debug"
+
+       "github.com/apache/incubator-devlake/models/domainlayer/devops"
+       "github.com/apache/incubator-devlake/plugins/core"
+       "github.com/apache/incubator-devlake/plugins/core/dal"
+       "github.com/apache/incubator-devlake/plugins/helper"
+)
+
+var EnrichTaskEnvMeta = core.SubTaskMeta{
+       Name:             "EnrichTaskEnv",
+       EntryPoint:       EnrichTasksEnv,
+       EnabledByDefault: true,
+       Description:      "calculate deployment frequency",
+       DomainTypes:      []string{core.DOMAIN_TYPE_CICD},
+}
+
+func EnrichTasksEnv(taskCtx core.SubTaskContext) (err error) {
+       db := taskCtx.GetDal()
+       data := taskCtx.GetData().(*DoraTaskData)
+       repoId := data.Options.RepoId
+
+       var taskNameReg *regexp.Regexp
+       taskNamePattern := data.Options.EnvironmentRegex
+       if len(taskNamePattern) > 0 {
+               taskNameReg, err = regexp.Compile(taskNamePattern)
+               if err != nil {
+                       return fmt.Errorf("regexp Compile taskNameReg 
failed:[%s] stack:[%s]", err.Error(), debug.Stack())
+               }
+       } else {
+               taskNamePattern = "deploy" // default
+               taskNameReg, err = regexp.Compile(taskNamePattern)
+               if err != nil {
+                       return fmt.Errorf("regexp Compile taskNameReg 
failed:[%s] stack:[%s]", err.Error(), debug.Stack())
+               }
+       }
+
+       cursor, err := db.Cursor(
+               dal.From(&devops.CICDTask{}),
+               dal.Join("left join cicd_pipeline_repos cpr on cpr.repo=? and 
pipeline_id = cpr.id ", repoId),
+               dal.Where("status=?", devops.DONE))
+       if err != nil {
+               return err
+       }
+       defer cursor.Close()
+
+       converter, err := helper.NewDataConverter(helper.DataConverterArgs{
+               RawDataSubTaskArgs: helper.RawDataSubTaskArgs{
+                       Ctx:    taskCtx,
+                       Params: DoraApiParams{
+                               // TODO
+                       },
+                       Table: "cicd_tasks",
+               },
+               InputRowType: reflect.TypeOf(devops.CICDTask{}),
+               Input:        cursor,
+               Convert: func(inputRow interface{}) ([]interface{}, error) {
+                       cicdTask := inputRow.(*devops.CICDTask)
+                       results := make([]interface{}, 0, 1)
+                       if deployTask := taskNameReg.FindString(cicdTask.Name); 
deployTask == "" {
+                               return nil, nil
+                       }
+                       cicdPipelineFilter := &devops.CICDTask{
+                               DomainEntity: cicdTask.DomainEntity,
+                               PipelineId:   cicdTask.PipelineId,
+                               Name:         cicdTask.Name,
+                               Result:       cicdTask.Result,
+                               Status:       cicdTask.Status,
+                               Type:         "DEPLOY",
+                               DurationSec:  cicdTask.DurationSec,
+                               StartedDate:  cicdTask.StartedDate,
+                               FinishedDate: cicdTask.FinishedDate,
+                               Environment:  data.Options.Environment,
+                       }
+                       results = append(results, cicdPipelineFilter)
+                       return results, nil
+               },
+       })
+       if err != nil {
+               return err
+       }
+
+       return converter.Execute()
+}
diff --git a/plugins/dora/tasks/task_data.go b/plugins/dora/tasks/task_data.go
index ebae0c34..60d64aa7 100644
--- a/plugins/dora/tasks/task_data.go
+++ b/plugins/dora/tasks/task_data.go
@@ -24,10 +24,16 @@ import (
 type DoraApiParams struct {
 }
 
+type TransformationRules struct {
+       Environment      string `mapstructure:"environment" json:"environment"`
+       EnvironmentRegex string `mapstructure:"environmentRegex" 
json:"environmentRegex"`
+}
+
 type DoraOptions struct {
-       Tasks  []string `json:"tasks,omitempty"`
-       Since  string
-       RepoId string
+       Tasks               []string `json:"tasks,omitempty"`
+       Since               string
+       RepoId              string `json:"repoId"`
+       TransformationRules `mapstructure:"transformationRules" 
json:"transformationRules"`
 }
 
 type DoraTaskData struct {

Reply via email to