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

lynwee 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 e2afe72d4 refactor: extract MakePipelinePlanTask function (#6550)
e2afe72d4 is described below

commit e2afe72d451f1f49ab1cb97bb1d721953b30e666
Author: Klesh Wong <[email protected]>
AuthorDate: Fri Dec 1 18:46:14 2023 +0800

    refactor: extract MakePipelinePlanTask function (#6550)
    
    * fix: sensitive information should be removed for Create Scope api
    
    * fix: shoud not panic when scope config is missing
    
    * refactor: rename gitlab remote api file
    
    * refactor: extract MakePipelinePlanTask function
---
 .../helpers/pluginhelper/api/model_api_helper.go   |  1 +
 backend/helpers/pluginhelper/api/pipeline_plan.go  | 31 ++++++++++++++++++++++
 backend/helpers/srvhelper/scope_service_helper.go  | 13 +++++----
 backend/plugins/github/api/blueprint_v200.go       | 24 +++++------------
 .../github/api/{remote.go => remote_api.go}        |  0
 backend/plugins/github/tasks/task_data.go          |  9 -------
 .../gitlab/api/{remote.go => remote_api.go}        |  0
 7 files changed, 46 insertions(+), 32 deletions(-)

diff --git a/backend/helpers/pluginhelper/api/model_api_helper.go 
b/backend/helpers/pluginhelper/api/model_api_helper.go
index af4905eb6..f0f72bda3 100644
--- a/backend/helpers/pluginhelper/api/model_api_helper.go
+++ b/backend/helpers/pluginhelper/api/model_api_helper.go
@@ -73,6 +73,7 @@ func (self *ModelApiHelper[M]) Post(input 
*plugin.ApiResourceInput) (*plugin.Api
        if err != nil {
                return nil, err
        }
+       model = self.Sanitize(model)
        return &plugin.ApiResourceOutput{
                Status: http.StatusCreated,
                Body:   model,
diff --git a/backend/helpers/pluginhelper/api/pipeline_plan.go 
b/backend/helpers/pluginhelper/api/pipeline_plan.go
index ce920b168..4c11f6ef3 100644
--- a/backend/helpers/pluginhelper/api/pipeline_plan.go
+++ b/backend/helpers/pluginhelper/api/pipeline_plan.go
@@ -21,6 +21,7 @@ import (
        "fmt"
 
        "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/models"
        plugin "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/core/utils"
 )
@@ -52,3 +53,33 @@ func MakePipelinePlanSubtasks(subtaskMetas 
[]plugin.SubTaskMeta, entities []stri
        }
        return subtasks, nil
 }
+
+func MakePipelinePlanTask(
+       pluginName string,
+       subtaskMetas []plugin.SubTaskMeta,
+       entities []string,
+       options interface{},
+) (*models.PipelineTask, errors.Error) {
+       subtasks, err := MakePipelinePlanSubtasks(subtaskMetas, entities)
+       if err != nil {
+               return nil, err
+       }
+       op, err := encodeTaskOptions(options)
+       if err != nil {
+               return nil, err
+       }
+       return &models.PipelineTask{
+               Plugin:   pluginName,
+               Subtasks: subtasks,
+               Options:  op,
+       }, nil
+}
+
+func encodeTaskOptions(op interface{}) (map[string]interface{}, errors.Error) {
+       var result map[string]interface{}
+       err := Decode(op, &result, nil)
+       if err != nil {
+               return nil, err
+       }
+       return result, nil
+}
diff --git a/backend/helpers/srvhelper/scope_service_helper.go 
b/backend/helpers/srvhelper/scope_service_helper.go
index 27e2257b1..a375c446a 100644
--- a/backend/helpers/srvhelper/scope_service_helper.go
+++ b/backend/helpers/srvhelper/scope_service_helper.go
@@ -159,15 +159,18 @@ func (scopeSrv *ScopeSrvHelper[C, S, SC]) 
getScopeConfig(scopeConfigId uint64) *
        if scopeConfigId < 1 {
                return nil
        }
-       scopeConfig := new(SC)
-       errors.Must(scopeSrv.db.First(
-               scopeConfig,
+       var scopeConfig SC
+       err := scopeSrv.db.First(
+               &scopeConfig,
                dal.Where(
                        "id = ?",
                        scopeConfigId,
                ),
-       ))
-       return scopeConfig
+       )
+       if err != nil {
+               return nil
+       }
+       return &scopeConfig
 }
 
 func (scopeSrv *ScopeSrvHelper[C, S, SC]) getAllBlueprinsByScope(connectionId 
uint64, scopeId string) []*models.Blueprint {
diff --git a/backend/plugins/github/api/blueprint_v200.go 
b/backend/plugins/github/api/blueprint_v200.go
index aaf85b147..156b10730 100644
--- a/backend/plugins/github/api/blueprint_v200.go
+++ b/backend/plugins/github/api/blueprint_v200.go
@@ -111,11 +111,7 @@ func makeDataSourcePipelinePlanV200(
                        GithubId:     githubRepo.GithubId,
                        Name:         githubRepo.FullName,
                }
-               options, err := tasks.EncodeTaskOptions(op)
-               if err != nil {
-                       return nil, err
-               }
-               stage, err = addGithub(subtaskMetas, connection, 
scopeConfig.Entities, stage, options)
+               stage, err := addGithub(subtaskMetas, connection, 
scopeConfig.Entities, stage, op)
                if err != nil {
                        return nil, err
                }
@@ -196,7 +192,7 @@ func addGithub(
        connection *models.GithubConnection,
        entities []string,
        stage coreModels.PipelineStage,
-       options map[string]interface{},
+       options *tasks.GithubOptions,
 ) (coreModels.PipelineStage, errors.Error) {
        // construct github(graphql) task
        if connection.EnableGraphql {
@@ -206,28 +202,20 @@ func addGithub(
                        return nil, err
                }
                if pluginGq, ok := p.(plugin.PluginTask); ok {
-                       subtasks, err := 
helper.MakePipelinePlanSubtasks(pluginGq.SubTaskMetas(), entities)
+                       task, err := 
helper.MakePipelinePlanTask("github_graphql", pluginGq.SubTaskMetas(), 
entities, options)
                        if err != nil {
                                return nil, err
                        }
-                       stage = append(stage, &coreModels.PipelineTask{
-                               Plugin:   "github_graphql",
-                               Subtasks: subtasks,
-                               Options:  options,
-                       })
+                       stage = append(stage, task)
                } else {
                        return nil, errors.BadInput.New("plugin github_graphql 
does not support SubTaskMetas")
                }
        } else {
-               subtasks, err := helper.MakePipelinePlanSubtasks(subtaskMetas, 
entities)
+               task, err := helper.MakePipelinePlanTask("github", 
subtaskMetas, entities, options)
                if err != nil {
                        return nil, err
                }
-               stage = append(stage, &coreModels.PipelineTask{
-                       Plugin:   "github",
-                       Subtasks: subtasks,
-                       Options:  options,
-               })
+               stage = append(stage, task)
        }
        return stage, nil
 }
diff --git a/backend/plugins/github/api/remote.go 
b/backend/plugins/github/api/remote_api.go
similarity index 100%
rename from backend/plugins/github/api/remote.go
rename to backend/plugins/github/api/remote_api.go
diff --git a/backend/plugins/github/tasks/task_data.go 
b/backend/plugins/github/tasks/task_data.go
index 714dbbd95..f87d57694 100644
--- a/backend/plugins/github/tasks/task_data.go
+++ b/backend/plugins/github/tasks/task_data.go
@@ -67,15 +67,6 @@ func DecodeTaskOptions(options map[string]interface{}) 
(*GithubOptions, errors.E
        return &op, nil
 }
 
-func EncodeTaskOptions(op *GithubOptions) (map[string]interface{}, 
errors.Error) {
-       var result map[string]interface{}
-       err := helper.Decode(op, &result, nil)
-       if err != nil {
-               return nil, err
-       }
-       return result, nil
-}
-
 func ValidateTaskOptions(op *GithubOptions) errors.Error {
        if op.Name == "" {
                op.Name = strings.Trim(fmt.Sprintf("%s/%s", op.Owner, op.Repo), 
" ")
diff --git a/backend/plugins/gitlab/api/remote.go 
b/backend/plugins/gitlab/api/remote_api.go
similarity index 100%
rename from backend/plugins/gitlab/api/remote.go
rename to backend/plugins/gitlab/api/remote_api.go

Reply via email to