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 dd6d4e3c5 fix: support create bp without project (#3970)
dd6d4e3c5 is described below

commit dd6d4e3c54da1c8a530af1301ccdc9a3c123fbe2
Author: mappjzc <[email protected]>
AuthorDate: Mon Dec 19 18:49:00 2022 +0800

    fix: support create bp without project (#3970)
    
    support create bp without project
    
    Nddtfjiang <[email protected]>
---
 services/blueprint.go               | 26 ++++++++++++++++++++------
 services/blueprint_makeplan_v200.go | 10 ++++++----
 2 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/services/blueprint.go b/services/blueprint.go
index 81fa3b0cd..c1f5c8491 100644
--- a/services/blueprint.go
+++ b/services/blueprint.go
@@ -107,6 +107,9 @@ func GetBlueprint(blueprintId uint64) (*models.Blueprint, 
errors.Error) {
 
 // GetBlueprintByProjectName returns the detail of a given ProjectName
 func GetBlueprintByProjectName(projectName string) (*models.Blueprint, 
errors.Error) {
+       if projectName == "" {
+               return nil, errors.Internal.New("can not use the empty 
projectName to search the unique blueprint")
+       }
        dbBlueprint, err := GetDbBlueprintByProjectName(projectName)
        if err != nil {
                // Allow specific projectName to fail to find the corresponding 
blueprint
@@ -136,7 +139,18 @@ func validateBlueprintAndMakePlan(blueprint 
*models.Blueprint) errors.Error {
                if err != nil {
                        return errors.Default.Wrap(err, fmt.Sprintf("invalid 
projectName: [%s] for the blueprint [%s]", blueprint.ProjectName, 
blueprint.Name))
                }
+
+               bp, err := GetBlueprintByProjectName(blueprint.ProjectName)
+               if err != nil {
+                       return err
+               }
+               if bp != nil {
+                       if bp.ID != blueprint.ID {
+                               return errors.Default.New(fmt.Sprintf("Each 
project can only be used by one blueprint. The currently selected projectName: 
[%s] has been used by blueprint: [id:%d] [name:%s] and cannot be reused.", 
bp.ProjectName, bp.ID, bp.Name))
+                       }
+               }
        }
+
        if strings.ToLower(blueprint.CronConfig) == "manual" {
                blueprint.IsManual = true
        }
@@ -341,15 +355,15 @@ func MakePlanForBlueprint(blueprint *models.Blueprint) 
(core.PipelinePlan, error
                // Notice: v1 not complete SkipOnFail & CreatedDateAfter
                plan, err = GeneratePlanJsonV100(bpSettings)
        case "2.0.0":
-               if blueprint.ProjectName == "" {
-                       return nil, errors.BadInput.New("projectName is 
required for blueprint v2.0.0")
-               }
                // load project metric plugins and convert it to a map
                metrics := make(map[string]json.RawMessage)
                projectMetrics := make([]models.ProjectMetric, 0)
-               db.Find(&projectMetrics, "project_name = ? AND enable = ?", 
blueprint.ProjectName, true)
-               for _, projectMetric := range projectMetrics {
-                       metrics[projectMetric.PluginName] = 
json.RawMessage(projectMetric.PluginOption)
+
+               if blueprint.ProjectName != "" {
+                       db.Find(&projectMetrics, "project_name = ? AND enable = 
?", blueprint.ProjectName, true)
+                       for _, projectMetric := range projectMetrics {
+                               metrics[projectMetric.PluginName] = 
json.RawMessage(projectMetric.PluginOption)
+                       }
                }
                plan, err = GeneratePlanJsonV200(blueprint.ProjectName, 
bpSyncPolicy, bpSettings, metrics)
        default:
diff --git a/services/blueprint_makeplan_v200.go 
b/services/blueprint_makeplan_v200.go
index 997cec2d2..607b258f5 100644
--- a/services/blueprint_makeplan_v200.go
+++ b/services/blueprint_makeplan_v200.go
@@ -41,12 +41,14 @@ func GeneratePlanJsonV200(
        }
        // refresh project_mapping table to reflect project/scopes relationship
        if len(scopes) > 0 {
-               e := db.Where("project_name = ?", 
projectName).Delete(&crossdomain.ProjectMapping{}).Error
-               if e != nil {
-                       return nil, errors.Default.Wrap(e, 
fmt.Sprintf("projectName:[%s]", projectName))
+               if projectName != "" {
+                       e := db.Where("project_name = ?", 
projectName).Delete(&crossdomain.ProjectMapping{}).Error
+                       if e != nil {
+                               return nil, errors.Default.Wrap(e, 
fmt.Sprintf("projectName:[%s]", projectName))
+                       }
                }
                for _, scope := range scopes {
-                       e = basicRes.GetDal().CreateOrUpdate(scope)
+                       e := basicRes.GetDal().CreateOrUpdate(scope)
                        if e != nil {
                                scopeInfo := 
fmt.Sprintf("[Id:%s][Name:%s][TableName:%s]", scope.ScopeId(), 
scope.ScopeName(), scope.TableName())
                                return nil, errors.Default.Wrap(e, 
fmt.Sprintf("failed to create scopes:[%s]", scopeInfo))

Reply via email to