Skip to content

Commit 0257500

Browse files
authored
refactor(api): context for project dao (#5970)
1 parent 327de93 commit 0257500

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+247
-233
lines changed

engine/api/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ func (a *API) Serve(ctx context.Context) error {
767767
return fmt.Errorf("cannot setup builtin workflow outgoing hook models: %v", err)
768768
}
769769

770-
if err := integration.CreateBuiltinModels(a.DBConnectionFactory.GetDBMap(gorpmapping.Mapper)()); err != nil {
770+
if err := integration.CreateBuiltinModels(ctx, a.DBConnectionFactory.GetDBMap(gorpmapping.Mapper)()); err != nil {
771771
return fmt.Errorf("cannot setup integrations: %v", err)
772772
}
773773

engine/api/application.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ func (api *API) updateAsCodeApplicationHandler() service.Handler {
529529

530530
u := getAPIConsumer(ctx)
531531
a.ProjectID = proj.ID
532-
app, err := application.ExportApplication(tx, a, project.EncryptWithBuiltinKey, fmt.Sprintf("app:%d:%s", appDB.ID, branch))
532+
app, err := application.ExportApplication(ctx, tx, a, project.EncryptWithBuiltinKey, fmt.Sprintf("app:%d:%s", appDB.ID, branch))
533533
if err != nil {
534534
return sdk.WrapError(err, "unable to export app %s", a.Name)
535535
}

engine/api/application/application_exporter.go

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

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/go-gorp/gorp"
@@ -10,7 +11,7 @@ import (
1011
)
1112

1213
// Export an application
13-
func Export(db gorp.SqlExecutor, key string, appName string, encryptFunc sdk.EncryptFunc) (exportentities.Application, error) {
14+
func Export(ctx context.Context, db gorp.SqlExecutor, key string, appName string, encryptFunc sdk.EncryptFunc) (exportentities.Application, error) {
1415
app, err := LoadByNameWithClearVCSStrategyPassword(db, key, appName,
1516
LoadOptions.WithVariablesWithClearPassword,
1617
LoadOptions.WithClearKeys,
@@ -20,19 +21,19 @@ func Export(db gorp.SqlExecutor, key string, appName string, encryptFunc sdk.Enc
2021
return exportentities.Application{}, sdk.WrapError(err, "cannot load application %s", appName)
2122
}
2223

23-
return ExportApplication(db, *app, encryptFunc, fmt.Sprintf("appID:%d", app.ID))
24+
return ExportApplication(ctx, db, *app, encryptFunc, fmt.Sprintf("appID:%d", app.ID))
2425
}
2526

2627
// ExportApplication encrypt and export
27-
func ExportApplication(db gorp.SqlExecutor, app sdk.Application, encryptFunc sdk.EncryptFunc, encryptPrefix string) (exportentities.Application, error) {
28+
func ExportApplication(ctx context.Context, db gorp.SqlExecutor, app sdk.Application, encryptFunc sdk.EncryptFunc, encryptPrefix string) (exportentities.Application, error) {
2829
var appvars []sdk.ApplicationVariable
2930
for _, v := range app.Variables {
3031
switch v.Type {
3132
case sdk.KeyVariable:
3233
return exportentities.Application{}, sdk.NewErrorFrom(sdk.ErrUnknownError,
3334
"variable %s: variable of type key are deprecated. Please use the standard keys from your project or your application", v.Name)
3435
case sdk.SecretVariable:
35-
content, err := encryptFunc(db, app.ProjectID, fmt.Sprintf("%s:%s", encryptPrefix, v.Name), v.Value)
36+
content, err := encryptFunc(ctx, db, app.ProjectID, fmt.Sprintf("%s:%s", encryptPrefix, v.Name), v.Value)
3637
if err != nil {
3738
return exportentities.Application{}, sdk.WrapError(err, "unknown key type")
3839
}
@@ -48,7 +49,7 @@ func ExportApplication(db gorp.SqlExecutor, app sdk.Application, encryptFunc sdk
4849
var keys []exportentities.EncryptedKey
4950
// Parse keys
5051
for _, k := range app.Keys {
51-
content, err := encryptFunc(db, app.ProjectID, fmt.Sprintf("%s:%s", encryptPrefix, k.Name), k.Private)
52+
content, err := encryptFunc(ctx, db, app.ProjectID, fmt.Sprintf("%s:%s", encryptPrefix, k.Name), k.Private)
5253
if err != nil {
5354
return exportentities.Application{}, sdk.WrapError(err, "unable to encrypt key")
5455
}
@@ -61,7 +62,7 @@ func ExportApplication(db gorp.SqlExecutor, app sdk.Application, encryptFunc sdk
6162
}
6263

6364
if app.RepositoryStrategy.Password != "" {
64-
content, err := encryptFunc(db, app.ProjectID, fmt.Sprintf("%s:%s", encryptPrefix, "vcs:password"), app.RepositoryStrategy.Password)
65+
content, err := encryptFunc(ctx, db, app.ProjectID, fmt.Sprintf("%s:%s", encryptPrefix, "vcs:password"), app.RepositoryStrategy.Password)
6566
if err != nil {
6667
return exportentities.Application{}, sdk.WrapError(err, "unable to encrypt password")
6768
}
@@ -71,7 +72,7 @@ func ExportApplication(db gorp.SqlExecutor, app sdk.Application, encryptFunc sdk
7172
for pfName, pfConfig := range app.DeploymentStrategies {
7273
for k, v := range pfConfig {
7374
if v.Type == sdk.SecretVariable {
74-
content, err := encryptFunc(db, app.ProjectID, fmt.Sprintf("%s:%s:%s:%s", encryptPrefix, pfName, k, "deployment:password"), v.Value)
75+
content, err := encryptFunc(ctx, db, app.ProjectID, fmt.Sprintf("%s:%s:%s:%s", encryptPrefix, pfName, k, "deployment:password"), v.Value)
7576
if err != nil {
7677
return exportentities.Application{}, sdk.WrapError(err, "Unable to encrypt password")
7778
}

engine/api/application/application_parser.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func ParseAndImport(ctx context.Context, db gorpmapper.SqlExecutorWithTx, cache
7878
case "":
7979
v.Type = sdk.StringVariable
8080
case sdk.SecretVariable:
81-
secret, err := decryptFunc(db, proj.ID, v.Value)
81+
secret, err := decryptFunc(ctx, db, proj.ID, v.Value)
8282
if err != nil {
8383
return app, nil, msgList, sdk.WrapError(sdk.NewError(sdk.ErrWrongRequest, err), "unable to decrypt secret variable")
8484
}
@@ -123,7 +123,7 @@ func ParseAndImport(ctx context.Context, db gorpmapper.SqlExecutorWithTx, cache
123123
keepOldValue = true
124124
}
125125

126-
kk, err := keys.Parse(db, proj.ID, kname, kval, decryptFunc)
126+
kk, err := keys.Parse(ctx, db, proj.ID, kname, kval, decryptFunc)
127127
if err != nil {
128128
return app, nil, msgList, sdk.ErrorWithFallback(err, sdk.ErrWrongRequest, "unable to parse key %s", kname)
129129
}
@@ -169,7 +169,7 @@ func ParseAndImport(ctx context.Context, db gorpmapper.SqlExecutorWithTx, cache
169169
return app, nil, msgList, sdk.NewErrorFrom(sdk.ErrInvalidApplicationRepoStrategy, "could not import application %s with a connection type ssh without ssh key", app.Name)
170170
}
171171
if eapp.VCSPassword != "" {
172-
clearPWD, err := decryptFunc(db, proj.ID, eapp.VCSPassword)
172+
clearPWD, err := decryptFunc(ctx, db, proj.ID, eapp.VCSPassword)
173173
if err != nil {
174174
return app, nil, msgList, sdk.WrapError(sdk.NewError(sdk.ErrWrongRequest, err), "unable to decrypt vcs password")
175175
}
@@ -207,7 +207,7 @@ func ParseAndImport(ctx context.Context, db gorpmapper.SqlExecutorWithTx, cache
207207
for k, v := range pfConfig {
208208
if v.Value != "" {
209209
if v.Type == sdk.SecretVariable {
210-
clearPWD, err := decryptFunc(db, proj.ID, v.Value)
210+
clearPWD, err := decryptFunc(ctx, db, proj.ID, v.Value)
211211
if err != nil {
212212
return app, nil, nil, sdk.WrapError(sdk.NewError(sdk.ErrWrongRequest, err), "unable to decrypt deployment strategy password")
213213
}

engine/api/application/dao_application_integration_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Test_LoadAllDeploymentAllApps(t *testing.T) {
4040
},
4141
}
4242
test.NoError(t, integration.InsertModel(db, &pf))
43-
defer func() { _ = integration.DeleteModel(db, pf.ID) }()
43+
defer func() { _ = integration.DeleteModel(context.TODO(), db, pf.ID) }()
4444

4545
pp := sdk.ProjectIntegration{
4646
Model: pf,

engine/api/application_deployment_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Test_postApplicationDeploymentStrategyConfigHandler(t *testing.T) {
6464
Deployment: true,
6565
}
6666
test.NoError(t, integration.InsertModel(db, &pf))
67-
defer func() { _ = integration.DeleteModel(db, pf.ID) }()
67+
defer func() { _ = integration.DeleteModel(context.TODO(), db, pf.ID) }()
6868

6969
pp := sdk.ProjectIntegration{
7070
Model: pf,
@@ -187,7 +187,7 @@ func Test_postApplicationDeploymentStrategyConfigHandler_InsertTwoDifferentInteg
187187
Deployment: true,
188188
}
189189
test.NoError(t, integration.InsertModel(db, &pf))
190-
defer func() { _ = integration.DeleteModel(db, pf.ID) }()
190+
defer func() { _ = integration.DeleteModel(context.TODO(), db, pf.ID) }()
191191

192192
pp := sdk.ProjectIntegration{
193193
Model: pf,
@@ -322,7 +322,7 @@ func Test_postApplicationDeploymentStrategyConfigHandlerAsProvider(t *testing.T)
322322
},
323323
}
324324
test.NoError(t, integration.InsertModel(db, &pf))
325-
defer func() { _ = integration.DeleteModel(api.mustDB(), pf.ID) }()
325+
defer func() { _ = integration.DeleteModel(context.TODO(), api.mustDB(), pf.ID) }()
326326

327327
pp := sdk.ProjectIntegration{
328328
Model: pf,

engine/api/application_export.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (api *API) getApplicationExportHandler() service.Handler {
2727
return err
2828
}
2929

30-
app, err := application.Export(api.mustDB(), key, appName, project.EncryptWithBuiltinKey)
30+
app, err := application.Export(ctx, api.mustDB(), key, appName, project.EncryptWithBuiltinKey)
3131
if err != nil {
3232
return sdk.WithStack(err)
3333
}

engine/api/application_import_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
"bytes"
5+
"context"
56
"io/ioutil"
67
"net/http/httptest"
78
"strings"
@@ -654,7 +655,7 @@ func Test_postApplicationImportHandler_ExistingAppWithDeploymentStrategy(t *test
654655
},
655656
}
656657
test.NoError(t, integration.InsertModel(db, &pf))
657-
defer func() { _ = integration.DeleteModel(db, pf.ID) }()
658+
defer func() { _ = integration.DeleteModel(context.TODO(), db, pf.ID) }()
658659

659660
pp := sdk.ProjectIntegration{
660661
Model: pf,
@@ -765,7 +766,7 @@ func Test_postApplicationImportHandler_DontOverrideDeploymentPasswordIfNotGiven(
765766
},
766767
}
767768
test.NoError(t, integration.InsertModel(db, &pf))
768-
defer func() { _ = integration.DeleteModel(db, pf.ID) }()
769+
defer func() { _ = integration.DeleteModel(context.TODO(), db, pf.ID) }()
769770

770771
pp := sdk.ProjectIntegration{
771772
Model: pf,

engine/api/bootstrap/bootstrap.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func InitiliazeDB(ctx context.Context, defaultValues sdk.DefaultValues, DBFunc f
5252
return fmt.Errorf("cannot setup builtin workflow outgoing hook models: %v", err)
5353
}
5454

55-
if err := integration.CreateBuiltinModels(DBFunc()); err != nil {
55+
if err := integration.CreateBuiltinModels(ctx, DBFunc()); err != nil {
5656
return fmt.Errorf("cannot setup integrations: %v", err)
5757
}
5858

engine/api/environment.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func (api *API) updateAsCodeEnvironmentHandler() service.Handler {
320320

321321
u := getAPIConsumer(ctx)
322322
env.ProjectID = proj.ID
323-
envExported, err := environment.ExportEnvironment(tx, env, project.EncryptWithBuiltinKey, fmt.Sprintf("env:%d:%s", envDB.ID, branch))
323+
envExported, err := environment.ExportEnvironment(ctx, tx, env, project.EncryptWithBuiltinKey, fmt.Sprintf("env:%d:%s", envDB.ID, branch))
324324
if err != nil {
325325
return err
326326
}

engine/api/environment/environment_exporter.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ func Export(ctx context.Context, db gorp.SqlExecutor, key string, envName string
3232
}
3333
env.Keys = keys
3434

35-
return ExportEnvironment(db, *env, encryptFunc, fmt.Sprintf("env:%d", env.ID))
35+
return ExportEnvironment(ctx, db, *env, encryptFunc, fmt.Sprintf("env:%d", env.ID))
3636
}
3737

3838
// ExportEnvironment encrypt and export
39-
func ExportEnvironment(db gorp.SqlExecutor, env sdk.Environment, encryptFunc sdk.EncryptFunc, encryptPrefix string) (exportentities.Environment, error) {
39+
func ExportEnvironment(ctx context.Context, db gorp.SqlExecutor, env sdk.Environment, encryptFunc sdk.EncryptFunc, encryptPrefix string) (exportentities.Environment, error) {
4040
var envvars []sdk.EnvironmentVariable
4141
for _, v := range env.Variables {
4242
switch v.Type {
4343
case sdk.KeyVariable:
4444
return exportentities.Environment{}, sdk.NewErrorFrom(sdk.ErrWrongRequest, "unsupported variable %s", v.Name)
4545
case sdk.SecretVariable:
46-
content, err := encryptFunc(db, env.ProjectID, fmt.Sprintf("envID:%d:%s", env.ID, v.Name), v.Value)
46+
content, err := encryptFunc(ctx, db, env.ProjectID, fmt.Sprintf("envID:%d:%s", env.ID, v.Name), v.Value)
4747
if err != nil {
4848
return exportentities.Environment{}, sdk.WrapError(err, "unable to encrypt var for env %d in project %d", env.ID, env.ProjectID)
4949
}
@@ -57,7 +57,7 @@ func ExportEnvironment(db gorp.SqlExecutor, env sdk.Environment, encryptFunc sdk
5757

5858
var keys []exportentities.EncryptedKey
5959
for _, k := range env.Keys {
60-
content, err := encryptFunc(db, env.ProjectID, fmt.Sprintf("envID:%d:%s", env.ID, k.Name), k.Private)
60+
content, err := encryptFunc(ctx, db, env.ProjectID, fmt.Sprintf("envID:%d:%s", env.ID, k.Name), k.Private)
6161
if err != nil {
6262
return exportentities.Environment{}, sdk.WrapError(err, "unable to encrypt key for env %d in project %d", env.ID, env.ProjectID)
6363
}

engine/api/environment/environment_parser.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func ParseAndImport(ctx context.Context, db gorpmapper.SqlExecutorWithTx, proj s
8181
case "":
8282
vtype = sdk.StringVariable
8383
case sdk.SecretVariable:
84-
secret, err := decryptFunc(db, proj.ID, value)
84+
secret, err := decryptFunc(ctx, db, proj.ID, value)
8585
if err != nil {
8686
return env, nil, nil, sdk.WrapError(err, "Unable to decrypt secret variable")
8787
}
@@ -124,7 +124,7 @@ func ParseAndImport(ctx context.Context, db gorpmapper.SqlExecutorWithTx, proj s
124124
keepOldValue = true
125125
}
126126

127-
kk, err := keys.Parse(db, proj.ID, kname, kval, decryptFunc)
127+
kk, err := keys.Parse(ctx, db, proj.ID, kname, kval, decryptFunc)
128128
if err != nil {
129129
return env, nil, nil, sdk.WrapError(err, "Unable to parse key")
130130
}

engine/api/event/event.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func DeleteEventIntegration(eventIntegrationID int64) {
104104
func ResetEventIntegration(ctx context.Context, db gorp.SqlExecutor, eventIntegrationID int64) error {
105105
brokerConnectionKey := strconv.FormatInt(eventIntegrationID, 10)
106106
brokersConnectionCache.Delete(brokerConnectionKey)
107-
projInt, err := integration.LoadProjectIntegrationByIDWithClearPassword(db, eventIntegrationID)
107+
projInt, err := integration.LoadProjectIntegrationByIDWithClearPassword(ctx, db, eventIntegrationID)
108108
if err != nil {
109109
return fmt.Errorf("cannot load project integration id %d and type event: %v", eventIntegrationID, err)
110110
}
@@ -173,7 +173,7 @@ func DequeueEvent(ctx context.Context, db *gorp.DbMap) {
173173
brokerConnectionKey := strconv.FormatInt(eventIntegrationID, 10)
174174
brokerConnection, ok := brokersConnectionCache.Get(brokerConnectionKey)
175175
if !ok {
176-
projInt, err := integration.LoadProjectIntegrationByIDWithClearPassword(db, eventIntegrationID)
176+
projInt, err := integration.LoadProjectIntegrationByIDWithClearPassword(ctx, db, eventIntegrationID)
177177
if err != nil {
178178
log.Error(ctx, "Event.DequeueEvent> Cannot load project integration for project %s and id %d and type event: %v", e.ProjectKey, eventIntegrationID, err)
179179
continue

engine/api/group/project_group.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func DeleteLinksGroupProjectForProjectID(db gorp.SqlExecutor, projectID int64) e
5656
}
5757

5858
// LoadGroupsIntoProject retrieves all groups related to project
59-
func LoadGroupsIntoProject(db gorp.SqlExecutor, proj *sdk.Project) error {
60-
links, err := LoadLinksGroupProjectForProjectIDs(context.Background(), db, []int64{proj.ID})
59+
func LoadGroupsIntoProject(ctx context.Context, db gorp.SqlExecutor, proj *sdk.Project) error {
60+
links, err := LoadLinksGroupProjectForProjectIDs(ctx, db, []int64{proj.ID})
6161
if err != nil {
6262
return err
6363
}
@@ -69,7 +69,7 @@ func LoadGroupsIntoProject(db gorp.SqlExecutor, proj *sdk.Project) error {
6969
groupIDsMap[l.GroupID] = l.Role
7070
}
7171

72-
groups, err := LoadAllByIDs(context.Background(), db, groupIDs)
72+
groups, err := LoadAllByIDs(ctx, db, groupIDs)
7373
if err != nil {
7474
return err
7575
}

engine/api/grpc_plugin.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (api *API) postGRPCluginHandler() service.Handler {
3939

4040
// a plugin can be attached to a integration model OR not, for "action plugin"
4141
if p.Integration != "" {
42-
integrationModel, err := integration.LoadModelByName(api.mustDB(), p.Integration)
42+
integrationModel, err := integration.LoadModelByName(ctx, api.mustDB(), p.Integration)
4343
if err != nil {
4444
return err
4545
}
@@ -127,7 +127,7 @@ func (api *API) putGRPCluginHandler() service.Handler {
127127

128128
// a plugin can be attached to a integration model OR not, for "action plugin"
129129
if p.Integration != "" {
130-
integrationModel, err := integration.LoadModelByName(api.mustDB(), p.Integration)
130+
integrationModel, err := integration.LoadModelByName(ctx, api.mustDB(), p.Integration)
131131
if err != nil {
132132
return err
133133
}

engine/api/integration.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (api *API) getIntegrationModelHandler() service.Handler {
3232
vars := mux.Vars(r)
3333
name := vars["name"]
3434

35-
p, err := integration.LoadModelByName(api.mustDB(), name)
35+
p, err := integration.LoadModelByName(ctx, api.mustDB(), name)
3636
if err != nil {
3737
return sdk.WrapError(err, "Cannot get integration model")
3838
}
@@ -101,7 +101,7 @@ func (api *API) putIntegrationModelHandler() service.Handler {
101101
}
102102
defer tx.Rollback() // nolint
103103

104-
old, err := integration.LoadModelByName(tx, name)
104+
old, err := integration.LoadModelByName(ctx, tx, name)
105105
if err != nil {
106106
return sdk.WrapError(err, "Unable to load model")
107107
}
@@ -115,7 +115,7 @@ func (api *API) putIntegrationModelHandler() service.Handler {
115115
}
116116

117117
m.ID = old.ID
118-
if err := integration.UpdateModel(tx, m); err != nil {
118+
if err := integration.UpdateModel(ctx, tx, m); err != nil {
119119
return err
120120
}
121121

@@ -174,7 +174,7 @@ func propagatePublicIntegrationModelOnProject(ctx context.Context, db gorpmapper
174174

175175
for pfName, immutableCfg := range m.PublicConfigurations {
176176
cfg := immutableCfg.Clone()
177-
oldPP, _ := integration.LoadProjectIntegrationByNameWithClearPassword(db, p.Key, pfName)
177+
oldPP, _ := integration.LoadProjectIntegrationByNameWithClearPassword(ctx, db, p.Key, pfName)
178178
if oldPP.ID == 0 {
179179
pp := sdk.ProjectIntegration{
180180
Model: m,
@@ -199,7 +199,7 @@ func propagatePublicIntegrationModelOnProject(ctx context.Context, db gorpmapper
199199
ProjectID: p.ID,
200200
}
201201
oldPP.Config = m.DefaultConfig
202-
if err := integration.UpdateIntegration(db, pp); err != nil {
202+
if err := integration.UpdateIntegration(ctx, db, pp); err != nil {
203203
return err
204204
}
205205
event.PublishUpdateProjectIntegration(ctx, &p, oldPP, pp, u)
@@ -218,12 +218,12 @@ func (api *API) deleteIntegrationModelHandler() service.Handler {
218218
}
219219
defer tx.Rollback() // nolint
220220

221-
old, err := integration.LoadModelByName(tx, name)
221+
old, err := integration.LoadModelByName(ctx, tx, name)
222222
if err != nil {
223223
return err
224224
}
225225

226-
if err := integration.DeleteModel(tx, old.ID); err != nil {
226+
if err := integration.DeleteModel(ctx, tx, old.ID); err != nil {
227227
return sdk.WithStack(err)
228228
}
229229

0 commit comments

Comments
 (0)