This is an automated email from the ASF dual-hosted git repository.

zhangliang2022 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 e750403b4 Refactor Chage Lead Time - Step 3: convert bitbucket 
deployment to domain layer (#4894)
e750403b4 is described below

commit e750403b40c434b40726765784ab8ab25e2af719
Author: Klesh Wong <[email protected]>
AuthorDate: Wed Apr 12 18:58:16 2023 +0800

    Refactor Chage Lead Time - Step 3: convert bitbucket deployment to domain 
layer (#4894)
    
    * fix: GetStatus returned FAILURE when it should be DONE
    
    * feat: bitbucket deployment convertor
    
    * chore: add todo to rename RegexEnricher
    
    * fix: e2e
---
 .../models/domainlayer/devops/cicd_deployment.go   |  46 ++++++++
 .../models/domainlayer/devops/cicd_pipeline.go     |   5 +-
 .../20230411_add_cicd_deployment_commits.go        |  45 ++++++++
 .../migrationscripts/archived/cicd_deployment.go   |  44 ++++++++
 backend/core/models/migrationscripts/register.go   |   1 +
 .../helpers/pluginhelper/api/enrich_with_regex.go  |   1 +
 .../bamboo/e2e/snapshot_tables/cicd_pipelines.csv  |  18 ++--
 .../bamboo/e2e/snapshot_tables/cicd_tasks.csv      |  18 ++--
 backend/plugins/bitbucket/impl/impl.go             |   1 +
 .../bitbucket/tasks/deployment_convertor.go        | 117 +++++++++++++++++++++
 10 files changed, 276 insertions(+), 20 deletions(-)

diff --git a/backend/core/models/domainlayer/devops/cicd_deployment.go 
b/backend/core/models/domainlayer/devops/cicd_deployment.go
new file mode 100644
index 000000000..03f5dc898
--- /dev/null
+++ b/backend/core/models/domainlayer/devops/cicd_deployment.go
@@ -0,0 +1,46 @@
+/*
+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 devops
+
+import (
+       "time"
+
+       "github.com/apache/incubator-devlake/core/models/domainlayer"
+)
+
+type CicdDeploymentCommit struct {
+       domainlayer.DomainEntity
+       CicdScopeId                   string `gorm:"index;type:varchar(255)"`
+       CicdPipelineId                string `gorm:"type:varchar(255)"` // if 
it is converted from a cicd_pipeline_commit
+       Name                          string `gorm:"type:varchar(255)"`
+       Result                        string `gorm:"type:varchar(100)"`
+       Status                        string `gorm:"type:varchar(100)"`
+       Environment                   string `gorm:"type:varchar(255)"`
+       CreatedDate                   time.Time
+       StartedDate                   *time.Time
+       DurationSec                   *uint64
+       CommitSha                     string 
`gorm:"primaryKey;type:varchar(255)"`
+       RefName                       string `gorm:"type:varchar(255)"` // to 
delete?
+       RepoId                        string `gorm:"type:varchar(255)"`
+       RepoUrl                       string `gorm:"index;not null"`
+       PrevSuccessDeploymentCommitId string `gorm:"type:varchar(255)"`
+}
+
+func (CicdDeploymentCommit) TableName() string {
+       return "cicd_deployment_commits"
+}
diff --git a/backend/core/models/domainlayer/devops/cicd_pipeline.go 
b/backend/core/models/domainlayer/devops/cicd_pipeline.go
index ce603911c..167d62c67 100644
--- a/backend/core/models/domainlayer/devops/cicd_pipeline.go
+++ b/backend/core/models/domainlayer/devops/cicd_pipeline.go
@@ -18,8 +18,9 @@ limitations under the License.
 package devops
 
 import (
-       "github.com/apache/incubator-devlake/core/models/domainlayer"
        "time"
+
+       "github.com/apache/incubator-devlake/core/models/domainlayer"
 )
 
 type CICDPipeline struct {
@@ -101,7 +102,7 @@ func GetStatus(rule *StatusRule, input interface{}) string {
        }
        for _, done := range rule.Done {
                if done == input {
-                       return FAILURE
+                       return DONE
                }
        }
        for _, manual := range rule.Manual {
diff --git 
a/backend/core/models/migrationscripts/20230411_add_cicd_deployment_commits.go 
b/backend/core/models/migrationscripts/20230411_add_cicd_deployment_commits.go
new file mode 100644
index 000000000..92eb82196
--- /dev/null
+++ 
b/backend/core/models/migrationscripts/20230411_add_cicd_deployment_commits.go
@@ -0,0 +1,45 @@
+/*
+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 migrationscripts
+
+import (
+       "github.com/apache/incubator-devlake/core/context"
+       "github.com/apache/incubator-devlake/core/errors"
+       
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
+       "github.com/apache/incubator-devlake/core/plugin"
+       "github.com/apache/incubator-devlake/helpers/migrationhelper"
+)
+
+var _ plugin.MigrationScript = (*addCicdDeploymentCommits)(nil)
+
+type addCicdDeploymentCommits struct{}
+
+func (*addCicdDeploymentCommits) Up(basicRes context.BasicRes) errors.Error {
+       return migrationhelper.AutoMigrateTables(
+               basicRes,
+               &archived.CicdDeploymentCommit{},
+       )
+}
+
+func (*addCicdDeploymentCommits) Version() uint64 {
+       return 20230411150701
+}
+
+func (*addCicdDeploymentCommits) Name() string {
+       return "Rename cicd_piopeline_commits repo to repo_url"
+}
diff --git a/backend/core/models/migrationscripts/archived/cicd_deployment.go 
b/backend/core/models/migrationscripts/archived/cicd_deployment.go
new file mode 100644
index 000000000..53f0c888b
--- /dev/null
+++ b/backend/core/models/migrationscripts/archived/cicd_deployment.go
@@ -0,0 +1,44 @@
+/*
+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 archived
+
+import (
+       "time"
+)
+
+type CicdDeploymentCommit struct {
+       DomainEntity
+       CicdScopeId                   string `gorm:"index;type:varchar(255)"`
+       CicdPipelineId                string `gorm:"type:varchar(255)"` // if 
it is converted from a cicd_pipeline_commit
+       Name                          string `gorm:"type:varchar(255)"`
+       Result                        string `gorm:"type:varchar(100)"`
+       Status                        string `gorm:"type:varchar(100)"`
+       Environment                   string `gorm:"type:varchar(255)"`
+       CreatedDate                   time.Time
+       StartedDate                   *time.Time
+       DurationSec                   *uint64
+       CommitSha                     string 
`gorm:"primaryKey;type:varchar(255)"`
+       RefName                       string `gorm:"type:varchar(255)"`
+       RepoId                        string `gorm:"type:varchar(255)"`
+       RepoUrl                       string `gorm:"index;not null"`
+       PrevSuccessDeploymentCommitId string `gorm:"type:varchar(255)"`
+}
+
+func (CicdDeploymentCommit) TableName() string {
+       return "cicd_deployment_commits"
+}
diff --git a/backend/core/models/migrationscripts/register.go 
b/backend/core/models/migrationscripts/register.go
index 9e858bff0..e5ec8ed93 100644
--- a/backend/core/models/migrationscripts/register.go
+++ b/backend/core/models/migrationscripts/register.go
@@ -77,5 +77,6 @@ func All() []plugin.MigrationScript {
                new(addHostNamespaceRepoName),
                new(renameCollectorTapStateTable),
                new(renameCicdPipelineRepoToRepoUrl),
+               new(addCicdDeploymentCommits),
        }
 }
diff --git a/backend/helpers/pluginhelper/api/enrich_with_regex.go 
b/backend/helpers/pluginhelper/api/enrich_with_regex.go
index 2df1dfcec..1c25c3913 100644
--- a/backend/helpers/pluginhelper/api/enrich_with_regex.go
+++ b/backend/helpers/pluginhelper/api/enrich_with_regex.go
@@ -26,6 +26,7 @@ import (
 )
 
 // RegexEnricher process value with regex pattern
+// TODO: remove Enricher from naming since it is more like a util function
 type RegexEnricher struct {
        // This field will store compiled regular expression for every pattern
        regexpMap map[string]*regexp.Regexp
diff --git a/backend/plugins/bamboo/e2e/snapshot_tables/cicd_pipelines.csv 
b/backend/plugins/bamboo/e2e/snapshot_tables/cicd_pipelines.csv
index 4ff92f902..a7b20a513 100644
--- a/backend/plugins/bamboo/e2e/snapshot_tables/cicd_pipelines.csv
+++ b/backend/plugins/bamboo/e2e/snapshot_tables/cicd_pipelines.csv
@@ -1,10 +1,10 @@
 
id,name,result,status,type,duration_sec,environment,created_date,finished_date,cicd_scope_id
-bamboo:BambooPlanBuild:3:TEST1-TEST1-22,test_plan,SUCCESS,FAILURE,,0,,2023-02-22T08:31:51.532+00:00,2023-02-22T08:31:51.624+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooPlanBuild:3:TEST1-TEST1-23,test_plan,SUCCESS,FAILURE,,0,,2023-02-22T08:31:54.760+00:00,2023-02-22T08:31:54.811+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooPlanBuild:3:TEST1-TEST2-1,test2,FAILURE,FAILURE,,0,,2023-02-22T08:54:40.831+00:00,2023-02-22T08:54:40.884+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooPlanBuild:3:TEST1-TEST2-2,test2,FAILURE,FAILURE,,0,,2023-02-22T08:57:02.868+00:00,2023-02-22T08:57:02.903+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooPlanBuild:3:TEST1-TEST2-3,test2,FAILURE,FAILURE,,0,,2023-02-22T08:57:06.713+00:00,2023-02-22T08:57:06.770+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooPlanBuild:3:TEST1-TEST3-1,test3,FAILURE,FAILURE,,0,,2023-02-22T08:55:19.422+00:00,2023-02-22T08:55:19.500+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooPlanBuild:3:TEST1-TEST3-2,test3,FAILURE,FAILURE,,0,,2023-02-22T08:55:21.888+00:00,2023-02-22T08:55:21.930+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooPlanBuild:3:TEST1-TEST4-1,test4,FAILURE,FAILURE,,0,,2023-02-22T08:56:25.911+00:00,2023-02-22T08:56:25.972+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooPlanBuild:3:TEST1-TEST4-2,test4,FAILURE,FAILURE,,0,,2023-02-22T08:56:27.881+00:00,2023-02-22T08:56:27.917+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooPlanBuild:3:TEST1-TEST1-22,test_plan,SUCCESS,DONE,,0,,2023-02-22T08:31:51.532+00:00,2023-02-22T08:31:51.624+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooPlanBuild:3:TEST1-TEST1-23,test_plan,SUCCESS,DONE,,0,,2023-02-22T08:31:54.760+00:00,2023-02-22T08:31:54.811+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooPlanBuild:3:TEST1-TEST2-1,test2,FAILURE,DONE,,0,,2023-02-22T08:54:40.831+00:00,2023-02-22T08:54:40.884+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooPlanBuild:3:TEST1-TEST2-2,test2,FAILURE,DONE,,0,,2023-02-22T08:57:02.868+00:00,2023-02-22T08:57:02.903+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooPlanBuild:3:TEST1-TEST2-3,test2,FAILURE,DONE,,0,,2023-02-22T08:57:06.713+00:00,2023-02-22T08:57:06.770+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooPlanBuild:3:TEST1-TEST3-1,test3,FAILURE,DONE,,0,,2023-02-22T08:55:19.422+00:00,2023-02-22T08:55:19.500+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooPlanBuild:3:TEST1-TEST3-2,test3,FAILURE,DONE,,0,,2023-02-22T08:55:21.888+00:00,2023-02-22T08:55:21.930+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooPlanBuild:3:TEST1-TEST4-1,test4,FAILURE,DONE,,0,,2023-02-22T08:56:25.911+00:00,2023-02-22T08:56:25.972+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooPlanBuild:3:TEST1-TEST4-2,test4,FAILURE,DONE,,0,,2023-02-22T08:56:27.881+00:00,2023-02-22T08:56:27.917+00:00,bamboo:BambooProject:3:TEST1
diff --git a/backend/plugins/bamboo/e2e/snapshot_tables/cicd_tasks.csv 
b/backend/plugins/bamboo/e2e/snapshot_tables/cicd_tasks.csv
index 212768518..60f5abb74 100644
--- a/backend/plugins/bamboo/e2e/snapshot_tables/cicd_tasks.csv
+++ b/backend/plugins/bamboo/e2e/snapshot_tables/cicd_tasks.csv
@@ -1,10 +1,10 @@
 
id,name,pipeline_id,result,status,type,environment,duration_sec,started_date,finished_date,cicd_scope_id
-bamboo:BambooJobBuild:3:TEST1-TEST1-JOB1-22,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST1-22,SUCCESS,FAILURE,,,0,2023-02-22T08:31:51.580+00:00,2023-02-22T08:31:51.590+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooJobBuild:3:TEST1-TEST1-JOB1-23,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST1-23,SUCCESS,FAILURE,,,0,2023-02-22T08:31:54.768+00:00,2023-02-22T08:31:54.778+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooJobBuild:3:TEST1-TEST2-JOB1-2,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST2-2,FAILURE,FAILURE,,,0,2023-02-22T08:57:02.876+00:00,2023-02-22T08:57:02.879+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooJobBuild:3:TEST1-TEST2-JOB1-3,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST2-3,FAILURE,FAILURE,,,0,2023-02-22T08:57:06.722+00:00,2023-02-22T08:57:06.725+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooJobBuild:3:TEST1-TEST3-JOB1-2,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST3-2,FAILURE,FAILURE,,,0,2023-02-22T08:55:21.897+00:00,2023-02-22T08:55:21.901+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooJobBuild:3:TEST1-TEST3-JOB1-3,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST3-3,FAILURE,FAILURE,,,0,2023-02-22T08:55:25.123+00:00,2023-02-22T08:55:25.126+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooJobBuild:3:TEST1-TEST4-JOB1-1,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST4-1,FAILURE,FAILURE,,,0,2023-02-22T08:56:25.943+00:00,2023-02-22T08:56:25.947+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooJobBuild:3:TEST1-TEST4-JOB1-2,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST4-2,FAILURE,FAILURE,,,0,2023-02-22T08:56:27.888+00:00,2023-02-22T08:56:27.891+00:00,bamboo:BambooProject:3:TEST1
-bamboo:BambooJobBuild:3:TEST1-TEST4-JOB1-3,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST4-3,FAILURE,FAILURE,,,0,2023-02-22T08:57:01.230+00:00,2023-02-22T08:57:01.234+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooJobBuild:3:TEST1-TEST1-JOB1-22,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST1-22,SUCCESS,DONE,,,0,2023-02-22T08:31:51.580+00:00,2023-02-22T08:31:51.590+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooJobBuild:3:TEST1-TEST1-JOB1-23,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST1-23,SUCCESS,DONE,,,0,2023-02-22T08:31:54.768+00:00,2023-02-22T08:31:54.778+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooJobBuild:3:TEST1-TEST2-JOB1-2,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST2-2,FAILURE,DONE,,,0,2023-02-22T08:57:02.876+00:00,2023-02-22T08:57:02.879+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooJobBuild:3:TEST1-TEST2-JOB1-3,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST2-3,FAILURE,DONE,,,0,2023-02-22T08:57:06.722+00:00,2023-02-22T08:57:06.725+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooJobBuild:3:TEST1-TEST3-JOB1-2,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST3-2,FAILURE,DONE,,,0,2023-02-22T08:55:21.897+00:00,2023-02-22T08:55:21.901+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooJobBuild:3:TEST1-TEST3-JOB1-3,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST3-3,FAILURE,DONE,,,0,2023-02-22T08:55:25.123+00:00,2023-02-22T08:55:25.126+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooJobBuild:3:TEST1-TEST4-JOB1-1,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST4-1,FAILURE,DONE,,,0,2023-02-22T08:56:25.943+00:00,2023-02-22T08:56:25.947+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooJobBuild:3:TEST1-TEST4-JOB1-2,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST4-2,FAILURE,DONE,,,0,2023-02-22T08:56:27.888+00:00,2023-02-22T08:56:27.891+00:00,bamboo:BambooProject:3:TEST1
+bamboo:BambooJobBuild:3:TEST1-TEST4-JOB1-3,Default 
Job,bamboo:BambooPlanBuild:3:TEST1-TEST4-3,FAILURE,DONE,,,0,2023-02-22T08:57:01.230+00:00,2023-02-22T08:57:01.234+00:00,bamboo:BambooProject:3:TEST1
diff --git a/backend/plugins/bitbucket/impl/impl.go 
b/backend/plugins/bitbucket/impl/impl.go
index bf121fad6..69372603f 100644
--- a/backend/plugins/bitbucket/impl/impl.go
+++ b/backend/plugins/bitbucket/impl/impl.go
@@ -120,6 +120,7 @@ func (p Bitbucket) SubTaskMetas() []plugin.SubTaskMeta {
                tasks.ConvertIssueCommentsMeta,
                tasks.ConvertPipelineMeta,
                tasks.ConvertPipelineStepMeta,
+               tasks.ConvertiDeploymentMeta,
        }
 }
 
diff --git a/backend/plugins/bitbucket/tasks/deployment_convertor.go 
b/backend/plugins/bitbucket/tasks/deployment_convertor.go
new file mode 100644
index 000000000..d4274ddc5
--- /dev/null
+++ b/backend/plugins/bitbucket/tasks/deployment_convertor.go
@@ -0,0 +1,117 @@
+/*
+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 (
+       "reflect"
+
+       "github.com/apache/incubator-devlake/core/dal"
+       "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/models/domainlayer"
+       "github.com/apache/incubator-devlake/core/models/domainlayer/devops"
+       "github.com/apache/incubator-devlake/core/models/domainlayer/didgen"
+       plugin "github.com/apache/incubator-devlake/core/plugin"
+       "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+       "github.com/apache/incubator-devlake/plugins/bitbucket/models"
+)
+
+var ConvertiDeploymentMeta = plugin.SubTaskMeta{
+       Name:             "convertDeployments",
+       EntryPoint:       ConvertDeployments,
+       EnabledByDefault: true,
+       Description:      "Convert tool layer table bitbucket_deployment into 
domain layer tables",
+       DomainTypes:      []string{plugin.DOMAIN_TYPE_CICD},
+}
+
+type bitbucketDeploymentWithRefName struct {
+       models.BitbucketDeployment
+       RefName string
+}
+
+func ConvertDeployments(taskCtx plugin.SubTaskContext) errors.Error {
+       rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, 
RAW_PIPELINE_TABLE)
+       db := taskCtx.GetDal()
+
+       repo := &models.BitbucketRepo{}
+       err := db.First(repo, dal.Where("connection_id = ? AND bitbucket_id = 
?", data.Options.ConnectionId, data.Options.FullName))
+       if err != nil {
+               return err
+       }
+       repoId := 
didgen.NewDomainIdGenerator(&models.BitbucketRepo{}).Generate(data.Options.ConnectionId,
 repo.BitbucketId)
+
+       cursor, err := db.Cursor(
+               dal.Select("d.*, p.ref_name"),
+               dal.From("_tool_bitbucket_deployments d"),
+               dal.Join("LEFT JOIN _tool_bitbucket_pipelines p ON 
(p.connection_id = d.connection_id AND p.bitbucket_id = d.pipeline_id)"),
+               dal.Where("d.connection_id = ? AND p.repo_id = ? ", 
data.Options.ConnectionId, data.Options.FullName),
+       )
+       if err != nil {
+               return err
+       }
+       defer cursor.Close()
+
+       idGen := didgen.NewDomainIdGenerator(&models.BitbucketDeployment{})
+       pipelineIdGen := 
didgen.NewDomainIdGenerator(&models.BitbucketPipeline{})
+
+       converter, err := api.NewDataConverter(api.DataConverterArgs{
+               InputRowType:       
reflect.TypeOf(bitbucketDeploymentWithRefName{}),
+               Input:              cursor,
+               RawDataSubTaskArgs: *rawDataSubTaskArgs,
+               Convert: func(inputRow interface{}) ([]interface{}, 
errors.Error) {
+                       bitbucketDeployment := 
inputRow.(*bitbucketDeploymentWithRefName)
+
+                       var duration *uint64
+                       if bitbucketDeployment.CompletedOn != nil {
+                               d := 
uint64(bitbucketDeployment.CompletedOn.Sub(*bitbucketDeployment.StartedOn).Seconds())
+                               duration = &d
+                       }
+                       domainDeployCommit := &devops.CicdDeploymentCommit{
+                               DomainEntity: domainlayer.DomainEntity{
+                                       Id: 
idGen.Generate(data.Options.ConnectionId, bitbucketDeployment.BitbucketId),
+                               },
+                               CicdScopeId:    repoId,
+                               CicdPipelineId: 
pipelineIdGen.Generate(data.Options.ConnectionId, 
bitbucketDeployment.PipelineId),
+                               Name:           bitbucketDeployment.Name,
+                               Result: devops.GetResult(&devops.ResultRule{
+                                       Failed:  []string{"UNDEPLOYED"},
+                                       Success: []string{"COMPLETED"},
+                                       Default: "",
+                               }, bitbucketDeployment.Status),
+                               Status: devops.GetStatus(&devops.StatusRule{
+                                       Done:    []string{"COMPLETED", 
"UNDEPLOYED"},
+                                       Default: devops.IN_PROGRESS,
+                               }, bitbucketDeployment.Status),
+                               Environment: bitbucketDeployment.Environment,
+                               CreatedDate: *bitbucketDeployment.CreatedOn,
+                               StartedDate: bitbucketDeployment.StartedOn,
+                               DurationSec: duration,
+                               CommitSha:   bitbucketDeployment.CommitSha,
+                               RefName:     bitbucketDeployment.RefName,
+                               RepoId:      repoId,
+                               RepoUrl:     repo.HTMLUrl,
+                       }
+                       return []interface{}{domainDeployCommit}, nil
+               },
+       })
+
+       if err != nil {
+               return err
+       }
+
+       return converter.Execute()
+}

Reply via email to