Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: post project with blueprint #7449

Merged
merged 10 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/core/models/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions backend/server/services/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package services

import (
"fmt"
"time"

"github.com/apache/incubator-devlake/core/dal"
"github.com/apache/incubator-devlake/core/errors"
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions backend/test/e2e/remote/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
}
Expand Down
37 changes: 35 additions & 2 deletions backend/test/helper/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"reflect"
"strings"
"time"

"github.com/apache/incubator-devlake/helpers/pluginhelper/services"

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
})
}

Expand Down
1 change: 1 addition & 0 deletions backend/test/helper/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type (
ProjectDescription string
EnableDora bool
MetricPlugins []ProjectPlugin
Blueprint *models.Blueprint
}

ScopeResponse struct {
Expand Down
Loading