Skip to content

Commit

Permalink
refactor: extract MakePipelinePlanTask function (#6550)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
klesh authored Dec 1, 2023
1 parent 09457a1 commit e2afe72
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 32 deletions.
1 change: 1 addition & 0 deletions backend/helpers/pluginhelper/api/model_api_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions backend/helpers/pluginhelper/api/pipeline_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
13 changes: 8 additions & 5 deletions backend/helpers/srvhelper/scope_service_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 6 additions & 18 deletions backend/plugins/github/api/blueprint_v200.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
File renamed without changes.
9 changes: 0 additions & 9 deletions backend/plugins/github/tasks/task_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), " ")
Expand Down
File renamed without changes.

0 comments on commit e2afe72

Please sign in to comment.