diff --git a/backend/core/models/project.go b/backend/core/models/project.go index 5014f20b3f8..56cd3b500e7 100644 --- a/backend/core/models/project.go +++ b/backend/core/models/project.go @@ -62,6 +62,7 @@ type ApiInputProject struct { BaseProject `mapstructure:",squash"` Enable *bool `json:"enable" mapstructure:"enable"` Metrics []*BaseMetric `json:"metrics" mapstructure:"metrics"` + Blueprint *Blueprint `json:"blueprint" mapstructure:"blueprint"` } type ApiOutputProject struct { diff --git a/backend/server/services/project.go b/backend/server/services/project.go index 51d773e820a..754092ad2c6 100644 --- a/backend/server/services/project.go +++ b/backend/server/services/project.go @@ -19,6 +19,7 @@ package services import ( "fmt" + "time" "github.com/apache/incubator-devlake/core/dal" "github.com/apache/incubator-devlake/core/errors" @@ -108,12 +109,43 @@ func CreateProject(projectInput *models.ApiInputProject) (*models.ApiOutputProje } } + // create blueprint + blueprint := &models.Blueprint{ + Name: project.Name + "-Blueprint", + ProjectName: project.Name, + Mode: "NORMAL", + Enable: true, + CronConfig: "0 0 * * *", + IsManual: false, + SyncPolicy: models.SyncPolicy{ + TimeAfter: func() *time.Time { + t := time.Now().AddDate(0, -6, 0) + t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) + return &t + }(), + }, + Connections: nil, + } + if projectInput.Blueprint != nil { + blueprint = projectInput.Blueprint + } + err = tx.Create(blueprint) + if err != nil { + return nil, errors.Default.Wrap(err, "error creating DB blueprint") + } + // all good, commit transaction err = tx.Commit() if err != nil { return nil, err } + // reload schedule + err = reloadBlueprint(blueprint) + if err != nil { + return nil, err + } + return makeProjectOutput(project, false) } diff --git a/backend/test/e2e/remote/helper.go b/backend/test/e2e/remote/helper.go index 82cc2e3229d..e94f6a4a399 100644 --- a/backend/test/e2e/remote/helper.go +++ b/backend/test/e2e/remote/helper.go @@ -145,8 +145,10 @@ func CreateTestBlueprints(t *testing.T, client *helper.DevlakeClient, count int) client.CreateProject(&helper.ProjectConfig{ ProjectName: projectName, }) - blueprint := client.CreateBasicBlueprintV2( - fmt.Sprintf("Test blueprint %d", i), + project := client.GetProject(projectName) + blueprint := client.PatchBasicBlueprintV2( + project.Blueprint.ID, + fmt.Sprintf("Test project %d-Blueprint", i), &helper.BlueprintV2Config{ Connection: &models.BlueprintConnection{ PluginName: "fake", @@ -162,7 +164,6 @@ func CreateTestBlueprints(t *testing.T, client *helper.DevlakeClient, count int) }, ) bps = append(bps, blueprint) - project := client.GetProject(projectName) require.Equal(t, blueprint.Name, project.Blueprint.Name) projects = append(projects, project) } diff --git a/backend/test/helper/api.go b/backend/test/helper/api.go index 9099f90adad..2af718f3500 100644 --- a/backend/test/helper/api.go +++ b/backend/test/helper/api.go @@ -22,6 +22,7 @@ import ( "net/http" "reflect" "strings" + "time" "github.com/apache/incubator-devlake/helpers/pluginhelper/services" @@ -103,6 +104,37 @@ func (d *DevlakeClient) CreateBasicBlueprintV2(name string, config *BlueprintV2C return blueprint } +// PatchBasicBlueprintV2 FIXME +func (d *DevlakeClient) PatchBasicBlueprintV2(blueprintId uint64, name string, config *BlueprintV2Config) models.Blueprint { + blueprint := models.Blueprint{ + Name: name, + ProjectName: config.ProjectName, + Mode: models.BLUEPRINT_MODE_NORMAL, + Plan: nil, + Enable: true, + CronConfig: "manual", + IsManual: true, + SyncPolicy: models.SyncPolicy{ + SkipOnFail: config.SkipOnFail, + TimeAfter: func() *time.Time { + t, _ := time.Parse(time.RFC3339, time.Now().AddDate(0, 0, 1).Format(time.RFC3339)) + return &t + }(), + }, + Labels: []string{"test-label"}, + Connections: []*models.BlueprintConnection{ + config.Connection, + }, + } + d.testCtx.Helper() + blueprint = sendHttpRequest[models.Blueprint](d.testCtx, d.timeout, &testContext{ + client: d, + printPayload: true, + inlineJson: false, + }, http.MethodPatch, fmt.Sprintf("%s/blueprints/%d", d.Endpoint, blueprintId), nil, &blueprint) + return blueprint +} + func (d *DevlakeClient) ListBlueprints() blueprints.PaginatedBlueprint { return sendHttpRequest[blueprints.PaginatedBlueprint](d.testCtx, d.timeout, &testContext{ client: d, @@ -156,8 +188,9 @@ func (d *DevlakeClient) CreateProject(project *ProjectConfig) models.ApiOutputPr Name: project.ProjectName, Description: project.ProjectDescription, }, - Enable: Val(true), - Metrics: metrics, + Enable: Val(true), + Metrics: metrics, + Blueprint: project.Blueprint, }) } diff --git a/backend/test/helper/models.go b/backend/test/helper/models.go index 1596a3c65db..8e0bbbc1058 100644 --- a/backend/test/helper/models.go +++ b/backend/test/helper/models.go @@ -36,6 +36,7 @@ type ( ProjectDescription string EnableDora bool MetricPlugins []ProjectPlugin + Blueprint *models.Blueprint } ScopeResponse struct {