You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by li...@apache.org on 2022/11/18 10:54:01 UTC

[incubator-devlake] branch main updated: feat: add project to blueprint (#3754)

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 043c99fec feat: add project to blueprint (#3754)
043c99fec is described below

commit 043c99fecd820672acffe877d9a6f5dc5116d42c
Author: mappjzc <zh...@merico.dev>
AuthorDate: Fri Nov 18 18:53:56 2022 +0800

    feat: add project to blueprint (#3754)
    
    Add project_name to blueprint.
    
    Nddtfjiang <zh...@merico.dev>
---
 api/blueprints/blueprints.go                       |  9 +++-
 models/blueprint.go                                | 19 ++++---
 .../20221117_add_project_to_blueprint.go           | 58 ++++++++++++++++++++++
 models/migrationscripts/register.go                |  1 +
 services/blueprint.go                              | 12 ++++-
 5 files changed, 88 insertions(+), 11 deletions(-)

diff --git a/api/blueprints/blueprints.go b/api/blueprints/blueprints.go
index c91b234f3..517f6c30e 100644
--- a/api/blueprints/blueprints.go
+++ b/api/blueprints/blueprints.go
@@ -28,6 +28,11 @@ import (
 	"github.com/gin-gonic/gin"
 )
 
+type PaginatedBlueprint struct {
+	Blueprints []*models.Blueprint
+	Count      int64
+}
+
 // @Summary post blueprints
 // @Description post blueprints
 // @Tags framework/blueprints
@@ -59,7 +64,7 @@ func Post(c *gin.Context) {
 // @Description get blueprints
 // @Tags framework/blueprints
 // @Accept application/json
-// @Success 200  {object} gin.H
+// @Success 200  {object} PaginatedBlueprint
 // @Failure 400  {object} shared.ApiBody "Bad Request"
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /blueprints [get]
@@ -75,7 +80,7 @@ func Index(c *gin.Context) {
 		shared.ApiOutputAbort(c, errors.Default.Wrap(err, "error getting blueprints"))
 		return
 	}
-	shared.ApiOutputSuccess(c, gin.H{"blueprints": blueprints, "count": count}, http.StatusOK)
+	shared.ApiOutputSuccess(c, PaginatedBlueprint{Blueprints: blueprints, Count: count}, http.StatusOK)
 }
 
 // @Summary get blueprints
diff --git a/models/blueprint.go b/models/blueprint.go
index 18abab73e..4d00a0931 100644
--- a/models/blueprint.go
+++ b/models/blueprint.go
@@ -19,6 +19,7 @@ package models
 
 import (
 	"encoding/json"
+
 	"github.com/apache/incubator-devlake/errors"
 
 	"github.com/apache/incubator-devlake/models/common"
@@ -32,10 +33,11 @@ const (
 
 // @Description CronConfig
 type Blueprint struct {
-	Name   string          `json:"name" validate:"required"`
-	Mode   string          `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
-	Plan   json.RawMessage `json:"plan"`
-	Enable bool            `json:"enable"`
+	Name        string          `json:"name" validate:"required"`
+	ProjectName string          `json:"projectName" gorm:"type:varchar(255)"`
+	Mode        string          `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
+	Plan        json.RawMessage `json:"plan"`
+	Enable      bool            `json:"enable"`
 	//please check this https://crontab.guru/ for detail
 	CronConfig   string          `json:"cronConfig" format:"* * * * *" example:"0 0 * * 1"`
 	IsManual     bool            `json:"isManual"`
@@ -64,10 +66,11 @@ func (bp *Blueprint) UnmarshalPlan() (core.PipelinePlan, errors.Error) {
 
 // @Description CronConfig
 type DbBlueprint struct {
-	Name   string `json:"name" validate:"required"`
-	Mode   string `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
-	Plan   string `json:"plan" encrypt:"yes"`
-	Enable bool   `json:"enable"`
+	Name        string `json:"name" validate:"required"`
+	ProjectName string `json:"projectName" gorm:"type:varchar(255)"`
+	Mode        string `json:"mode" gorm:"varchar(20)" validate:"required,oneof=NORMAL ADVANCED"`
+	Plan        string `json:"plan" encrypt:"yes"`
+	Enable      bool   `json:"enable"`
 	//please check this https://crontab.guru/ for detail
 	CronConfig   string `json:"cronConfig" format:"* * * * *" example:"0 0 * * 1"`
 	IsManual     bool   `json:"isManual"`
diff --git a/models/migrationscripts/20221117_add_project_to_blueprint.go b/models/migrationscripts/20221117_add_project_to_blueprint.go
new file mode 100644
index 000000000..2e598b519
--- /dev/null
+++ b/models/migrationscripts/20221117_add_project_to_blueprint.go
@@ -0,0 +1,58 @@
+/*
+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/errors"
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/core/dal"
+)
+
+var _ core.MigrationScript = (*addProjectToBluePrint)(nil)
+
+type addProjectToBluePrint struct{}
+
+type blueprint20221117 struct {
+	ProjectName string `json:"project_name" gorm:"type:varchar(255)"`
+}
+
+func (blueprint20221117) TableName() string {
+	return "_devlake_blueprints"
+}
+
+func (script *addProjectToBluePrint) Up(basicRes core.BasicRes) errors.Error {
+	// Add column `project_name`  with default value "" and false to `_devlake_blueprints`
+	db := basicRes.GetDal()
+	err := db.AutoMigrate(&blueprint20221117{})
+	if err != nil {
+		return err
+	}
+	err = db.UpdateColumn(&blueprint20221117{}, "project_name", "", dal.Where("project_name is null"))
+	if err != nil {
+		return err
+	}
+	return nil
+}
+
+func (*addProjectToBluePrint) Version() uint64 {
+	return 20221117184342
+}
+
+func (*addProjectToBluePrint) Name() string {
+	return "add project to blueprint"
+}
diff --git a/models/migrationscripts/register.go b/models/migrationscripts/register.go
index 78a1f890e..7e62d2a51 100644
--- a/models/migrationscripts/register.go
+++ b/models/migrationscripts/register.go
@@ -57,5 +57,6 @@ func All() []core.MigrationScript {
 		new(addSkipOnFail),
 		new(modifyCommitsDiffs),
 		new(addProjectTables),
+		new(addProjectToBluePrint),
 	}
 }
diff --git a/services/blueprint.go b/services/blueprint.go
index 5c59b6fc1..6c080ebdb 100644
--- a/services/blueprint.go
+++ b/services/blueprint.go
@@ -21,9 +21,10 @@ import (
 	"encoding/json"
 	goerror "errors"
 	"fmt"
-	"github.com/apache/incubator-devlake/errors"
 	"strings"
 
+	"github.com/apache/incubator-devlake/errors"
+
 	"github.com/apache/incubator-devlake/logger"
 	"github.com/apache/incubator-devlake/models"
 	"github.com/apache/incubator-devlake/plugins/core"
@@ -108,6 +109,15 @@ func validateBlueprintAndMakePlan(blueprint *models.Blueprint) errors.Error {
 	if err != nil {
 		return errors.BadInput.WrapRaw(err)
 	}
+
+	// checking if the project exist
+	if blueprint.ProjectName != "" {
+		_, err := GetProject(blueprint.ProjectName)
+		if err != nil {
+			return errors.Default.Wrap(err, fmt.Sprintf("invalid projectName: [%s] for the blueprint [%s]", blueprint.ProjectName, blueprint.Name))
+		}
+	}
+
 	if strings.ToLower(blueprint.CronConfig) == "manual" {
 		blueprint.IsManual = true
 	}