Skip to content

Commit 0eedf85

Browse files
authored
feat: compute default values for template parameter (#5092)
1 parent dc5da00 commit 0eedf85

Some content is hidden

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

49 files changed

+496
-252
lines changed

cli/cdsctl/application_key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var applicationKeyCreateCmd = cli.Command{
3838
func applicationCreateKeyRun(v cli.Values) error {
3939
key := &sdk.ApplicationKey{
4040
Name: v.GetString("key-name"),
41-
Type: v.GetString("key-type"),
41+
Type: sdk.KeyType(v.GetString("key-type")),
4242
}
4343
if err := client.ApplicationKeyCreate(v.GetString(_ProjectKey), v.GetString(_ApplicationName), key); err != nil {
4444
return err

cli/cdsctl/environment_key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var environmentKeyCreateCmd = cli.Command{
3838
func environmentCreateKeyRun(v cli.Values) error {
3939
key := &sdk.EnvironmentKey{
4040
Name: v.GetString("key-name"),
41-
Type: v.GetString("key-type"),
41+
Type: sdk.KeyType(v.GetString("key-type")),
4242
}
4343
if err := client.EnvironmentKeyCreate(v.GetString(_ProjectKey), v.GetString("env-name"), key); err != nil {
4444
return err

cli/cdsctl/project_key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var projectKeyCreateCmd = cli.Command{
3737
func projectCreateKeyRun(v cli.Values) error {
3838
key := &sdk.ProjectKey{
3939
Name: v.GetString("key-name"),
40-
Type: v.GetString("key-type"),
40+
Type: sdk.KeyType(v.GetString("key-type")),
4141
}
4242
if err := client.ProjectKeyCreate(v.GetString(_ProjectKey), key); err != nil {
4343
return err

cli/cdsctl/template_apply.go

+32
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,23 @@ func templateApplyRun(v cli.Values) error {
200200
}
201201

202202
var listRepositories []string
203+
var listSSHKeys []string
204+
var listPGPKeys []string
203205
var localRepoPath string
204206

205207
// if there are params of type repository in list of params to fill prepare
206208
// the list of repositories for project
207209
var withRepository bool
210+
var withKey bool
208211
for _, p := range wt.Parameters {
209212
if _, ok := params[p.Key]; !ok {
210213
if p.Type == sdk.ParameterTypeRepository {
211214
withRepository = true
215+
}
216+
if p.Type == sdk.ParameterTypeSSHKey || p.Type == sdk.ParameterTypePGPKey {
217+
withKey = true
218+
}
219+
if withRepository && withKey {
212220
break
213221
}
214222
}
@@ -234,6 +242,20 @@ func templateApplyRun(v cli.Values) error {
234242
}
235243
}
236244
}
245+
if withKey {
246+
pKeys, err := client.ProjectKeysList(projectKey)
247+
if err != nil {
248+
return err
249+
}
250+
for _, k := range pKeys {
251+
switch k.Type {
252+
case sdk.KeyTypeSSH:
253+
listSSHKeys = append(listSSHKeys, k.Name)
254+
case sdk.KeyTypePGP:
255+
listPGPKeys = append(listPGPKeys, k.Name)
256+
}
257+
}
258+
}
237259

238260
// for each param not already fill ask for the value
239261
for _, p := range wt.Parameters {
@@ -249,6 +271,16 @@ func templateApplyRun(v cli.Values) error {
249271
selected := cli.AskChoice(label, listRepositories...)
250272
choice = listRepositories[selected]
251273
}
274+
case sdk.ParameterTypeSSHKey:
275+
if len(listSSHKeys) > 0 {
276+
selected := cli.AskChoice(label, listSSHKeys...)
277+
choice = listSSHKeys[selected]
278+
}
279+
case sdk.ParameterTypePGPKey:
280+
if len(listPGPKeys) > 0 {
281+
selected := cli.AskChoice(label, listPGPKeys...)
282+
choice = listPGPKeys[selected]
283+
}
252284
case sdk.ParameterTypeBoolean:
253285
choice = fmt.Sprintf("%t", cli.AskConfirm(fmt.Sprintf("Set value to 'true' for param '%s'", p.Key)))
254286
}

cli/cdsctl/template_bulk.go

+48-23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/ovh/cds/cli"
1414
"github.com/ovh/cds/sdk"
15+
"github.com/ovh/cds/sdk/cdsclient"
1516
"github.com/ovh/cds/sdk/exportentities"
1617
)
1718

@@ -387,6 +388,8 @@ func templateBulkRun(v cli.Values) error {
387388
mprojects[wti.Project.Key] = wti.Project
388389
}
389390
projectRepositories := make(map[string][]string)
391+
projectSSHKeys := make(map[string][]string)
392+
projectPGPKeys := make(map[string][]string)
390393

391394
for operationKey, operation := range moperations {
392395
// check if some params are missing for current operation
@@ -400,14 +403,14 @@ func templateBulkRun(v cli.Values) error {
400403

401404
if paramMissing {
402405
// get project from map if exists else from api
403-
if _, ok := mprojects[operationKey]; !ok {
404-
p, err := client.ProjectGet(operation.Request.ProjectKey)
406+
if _, ok := mprojects[operation.Request.ProjectKey]; !ok {
407+
p, err := client.ProjectGet(operation.Request.ProjectKey, cdsclient.WithKeys())
405408
if err != nil {
406409
return err
407410
}
408411
mprojects[p.Key] = p
409412
}
410-
project := mprojects[operationKey]
413+
project := mprojects[operation.Request.ProjectKey]
411414

412415
// for each param not already in previous request ask for the value
413416
for _, p := range wt.Parameters {
@@ -416,40 +419,62 @@ func templateBulkRun(v cli.Values) error {
416419

417420
var value string
418421
switch p.Type {
419-
case sdk.ParameterTypeRepository:
420-
// get the project and its repositories if not already loaded
421-
if _, ok := projectRepositories[project.Key]; !ok {
422-
for _, vcs := range project.VCSServers {
423-
rs, err := client.RepositoriesList(project.Key, vcs.Name, false)
424-
if err != nil {
425-
return err
422+
case sdk.ParameterTypeRepository, sdk.ParameterTypeSSHKey, sdk.ParameterTypePGPKey:
423+
var options []string
424+
if p.Type == sdk.ParameterTypeRepository {
425+
// get the project and its repositories if not already loaded
426+
if _, ok := projectRepositories[project.Key]; !ok {
427+
for _, vcs := range project.VCSServers {
428+
rs, err := client.RepositoriesList(project.Key, vcs.Name, false)
429+
if err != nil {
430+
return err
431+
}
432+
for _, r := range rs {
433+
projectRepositories[project.Key] = append(projectRepositories[project.Key],
434+
fmt.Sprintf("%s/%s", vcs.Name, r.Slug))
435+
}
426436
}
427-
for _, r := range rs {
428-
projectRepositories[project.Key] = append(projectRepositories[project.Key],
429-
fmt.Sprintf("%s/%s", vcs.Name, r.Slug))
437+
}
438+
options = projectRepositories[project.Key]
439+
} else if p.Type == sdk.ParameterTypeSSHKey {
440+
if _, ok := projectSSHKeys[project.Key]; !ok {
441+
var sshKeys []string
442+
for _, k := range project.Keys {
443+
if k.Type == sdk.KeyTypeSSH {
444+
sshKeys = append(sshKeys, k.Name)
445+
}
446+
}
447+
projectSSHKeys[project.Key] = sshKeys
448+
}
449+
options = projectSSHKeys[project.Key]
450+
} else if p.Type == sdk.ParameterTypePGPKey {
451+
if _, ok := projectPGPKeys[project.Key]; !ok {
452+
var pgpKeys []string
453+
for _, k := range project.Keys {
454+
if k.Type == sdk.KeyTypePGP {
455+
pgpKeys = append(pgpKeys, k.Name)
456+
}
430457
}
458+
projectPGPKeys[project.Key] = pgpKeys
431459
}
460+
options = projectPGPKeys[project.Key]
432461
}
433462

434-
// ask to choose a repository, if only one ask to, if no repo found ask for value
435-
lengthRepo := len(projectRepositories[project.Key])
436-
if lengthRepo > 1 {
437-
if err := survey.AskOne(&survey.Select{
438-
Message: label,
439-
Options: projectRepositories[project.Key],
440-
}, &value, nil); err != nil {
463+
// ask to choose an option, if only one ask to, if no options found ask for value
464+
if len(options) > 1 {
465+
if err := survey.AskOne(&survey.Select{Message: label, Options: options}, &value, nil); err != nil {
441466
return err
442467
}
443-
} else if lengthRepo == 1 {
468+
} else if len(options) == 1 {
444469
var result bool
445470
if err := survey.AskOne(&survey.Confirm{
446-
Message: fmt.Sprintf("Set value to '%s' for param '%s' on '%s'", projectRepositories[project.Key][0], p.Key, operationKey),
471+
Message: fmt.Sprintf("Set value to '%s' for param '%s' on '%s'", options[0], p.Key, operationKey),
447472
Default: true,
448473
}, &result, nil); err != nil {
449474
return err
450475
}
451476
if result {
452-
value = projectRepositories[project.Key][0]
477+
value = options[0]
453478
}
454479
}
455480
if value == "" {

cli/cdsctl/workflow_init.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func craftApplicationFile(proj *sdk.Project, existingApp *sdk.Application, fetch
305305
}
306306
// The key is unknown, we have to create a new one
307307
app.VCSPGPKey = defaultPGPKey
308-
app.Keys[app.VCSPGPKey] = exportentities.KeyValue{Type: sdk.KeyTypePGP}
308+
app.Keys[app.VCSPGPKey] = exportentities.KeyValue{Type: string(sdk.KeyTypePGP)}
309309

310310
fmt.Printf(" * using PGP Key %s/%s for application VCS settings", cli.Magenta(proj.Key), cli.Magenta(app.VCSPGPKey))
311311
fmt.Println()
@@ -322,7 +322,7 @@ func craftApplicationFile(proj *sdk.Project, existingApp *sdk.Application, fetch
322322
app.VCSPGPKey = opts[selected]
323323
} else {
324324
app.VCSPGPKey = fmt.Sprintf("app-pgp-%s", repoManagerName)
325-
app.Keys[app.VCSPGPKey] = exportentities.KeyValue{Type: sdk.KeyTypePGP}
325+
app.Keys[app.VCSPGPKey] = exportentities.KeyValue{Type: string(sdk.KeyTypePGP)}
326326
}
327327
} else if len(projectPGPKeys) == 1 {
328328
app.VCSPGPKey = projectPGPKeys[0].Name
@@ -344,7 +344,7 @@ func craftApplicationFile(proj *sdk.Project, existingApp *sdk.Application, fetch
344344
}
345345

346346
app.VCSSSHKey = defaultSSHKey
347-
app.Keys[app.VCSSSHKey] = exportentities.KeyValue{Type: sdk.KeyTypeSSH}
347+
app.Keys[app.VCSSSHKey] = exportentities.KeyValue{Type: string(sdk.KeyTypeSSH)}
348348

349349
fmt.Printf(" * using SSH Key %s/%s for application VCS settings", cli.Magenta(proj.Key), cli.Magenta(app.VCSSSHKey))
350350
fmt.Println()
@@ -362,7 +362,7 @@ func craftApplicationFile(proj *sdk.Project, existingApp *sdk.Application, fetch
362362
app.VCSSSHKey = opts[selected]
363363
} else {
364364
app.VCSSSHKey = fmt.Sprintf("app-ssh-%s", repoManagerName)
365-
app.Keys[app.VCSSSHKey] = exportentities.KeyValue{Type: sdk.KeyTypePGP}
365+
app.Keys[app.VCSSSHKey] = exportentities.KeyValue{Type: string(sdk.KeyTypePGP)}
366366
}
367367
} else if len(projectSSHKeys) == 1 {
368368
app.VCSSSHKey = projectSSHKeys[0].Name
@@ -438,11 +438,7 @@ func workflowInitRun(c cli.Values) error {
438438
}
439439

440440
// Check if the project is linked to a repository
441-
proj, err := client.ProjectGet(pkey, func(r *http.Request) {
442-
q := r.URL.Query()
443-
q.Set("withKeys", "true")
444-
r.URL.RawQuery = q.Encode()
445-
})
441+
proj, err := client.ProjectGet(pkey, cdsclient.WithKeys())
446442
if err != nil {
447443
return fmt.Errorf("unable to get project: %v", err)
448444
}

engine/api/application/application_exporter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func ExportApplication(db gorp.SqlExecutor, app sdk.Application, encryptFunc sdk
5656
return exportentities.Application{}, sdk.WrapError(err, "unable to encrypt key")
5757
}
5858
ek := exportentities.EncryptedKey{
59-
Type: k.Type,
59+
Type: string(k.Type),
6060
Name: k.Name,
6161
Content: content,
6262
}

engine/api/application/application_importer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func Import(ctx context.Context, db gorp.SqlExecutor, store cache.Store, proj sd
8787
return sdk.WrapError(err, "Unable to insert key %s", k.Name)
8888
}
8989
if msgChan != nil {
90-
msgChan <- sdk.NewMessage(sdk.MsgAppKeyCreated, strings.ToUpper(k.Type), k.Name, app.Name)
90+
msgChan <- sdk.NewMessage(sdk.MsgAppKeyCreated, strings.ToUpper(string(k.Type)), k.Name, app.Name)
9191
}
9292
}
9393

engine/api/application/application_parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func ParseAndImport(ctx context.Context, db gorp.SqlExecutor, cache cache.Store,
4444

4545
//If the application exist and we don't want to force, raise an error
4646
if oldApp != nil && !opts.Force {
47-
return nil, msgList, sdk.ErrApplicationExist
47+
return nil, msgList, sdk.WithStack(sdk.ErrApplicationExist)
4848
}
4949

5050
if oldApp != nil && oldApp.FromRepository != "" && opts.FromRepository != oldApp.FromRepository {

engine/api/application_import_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImpo
421421
test.Equal(t, "MySecretValue", app.Variables[0].Value)
422422
test.Equal(t, 2, len(app.Keys))
423423

424-
mKeys := make(map[string]sdk.ApplicationKey, 2)
424+
mKeys := make(map[sdk.KeyType]sdk.ApplicationKey, 2)
425425
mKeys[app.Keys[0].Type] = app.Keys[0]
426426
mKeys[app.Keys[1].Type] = app.Keys[1]
427427
rssh, ok := mKeys["ssh"]
@@ -493,7 +493,7 @@ func Test_postApplicationImportHandler_NewAppFromYAMLWithKeysAndSecretsAndReImpo
493493
test.Equal(t, 1, len(app.Variables))
494494
test.Equal(t, "MySecretValue", app.Variables[0].Value)
495495
test.Equal(t, 2, len(app.Keys))
496-
mKeys = make(map[string]sdk.ApplicationKey, 2)
496+
mKeys = make(map[sdk.KeyType]sdk.ApplicationKey, 2)
497497
mKeys[app.Keys[0].Type] = app.Keys[0]
498498
mKeys[app.Keys[1].Type] = app.Keys[1]
499499
rssh, ok = mKeys["ssh"]

engine/api/ascode.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,15 @@ func (api *API) postPerformImportAsCodeHandler() service.Handler {
158158

159159
consumer := getAPIConsumer(ctx)
160160

161-
var mods []workflowtemplate.TemplateRequestModifierFunc
161+
mods := []workflowtemplate.TemplateRequestModifierFunc{
162+
workflowtemplate.TemplateRequestModifiers.DefaultKeys(*proj),
163+
}
162164
if !opt.IsDefaultBranch {
163165
mods = append(mods, workflowtemplate.TemplateRequestModifiers.Detached)
164166
}
167+
if opt.FromRepository != "" {
168+
mods = append(mods, workflowtemplate.TemplateRequestModifiers.DefaultNameAndRepositories(ctx, api.mustDB(), api.Cache, *proj, opt.FromRepository))
169+
}
165170
wti, err := workflowtemplate.CheckAndExecuteTemplate(ctx, api.mustDB(), *consumer, *proj, &data, mods...)
166171
if err != nil {
167172
return err

engine/api/environment/environment_exporter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func ExportEnvironment(db gorp.SqlExecutor, env sdk.Environment, encryptFunc sdk
6262
return exportentities.Environment{}, sdk.WrapError(err, "Unable to encrypt key")
6363
}
6464
ek := exportentities.EncryptedKey{
65-
Type: k.Type,
65+
Type: string(k.Type),
6666
Name: k.Name,
6767
Content: content,
6868
}

engine/api/environment/environment_importer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Import(db gorp.SqlExecutor, proj sdk.Project, env *sdk.Environment, msgChan
5353
return sdk.WrapError(err, "Unable to insert key %s", k.Name)
5454
}
5555
if msgChan != nil {
56-
msgChan <- sdk.NewMessage(sdk.MsgEnvironmentKeyCreated, strings.ToUpper(k.Type), k.Name, env.Name)
56+
msgChan <- sdk.NewMessage(sdk.MsgEnvironmentKeyCreated, strings.ToUpper(string(k.Type)), k.Name, env.Name)
5757
}
5858
}
5959

engine/api/event/publish_project.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ func PublishAddProjectKey(ctx context.Context, p *sdk.Project, k sdk.ProjectKey,
128128

129129
// PublishDeleteProjectKey publishes an event on deleting a project key
130130
func PublishDeleteProjectKey(ctx context.Context, p *sdk.Project, k sdk.ProjectKey, u sdk.Identifiable) {
131-
if sdk.NeedPlaceholder(k.Type) {
132-
k.Private = sdk.PasswordPlaceholder
133-
}
131+
k.Private = sdk.PasswordPlaceholder
134132
e := sdk.EventProjectKeyDelete{
135133
Key: k,
136134
}

engine/api/keys/parse.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type DecryptFunc func(gorp.SqlExecutor, int64, string) (string, error)
1515
// Parse and decrypts an exported key
1616
func Parse(db gorp.SqlExecutor, projID int64, kname string, kval exportentities.KeyValue, decryptFunc DecryptFunc) (*sdk.Key, error) {
1717
k := new(sdk.Key)
18-
k.Type = kval.Type
18+
k.Type = sdk.KeyType(kval.Type)
1919
k.Name = kname
2020
if kval.Value != "" {
2121
privateKey, err := decryptFunc(db, projID, kval.Value)

engine/api/migrate/refactor_app_keys.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func refactorApplicationKeys(ctx context.Context, db *gorp.DbMap, id int64) erro
110110
if err != nil {
111111
return err
112112
}
113-
k.Type = s
113+
k.Type = sdk.KeyType(s)
114114

115115
s, err = stringIfValid("public", public)
116116
if err != nil {

engine/api/migrate/refactor_env_keys.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func refactorEnvironmentKeys(ctx context.Context, db *gorp.DbMap, id int64) erro
110110
if err != nil {
111111
return err
112112
}
113-
k.Type = s
113+
k.Type = sdk.KeyType(s)
114114

115115
s, err = stringIfValid("public", public)
116116
if err != nil {

engine/api/migrate/refactor_proj_keys.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func refactorProjectKeys(ctx context.Context, db *gorp.DbMap, id int64) error {
110110
if err != nil {
111111
return err
112112
}
113-
k.Type = s
113+
k.Type = sdk.KeyType(s)
114114

115115
s, err = stringIfValid("public", public)
116116
if err != nil {

0 commit comments

Comments
 (0)