Skip to content

Commit 5f4b6c5

Browse files
authored
fix(api): load missing fields from app deployment strategies (#6140)
Signed-off-by: Yvonnick Esnault <[email protected]>
1 parent d9edaeb commit 5f4b6c5

6 files changed

+54
-29
lines changed

engine/api/application/dao.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (e dbApplication) Canonical() gorpmapper.CanonicalForms {
2727
}
2828

2929
// LoadOptionFunc is a type for all options in LoadOptions
30-
type LoadOptionFunc *func(gorp.SqlExecutor, *sdk.Application) error
30+
type LoadOptionFunc *func(context.Context, gorp.SqlExecutor, *sdk.Application) error
3131

3232
// LoadOptions provides all options on project loads functions
3333
var LoadOptions = struct {
@@ -137,14 +137,14 @@ func getWithClearVCSStrategyPassword(ctx context.Context, db gorp.SqlExecutor, k
137137
return nil, err
138138
}
139139
if !isValid {
140-
log.Error(context.Background(), "application.get> application %d data corrupted", dbApp.ID)
140+
log.Error(ctx, "application.get> application %d data corrupted", dbApp.ID)
141141
return nil, sdk.WithStack(sdk.ErrNotFound)
142142
}
143143
dbApp.ProjectKey = key
144-
return unwrap(db, opts, &dbApp)
144+
return unwrap(ctx, db, opts, &dbApp)
145145
}
146146

147-
func unwrap(db gorp.SqlExecutor, opts []LoadOptionFunc, dbApp *dbApplication) (*sdk.Application, error) {
147+
func unwrap(ctx context.Context, db gorp.SqlExecutor, opts []LoadOptionFunc, dbApp *dbApplication) (*sdk.Application, error) {
148148
app := &dbApp.Application
149149
if app.ProjectKey == "" {
150150
pkey, errP := db.SelectStr("SELECT projectkey FROM project WHERE id = $1", app.ProjectID)
@@ -155,7 +155,7 @@ func unwrap(db gorp.SqlExecutor, opts []LoadOptionFunc, dbApp *dbApplication) (*
155155
}
156156

157157
for _, f := range opts {
158-
if err := (*f)(db, app); err != nil && sdk.Cause(err) != sql.ErrNoRows {
158+
if err := (*f)(ctx, db, app); err != nil && sdk.Cause(err) != sql.ErrNoRows {
159159
return nil, sdk.WrapError(err, "application.unwrap")
160160
}
161161
}
@@ -283,7 +283,7 @@ func getAllWithClearVCS(ctx context.Context, db gorp.SqlExecutor, opts []LoadOpt
283283
continue
284284
}
285285
a := &res[i]
286-
app, err := unwrap(db, opts, a)
286+
app, err := unwrap(ctx, db, opts, a)
287287
if err != nil {
288288
return nil, sdk.WrapError(err, "application.getAllWithClearVCS")
289289
}
@@ -310,7 +310,7 @@ func getAll(ctx context.Context, db gorp.SqlExecutor, opts []LoadOptionFunc, que
310310
}
311311

312312
a := &res[i]
313-
app, err := unwrap(db, opts, a)
313+
app, err := unwrap(ctx, db, opts, a)
314314
if err != nil {
315315
return nil, sdk.WrapError(err, "application.getAll")
316316
}

engine/api/application/dao_application_integration.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/rockbears/log"
99

1010
"github.com/ovh/cds/engine/api/database/gorpmapping"
11+
"github.com/ovh/cds/engine/api/integration"
1112
"github.com/ovh/cds/engine/gorpmapper"
1213
"github.com/ovh/cds/sdk"
1314
)
@@ -46,15 +47,15 @@ func (e *dbApplicationDeploymentStrategy) IntegrationConfig() sdk.IntegrationCon
4647
}
4748

4849
// LoadDeploymentStrategies loads the deployment strategies for an application
49-
func LoadDeploymentStrategies(db gorp.SqlExecutor, appID int64, withClearPassword bool) (map[string]sdk.IntegrationConfig, error) {
50+
func LoadDeploymentStrategies(ctx context.Context, db gorp.SqlExecutor, appID int64, withClearPassword bool) (map[string]sdk.IntegrationConfig, error) {
5051
query := gorpmapping.NewQuery(`
5152
SELECT *
5253
FROM application_deployment_strategy
5354
WHERE application_id = $1
5455
`).Args(appID)
5556

5657
var res []dbApplicationDeploymentStrategy
57-
if err := gorpmapping.GetAll(context.Background(), db, query, &res, gorpmapping.GetOptions.WithDecryption); err != nil {
58+
if err := gorpmapping.GetAll(ctx, db, query, &res, gorpmapping.GetOptions.WithDecryption); err != nil {
5859
return nil, sdk.WrapError(err, "unable to load deployment strategies")
5960
}
6061

@@ -65,7 +66,7 @@ func LoadDeploymentStrategies(db gorp.SqlExecutor, appID int64, withClearPasswor
6566
return nil, err
6667
}
6768
if !isValid {
68-
log.Error(context.Background(), "application.LoadDeploymentStrategies> application_deployment_strategy %d data corrupted", appID)
69+
log.Error(ctx, "application.LoadDeploymentStrategies> application_deployment_strategy %d data corrupted", appID)
6970
continue
7071
}
7172

@@ -85,12 +86,16 @@ func LoadDeploymentStrategies(db gorp.SqlExecutor, appID int64, withClearPasswor
8586
newCfg[k] = v
8687
}
8788
}
88-
// Sorry about that :(
89-
projectIntegrationName, err := db.SelectStr("SELECT name FROM project_integration WHERE id = $1 ", r.ProjectIntegrationID)
89+
projectIntegration, err := integration.LoadProjectIntegrationByID(ctx, db, r.ProjectIntegrationID)
9090
if err != nil {
9191
return nil, sdk.WrapError(err, "unable to find project integration name for ID=%d", r.ProjectIntegrationID)
9292
}
93-
deps[projectIntegrationName] = newCfg
93+
for name, val := range projectIntegration.Model.AdditionalDefaultConfig {
94+
if _, ok := newCfg[name]; !ok {
95+
newCfg[name] = val
96+
}
97+
}
98+
deps[projectIntegration.Name] = newCfg
9499
}
95100

96101
return deps, nil

engine/api/application/dao_application_integration_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,23 @@ func Test_LoadAllDeploymentAllApps(t *testing.T) {
7575
require.Len(t, deps[app2.ID], 1)
7676
require.Equal(t, "secret1", deps[app1.ID][pp.ID]["token"].Value)
7777
require.Equal(t, "secret2", deps[app2.ID][pp.ID]["token"].Value)
78+
79+
pf.AdditionalDefaultConfig = sdk.IntegrationConfig{
80+
"token": sdk.IntegrationConfigValue{
81+
Type: sdk.IntegrationConfigTypePassword,
82+
Value: "my-secret-token",
83+
},
84+
"bar": sdk.IntegrationConfigValue{
85+
Type: sdk.IntegrationConfigTypeString,
86+
Value: "foo",
87+
},
88+
}
89+
90+
test.NoError(t, integration.UpdateModel(context.TODO(), db, &pf))
91+
92+
st, err := application.LoadDeploymentStrategies(context.TODO(), db, app1.ID, false)
93+
require.NoError(t, err)
94+
require.Len(t, st, 1)
95+
require.Equal(t, st[pf.Name]["token"].Value, "**********")
96+
require.Equal(t, st[pf.Name]["bar"].Value, "foo")
7897
}

engine/api/application/dao_dependencies.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package application
22

33
import (
4+
"context"
45
"database/sql"
56

67
"github.com/go-gorp/gorp"
@@ -9,14 +10,14 @@ import (
910
)
1011

1112
var (
12-
loadDefaultDependencies = func(db gorp.SqlExecutor, app *sdk.Application) error {
13-
if err := loadVariables(db, app); err != nil && sdk.Cause(err) != sql.ErrNoRows {
13+
loadDefaultDependencies = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
14+
if err := loadVariables(ctx, db, app); err != nil && sdk.Cause(err) != sql.ErrNoRows {
1415
return sdk.WrapError(err, "application.loadDefaultDependencies %s", app.Name)
1516
}
1617
return nil
1718
}
1819

19-
loadVariables = func(db gorp.SqlExecutor, app *sdk.Application) error {
20+
loadVariables = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
2021
variables, err := LoadAllVariables(db, app.ID)
2122
if err != nil && sdk.Cause(err) != sql.ErrNoRows {
2223
return sdk.WrapError(err, "Unable to load variables for application %d", app.ID)
@@ -25,7 +26,7 @@ var (
2526
return nil
2627
}
2728

28-
loadVariablesWithClearPassword = func(db gorp.SqlExecutor, app *sdk.Application) error {
29+
loadVariablesWithClearPassword = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
2930
variables, err := LoadAllVariablesWithDecrytion(db, app.ID)
3031
if err != nil && sdk.Cause(err) != sql.ErrNoRows {
3132
return sdk.WrapError(err, "Unable to load variables for application %d", app.ID)
@@ -34,7 +35,7 @@ var (
3435
return nil
3536
}
3637

37-
loadKeys = func(db gorp.SqlExecutor, app *sdk.Application) error {
38+
loadKeys = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
3839
keys, err := LoadAllKeys(db, app.ID)
3940
if err != nil {
4041
return err
@@ -43,7 +44,7 @@ var (
4344
return nil
4445
}
4546

46-
loadClearKeys = func(db gorp.SqlExecutor, app *sdk.Application) error {
47+
loadClearKeys = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
4748
keys, err := LoadAllKeysWithPrivateContent(db, app.ID)
4849
if err != nil {
4950
return err
@@ -52,16 +53,16 @@ var (
5253
return nil
5354
}
5455

55-
loadDeploymentStrategies = func(db gorp.SqlExecutor, app *sdk.Application) error {
56+
loadDeploymentStrategies = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
5657
var err error
57-
app.DeploymentStrategies, err = LoadDeploymentStrategies(db, app.ID, false)
58+
app.DeploymentStrategies, err = LoadDeploymentStrategies(ctx, db, app.ID, false)
5859
if err != nil && sdk.Cause(err) != sql.ErrNoRows {
5960
return sdk.WrapError(err, "Unable to load deployment strategies for application %d", app.ID)
6061
}
6162
return nil
6263
}
6364

64-
loadIcon = func(db gorp.SqlExecutor, app *sdk.Application) error {
65+
loadIcon = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
6566
var err error
6667
app.Icon, err = LoadIcon(db, app.ID)
6768
if err != nil && sdk.Cause(err) != sql.ErrNoRows {
@@ -70,7 +71,7 @@ var (
7071
return nil
7172
}
7273

73-
loadVulnerabilities = func(db gorp.SqlExecutor, app *sdk.Application) error {
74+
loadVulnerabilities = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
7475
var err error
7576
app.Vulnerabilities, err = LoadVulnerabilities(db, app.ID)
7677
if err != nil {
@@ -79,9 +80,9 @@ var (
7980
return nil
8081
}
8182

82-
loadDeploymentStrategiesWithClearPassword = func(db gorp.SqlExecutor, app *sdk.Application) error {
83+
loadDeploymentStrategiesWithClearPassword = func(ctx context.Context, db gorp.SqlExecutor, app *sdk.Application) error {
8384
var err error
84-
app.DeploymentStrategies, err = LoadDeploymentStrategies(db, app.ID, true)
85+
app.DeploymentStrategies, err = LoadDeploymentStrategies(ctx, db, app.ID, true)
8586
if err != nil && sdk.Cause(err) != sql.ErrNoRows {
8687
return sdk.WrapError(err, "Unable to load deployment strategies for application %d", app.ID)
8788
}

engine/api/application_deployment_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func Test_postApplicationDeploymentStrategyConfigHandlerAsProvider(t *testing.T)
353353
})
354354
test.NoError(t, err)
355355

356-
cfg, err := application.LoadDeploymentStrategies(api.mustDB(), app.ID, true)
356+
cfg, err := application.LoadDeploymentStrategies(context.TODO(), api.mustDB(), app.ID, true)
357357
test.NoError(t, err)
358358

359359
var assertCfg = func(key string, cfg sdk.IntegrationConfig, expected sdk.IntegrationConfigValue) {

engine/api/project/loader.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func loadApplicationWithDeploymentStrategies(ctx context.Context, db gorp.SqlExe
101101
}
102102
for i := range proj.Applications {
103103
a := &proj.Applications[i]
104-
if err := (*application.LoadOptions.WithDeploymentStrategies)(db, a); err != nil {
104+
if err := (*application.LoadOptions.WithDeploymentStrategies)(ctx, db, a); err != nil {
105105
return sdk.WithStack(err)
106106
}
107107
}
@@ -134,7 +134,7 @@ func loadApplicationVariables(ctx context.Context, db gorp.SqlExecutor, proj *sd
134134
}
135135

136136
for _, a := range proj.Applications {
137-
if err := (*application.LoadOptions.WithVariables)(db, &a); err != nil {
137+
if err := (*application.LoadOptions.WithVariables)(ctx, db, &a); err != nil {
138138
return sdk.WithStack(err)
139139
}
140140
}
@@ -150,7 +150,7 @@ func loadApplicationKeys(ctx context.Context, db gorp.SqlExecutor, proj *sdk.Pro
150150
}
151151

152152
for _, a := range proj.Applications {
153-
if err := (*application.LoadOptions.WithKeys)(db, &a); err != nil {
153+
if err := (*application.LoadOptions.WithKeys)(ctx, db, &a); err != nil {
154154
return sdk.WithStack(err)
155155
}
156156
}

0 commit comments

Comments
 (0)