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

likyh 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 f6d6c188b feat(tapd): add custom field enricher (#4912)
f6d6c188b is described below

commit f6d6c188bf685c451bbe2223294e6536cc11405a
Author: Warren Chen <[email protected]>
AuthorDate: Thu Apr 13 14:15:14 2023 +0800

    feat(tapd): add custom field enricher (#4912)
---
 backend/plugins/tapd/impl/impl.go                  |  6 ++
 .../register.go => bug_custom_field_value.go}      | 25 +++---
 ...ister.go => 20230412_add_custom_field_value.go} | 26 ++++---
 .../bug_custom_field_value.go}                     | 25 +++---
 .../story_custom_field_value.go}                   | 25 +++---
 .../task_custom_field_value.go}                    | 25 +++---
 .../tapd/models/migrationscripts/register.go       |  1 +
 .../register.go => story_custom_field_value.go}    | 25 +++---
 .../register.go => task_custom_field_value.go}     | 25 +++---
 .../tapd/tasks/bug_custom_field_enricher.go        | 87 +++++++++++++++++++++
 .../tapd/tasks/story_custom_field_enricher.go      | 89 ++++++++++++++++++++++
 .../tapd/tasks/task_custom_field_enricher.go       | 88 +++++++++++++++++++++
 12 files changed, 371 insertions(+), 76 deletions(-)

diff --git a/backend/plugins/tapd/impl/impl.go 
b/backend/plugins/tapd/impl/impl.go
index 5ead1ddc0..03f862067 100644
--- a/backend/plugins/tapd/impl/impl.go
+++ b/backend/plugins/tapd/impl/impl.go
@@ -83,6 +83,9 @@ func (p Tapd) GetTablesInfo() []dal.Tabler {
                &models.TapdWorklog{},
                &models.TapdWorkspace{},
                &models.TapdWorkspaceIteration{},
+               &models.TapdStoryCustomFieldValue{},
+               &models.TapdTaskCustomFieldValue{},
+               &models.TapdBugCustomFieldValue{},
        }
 }
 
@@ -151,6 +154,9 @@ func (p Tapd) SubTaskMetas() []plugin.SubTaskMeta {
                tasks.ConvertStoryLabelsMeta,
                tasks.ConvertTaskLabelsMeta,
                tasks.ConvertBugLabelsMeta,
+               tasks.EnrichStoryCustomFieldMeta,
+               tasks.EnrichBugCustomFieldMeta,
+               tasks.EnrichTaskCustomFieldMeta,
        }
 }
 
diff --git a/backend/plugins/tapd/models/migrationscripts/register.go 
b/backend/plugins/tapd/models/bug_custom_field_value.go
similarity index 55%
copy from backend/plugins/tapd/models/migrationscripts/register.go
copy to backend/plugins/tapd/models/bug_custom_field_value.go
index 232c48e33..4d83d39bf 100644
--- a/backend/plugins/tapd/models/migrationscripts/register.go
+++ b/backend/plugins/tapd/models/bug_custom_field_value.go
@@ -15,19 +15,22 @@ See the License for the specific language governing 
permissions and
 limitations under the License.
 */
 
-package migrationscripts
+package models
 
 import (
-       "github.com/apache/incubator-devlake/core/plugin"
+       "github.com/apache/incubator-devlake/core/models/common"
 )
 
-// All return all the migration scripts
-func All() []plugin.MigrationScript {
-       return []plugin.MigrationScript{
-               new(addInitTables),
-               new(encodeConnToken),
-               new(addTransformation),
-               new(deleteIssue),
-               new(modifyCustomFieldName),
-       }
+type TapdBugCustomFieldValue struct {
+       ConnectionId uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       WorkspaceId  uint64 `json:"workspace_id,string"`
+       BugId        uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       CustomField  string `gorm:"primaryKey;type:varchar(40) NOT NULL"` 
//TapdStoryCustomFields.custom_field
+       Name         string `json:"name" gorm:"type:varchar(255)"`        // 
TapdStoryCustomFields.name
+       CustomValue  string
+       common.NoPKModel
+}
+
+func (TapdBugCustomFieldValue) TableName() string {
+       return "_tool_tapd_bug_custom_field_value"
 }
diff --git a/backend/plugins/tapd/models/migrationscripts/register.go 
b/backend/plugins/tapd/models/migrationscripts/20230412_add_custom_field_value.go
similarity index 53%
copy from backend/plugins/tapd/models/migrationscripts/register.go
copy to 
backend/plugins/tapd/models/migrationscripts/20230412_add_custom_field_value.go
index 232c48e33..7b5e001bc 100644
--- a/backend/plugins/tapd/models/migrationscripts/register.go
+++ 
b/backend/plugins/tapd/models/migrationscripts/20230412_add_custom_field_value.go
@@ -18,16 +18,22 @@ limitations under the License.
 package migrationscripts
 
 import (
-       "github.com/apache/incubator-devlake/core/plugin"
+       "github.com/apache/incubator-devlake/core/context"
+       "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/helpers/migrationhelper"
+       
"github.com/apache/incubator-devlake/plugins/tapd/models/migrationscripts/archived"
 )
 
-// All return all the migration scripts
-func All() []plugin.MigrationScript {
-       return []plugin.MigrationScript{
-               new(addInitTables),
-               new(encodeConnToken),
-               new(addTransformation),
-               new(deleteIssue),
-               new(modifyCustomFieldName),
-       }
+type addCustomFieldValue struct{}
+
+func (*addCustomFieldValue) Up(basicRes context.BasicRes) errors.Error {
+       return migrationhelper.AutoMigrateTables(basicRes, 
&archived.TapdStoryCustomFieldValue{}, &archived.TapdBugCustomFieldValue{}, 
&archived.TapdTaskCustomFieldValue{})
+}
+
+func (*addCustomFieldValue) Version() uint64 {
+       return 20230412000015
+}
+
+func (*addCustomFieldValue) Name() string {
+       return "modify tapd custom field name"
 }
diff --git a/backend/plugins/tapd/models/migrationscripts/register.go 
b/backend/plugins/tapd/models/migrationscripts/archived/bug_custom_field_value.go
similarity index 54%
copy from backend/plugins/tapd/models/migrationscripts/register.go
copy to 
backend/plugins/tapd/models/migrationscripts/archived/bug_custom_field_value.go
index 232c48e33..58a8d9448 100644
--- a/backend/plugins/tapd/models/migrationscripts/register.go
+++ 
b/backend/plugins/tapd/models/migrationscripts/archived/bug_custom_field_value.go
@@ -15,19 +15,22 @@ See the License for the specific language governing 
permissions and
 limitations under the License.
 */
 
-package migrationscripts
+package archived
 
 import (
-       "github.com/apache/incubator-devlake/core/plugin"
+       
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
 )
 
-// All return all the migration scripts
-func All() []plugin.MigrationScript {
-       return []plugin.MigrationScript{
-               new(addInitTables),
-               new(encodeConnToken),
-               new(addTransformation),
-               new(deleteIssue),
-               new(modifyCustomFieldName),
-       }
+type TapdBugCustomFieldValue struct {
+       ConnectionId uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       WorkspaceId  uint64 `json:"workspace_id,string"`
+       BugId        uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       CustomField  string `gorm:"primaryKey;type:varchar(40) NOT NULL"` 
//TapdStoryCustomFields.custom_field
+       Name         string `json:"name" gorm:"type:varchar(255)"`        // 
TapdStoryCustomFields.name
+       CustomValue  string
+       archived.NoPKModel
+}
+
+func (TapdBugCustomFieldValue) TableName() string {
+       return "_tool_tapd_bug_custom_field_value"
 }
diff --git a/backend/plugins/tapd/models/migrationscripts/register.go 
b/backend/plugins/tapd/models/migrationscripts/archived/story_custom_field_value.go
similarity index 54%
copy from backend/plugins/tapd/models/migrationscripts/register.go
copy to 
backend/plugins/tapd/models/migrationscripts/archived/story_custom_field_value.go
index 232c48e33..14fe7a1e6 100644
--- a/backend/plugins/tapd/models/migrationscripts/register.go
+++ 
b/backend/plugins/tapd/models/migrationscripts/archived/story_custom_field_value.go
@@ -15,19 +15,22 @@ See the License for the specific language governing 
permissions and
 limitations under the License.
 */
 
-package migrationscripts
+package archived
 
 import (
-       "github.com/apache/incubator-devlake/core/plugin"
+       
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
 )
 
-// All return all the migration scripts
-func All() []plugin.MigrationScript {
-       return []plugin.MigrationScript{
-               new(addInitTables),
-               new(encodeConnToken),
-               new(addTransformation),
-               new(deleteIssue),
-               new(modifyCustomFieldName),
-       }
+type TapdStoryCustomFieldValue struct {
+       ConnectionId uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       WorkspaceId  uint64 `json:"workspace_id,string"`
+       StoryId      uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       CustomField  string `gorm:"primaryKey;type:varchar(40) NOT NULL"` 
//TapdStoryCustomFields.custom_field
+       Name         string `json:"name" gorm:"type:varchar(255)"`        // 
TapdStoryCustomFields.name
+       CustomValue  string
+       archived.NoPKModel
+}
+
+func (TapdStoryCustomFieldValue) TableName() string {
+       return "_tool_tapd_story_custom_field_value"
 }
diff --git a/backend/plugins/tapd/models/migrationscripts/register.go 
b/backend/plugins/tapd/models/migrationscripts/archived/task_custom_field_value.go
similarity index 54%
copy from backend/plugins/tapd/models/migrationscripts/register.go
copy to 
backend/plugins/tapd/models/migrationscripts/archived/task_custom_field_value.go
index 232c48e33..3d853738a 100644
--- a/backend/plugins/tapd/models/migrationscripts/register.go
+++ 
b/backend/plugins/tapd/models/migrationscripts/archived/task_custom_field_value.go
@@ -15,19 +15,22 @@ See the License for the specific language governing 
permissions and
 limitations under the License.
 */
 
-package migrationscripts
+package archived
 
 import (
-       "github.com/apache/incubator-devlake/core/plugin"
+       
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
 )
 
-// All return all the migration scripts
-func All() []plugin.MigrationScript {
-       return []plugin.MigrationScript{
-               new(addInitTables),
-               new(encodeConnToken),
-               new(addTransformation),
-               new(deleteIssue),
-               new(modifyCustomFieldName),
-       }
+type TapdTaskCustomFieldValue struct {
+       ConnectionId uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       WorkspaceId  uint64 `json:"workspace_id,string"`
+       TaskId       uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       CustomField  string `gorm:"primaryKey;type:varchar(40) NOT NULL"` 
//TapdStoryCustomFields.custom_field
+       Name         string `json:"name" gorm:"type:varchar(255)"`        // 
TapdStoryCustomFields.name
+       CustomValue  string
+       archived.NoPKModel
+}
+
+func (TapdTaskCustomFieldValue) TableName() string {
+       return "_tool_tapd_task_custom_field_value"
 }
diff --git a/backend/plugins/tapd/models/migrationscripts/register.go 
b/backend/plugins/tapd/models/migrationscripts/register.go
index 232c48e33..6af561d28 100644
--- a/backend/plugins/tapd/models/migrationscripts/register.go
+++ b/backend/plugins/tapd/models/migrationscripts/register.go
@@ -29,5 +29,6 @@ func All() []plugin.MigrationScript {
                new(addTransformation),
                new(deleteIssue),
                new(modifyCustomFieldName),
+               new(addCustomFieldValue),
        }
 }
diff --git a/backend/plugins/tapd/models/migrationscripts/register.go 
b/backend/plugins/tapd/models/story_custom_field_value.go
similarity index 55%
copy from backend/plugins/tapd/models/migrationscripts/register.go
copy to backend/plugins/tapd/models/story_custom_field_value.go
index 232c48e33..bb0e77ca7 100644
--- a/backend/plugins/tapd/models/migrationscripts/register.go
+++ b/backend/plugins/tapd/models/story_custom_field_value.go
@@ -15,19 +15,22 @@ See the License for the specific language governing 
permissions and
 limitations under the License.
 */
 
-package migrationscripts
+package models
 
 import (
-       "github.com/apache/incubator-devlake/core/plugin"
+       "github.com/apache/incubator-devlake/core/models/common"
 )
 
-// All return all the migration scripts
-func All() []plugin.MigrationScript {
-       return []plugin.MigrationScript{
-               new(addInitTables),
-               new(encodeConnToken),
-               new(addTransformation),
-               new(deleteIssue),
-               new(modifyCustomFieldName),
-       }
+type TapdStoryCustomFieldValue struct {
+       ConnectionId uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       WorkspaceId  uint64 `json:"workspace_id,string"`
+       StoryId      uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       CustomField  string `gorm:"primaryKey;type:varchar(40) NOT NULL"` 
//TapdStoryCustomFields.custom_field
+       Name         string `json:"name" gorm:"type:varchar(255)"`        // 
TapdStoryCustomFields.name
+       CustomValue  string
+       common.NoPKModel
+}
+
+func (TapdStoryCustomFieldValue) TableName() string {
+       return "_tool_tapd_story_custom_field_value"
 }
diff --git a/backend/plugins/tapd/models/migrationscripts/register.go 
b/backend/plugins/tapd/models/task_custom_field_value.go
similarity index 55%
copy from backend/plugins/tapd/models/migrationscripts/register.go
copy to backend/plugins/tapd/models/task_custom_field_value.go
index 232c48e33..2c6d15ab5 100644
--- a/backend/plugins/tapd/models/migrationscripts/register.go
+++ b/backend/plugins/tapd/models/task_custom_field_value.go
@@ -15,19 +15,22 @@ See the License for the specific language governing 
permissions and
 limitations under the License.
 */
 
-package migrationscripts
+package models
 
 import (
-       "github.com/apache/incubator-devlake/core/plugin"
+       "github.com/apache/incubator-devlake/core/models/common"
 )
 
-// All return all the migration scripts
-func All() []plugin.MigrationScript {
-       return []plugin.MigrationScript{
-               new(addInitTables),
-               new(encodeConnToken),
-               new(addTransformation),
-               new(deleteIssue),
-               new(modifyCustomFieldName),
-       }
+type TapdTaskCustomFieldValue struct {
+       ConnectionId uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       WorkspaceId  uint64 `json:"workspace_id,string"`
+       TaskId       uint64 `gorm:"primaryKey;type:BIGINT  NOT NULL"`
+       CustomField  string `gorm:"primaryKey;type:varchar(40) NOT NULL"` 
//TapdStoryCustomFields.custom_field
+       Name         string `json:"name" gorm:"type:varchar(255)"`        // 
TapdStoryCustomFields.name
+       CustomValue  string
+       common.NoPKModel
+}
+
+func (TapdTaskCustomFieldValue) TableName() string {
+       return "_tool_tapd_task_custom_field_value"
 }
diff --git a/backend/plugins/tapd/tasks/bug_custom_field_enricher.go 
b/backend/plugins/tapd/tasks/bug_custom_field_enricher.go
new file mode 100644
index 000000000..1ac86fa3d
--- /dev/null
+++ b/backend/plugins/tapd/tasks/bug_custom_field_enricher.go
@@ -0,0 +1,87 @@
+/*
+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"
+       "github.com/apache/incubator-devlake/core/dal"
+       "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/plugin"
+       helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+       "github.com/apache/incubator-devlake/plugins/tapd/models"
+       "reflect"
+)
+
+var _ plugin.SubTaskEntryPoint = EnrichBugCustomFields
+
+var EnrichBugCustomFieldMeta = plugin.SubTaskMeta{
+       Name:       "enrichBugCustomFields",
+       EntryPoint: EnrichBugCustomFields,
+       // TODO false or true?
+       EnabledByDefault: true,
+       Description:      "Enrich bug custom fields",
+       DomainTypes:      []string{plugin.DOMAIN_TYPE_TICKET},
+}
+
+func EnrichBugCustomFields(taskCtx plugin.SubTaskContext) errors.Error {
+       rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, 
RAW_STORY_TABLE)
+       db := taskCtx.GetDal()
+       clauses := []dal.Clause{
+               dal.From(&models.TapdBugCustomFields{}),
+               dal.Where("connection_id = ? and workspace_id = ?", 
data.Options.ConnectionId, data.Options.WorkspaceId),
+               dal.Orderby("name ASC"),
+       }
+
+       cursor, err := db.Cursor(clauses...)
+       if err != nil {
+               return err
+       }
+
+       defer cursor.Close()
+
+       converter, err := helper.NewDataConverter(helper.DataConverterArgs{
+               RawDataSubTaskArgs: *rawDataSubTaskArgs,
+               InputRowType:       
reflect.TypeOf(models.TapdBugCustomFields{}),
+               Input:              cursor,
+               Convert: func(inputRow interface{}) ([]interface{}, 
errors.Error) {
+                       customField := inputRow.(*models.TapdBugCustomFields)
+
+                       bugCustomFieldValues := 
make([]*models.TapdBugCustomFieldValue, 0)
+
+                       clausesForCustomFieldValue := []dal.Clause{
+                               dal.Select(fmt.Sprintf(`connection_id, 
workspace_id, id as bug_id, %s as custom_value, '%s' as custom_field, '%s' as 
name`,
+                                       customField.CustomField, 
customField.CustomField, customField.Name)),
+                               dal.From(&models.TapdBug{}),
+                               dal.Where("connection_id = ? and workspace_id = 
?", data.Options.ConnectionId, data.Options.WorkspaceId),
+                       }
+
+                       err = db.All(&bugCustomFieldValues, 
clausesForCustomFieldValue...)
+                       results := make([]interface{}, 0, 
len(bugCustomFieldValues))
+                       for _, bugCustomFieldValue := range 
bugCustomFieldValues {
+                               results = append(results, bugCustomFieldValue)
+                       }
+                       return results, nil
+               },
+       })
+
+       if err != nil {
+               return err
+       }
+
+       return converter.Execute()
+}
diff --git a/backend/plugins/tapd/tasks/story_custom_field_enricher.go 
b/backend/plugins/tapd/tasks/story_custom_field_enricher.go
new file mode 100644
index 000000000..49224ee4c
--- /dev/null
+++ b/backend/plugins/tapd/tasks/story_custom_field_enricher.go
@@ -0,0 +1,89 @@
+/*
+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"
+       "github.com/apache/incubator-devlake/core/dal"
+       "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/plugin"
+       helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+       "github.com/apache/incubator-devlake/plugins/tapd/models"
+       "reflect"
+)
+
+var _ plugin.SubTaskEntryPoint = EnrichStoryCustomFields
+
+var EnrichStoryCustomFieldMeta = plugin.SubTaskMeta{
+       Name:       "enrichStoryCustomFields",
+       EntryPoint: EnrichStoryCustomFields,
+       // TODO false or true?
+       EnabledByDefault: true,
+       Description:      "Enrich story custom fields",
+       DomainTypes:      []string{plugin.DOMAIN_TYPE_TICKET},
+}
+
+func EnrichStoryCustomFields(taskCtx plugin.SubTaskContext) errors.Error {
+       rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, 
RAW_STORY_TABLE)
+       db := taskCtx.GetDal()
+
+       clauses := []dal.Clause{
+               dal.From(&models.TapdStoryCustomFields{}),
+               dal.Where("connection_id = ? and workspace_id = ?", 
data.Options.ConnectionId, data.Options.WorkspaceId),
+               dal.Orderby("name ASC"),
+       }
+
+       cursor, err := db.Cursor(clauses...)
+       if err != nil {
+               return err
+       }
+
+       defer cursor.Close()
+
+       converter, err := helper.NewDataConverter(helper.DataConverterArgs{
+               RawDataSubTaskArgs: *rawDataSubTaskArgs,
+               InputRowType:       
reflect.TypeOf(models.TapdStoryCustomFields{}),
+               Input:              cursor,
+               Convert: func(inputRow interface{}) ([]interface{}, 
errors.Error) {
+                       customField := inputRow.(*models.TapdStoryCustomFields)
+
+                       storyCustomFieldValues := 
make([]*models.TapdStoryCustomFieldValue, 0)
+
+                       clausesForCustomFieldValue := []dal.Clause{
+                               dal.Select(fmt.Sprintf(`connection_id, 
workspace_id, id as story_id, %s as custom_value, '%s' as custom_field, '%s' as 
name`,
+                                       customField.CustomField, 
customField.CustomField, customField.Name)),
+                               dal.From(&models.TapdStory{}),
+                               dal.Where("connection_id = ? and workspace_id = 
?",
+                                       data.Options.ConnectionId, 
data.Options.WorkspaceId),
+                       }
+
+                       err = db.All(&storyCustomFieldValues, 
clausesForCustomFieldValue...)
+                       results := make([]interface{}, 0, 
len(storyCustomFieldValues))
+                       for _, storyCustomFieldValue := range 
storyCustomFieldValues {
+                               results = append(results, storyCustomFieldValue)
+                       }
+                       return results, nil
+               },
+       })
+
+       if err != nil {
+               return err
+       }
+
+       return converter.Execute()
+}
diff --git a/backend/plugins/tapd/tasks/task_custom_field_enricher.go 
b/backend/plugins/tapd/tasks/task_custom_field_enricher.go
new file mode 100644
index 000000000..685825327
--- /dev/null
+++ b/backend/plugins/tapd/tasks/task_custom_field_enricher.go
@@ -0,0 +1,88 @@
+/*
+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"
+       "github.com/apache/incubator-devlake/core/dal"
+       "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/plugin"
+       helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
+       "github.com/apache/incubator-devlake/plugins/tapd/models"
+       "reflect"
+)
+
+var _ plugin.SubTaskEntryPoint = EnrichTaskCustomFields
+
+var EnrichTaskCustomFieldMeta = plugin.SubTaskMeta{
+       Name:       "enrichTaskCustomFields",
+       EntryPoint: EnrichTaskCustomFields,
+       // TODO false or true?
+       EnabledByDefault: true,
+       Description:      "Enrich task custom fields",
+       DomainTypes:      []string{plugin.DOMAIN_TYPE_TICKET},
+}
+
+func EnrichTaskCustomFields(taskCtx plugin.SubTaskContext) errors.Error {
+       rawDataSubTaskArgs, data := CreateRawDataSubTaskArgs(taskCtx, 
RAW_STORY_TABLE)
+       db := taskCtx.GetDal()
+       clauses := []dal.Clause{
+               dal.From(&models.TapdTaskCustomFields{}),
+               dal.Where("connection_id = ? and workspace_id = ?", 
data.Options.ConnectionId, data.Options.WorkspaceId),
+               dal.Orderby("name ASC"),
+       }
+
+       cursor, err := db.Cursor(clauses...)
+       if err != nil {
+               return err
+       }
+
+       defer cursor.Close()
+
+       converter, err := helper.NewDataConverter(helper.DataConverterArgs{
+               RawDataSubTaskArgs: *rawDataSubTaskArgs,
+               InputRowType:       
reflect.TypeOf(models.TapdTaskCustomFields{}),
+               Input:              cursor,
+               Convert: func(inputRow interface{}) ([]interface{}, 
errors.Error) {
+                       customField := inputRow.(*models.TapdTaskCustomFields)
+
+                       taskCustomFieldValues := 
make([]*models.TapdTaskCustomFieldValue, 0)
+
+                       clausesForCustomFieldValue := []dal.Clause{
+                               dal.Select(fmt.Sprintf(`connection_id, 
workspace_id, id as task_id, %s as custom_value, '%s' as custom_field, '%s' as 
name`,
+                                       customField.CustomField, 
customField.CustomField, customField.Name)),
+                               dal.From(&models.TapdTask{}),
+                               dal.Where("connection_id = ? and workspace_id = 
?", data.Options.ConnectionId, data.Options.WorkspaceId),
+                       }
+
+                       err = db.All(&taskCustomFieldValues, 
clausesForCustomFieldValue...)
+                       results := make([]interface{}, 0, 
len(taskCustomFieldValues))
+                       for _, taskCustomFieldValue := range 
taskCustomFieldValues {
+                               results = append(results, taskCustomFieldValue)
+
+                       }
+                       return results, nil
+               },
+       })
+
+       if err != nil {
+               return err
+       }
+
+       return converter.Execute()
+}

Reply via email to