-
Notifications
You must be signed in to change notification settings - Fork 583
/
parameters.go
369 lines (313 loc) · 13.1 KB
/
parameters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package generator
import (
"fmt"
"path"
"sort"
"strings"
"github.com/SAP/jenkins-library/pkg/config"
)
// Replaces the Parameters placeholder with the content from the yaml
func createParametersSection(stepData *config.StepData) string {
var parameters = "## Parameters\n\n"
// sort parameters alphabetically with mandatory parameters first
sortStepParameters(stepData, true)
parameters += "### Overview - Step\n\n"
parameters += createParameterOverview(stepData, false)
parameters += "### Overview - Execution Environment\n\n"
parameters += "!!! note \"Orchestrator-specific only\"\n\n These parameters are relevant for orchestrator usage and not considered when using the command line option.\n\n"
parameters += createParameterOverview(stepData, true)
// sort parameters alphabetically
sortStepParameters(stepData, false)
parameters += "### Details\n\n"
parameters += createParameterDetails(stepData)
return parameters
}
func parameterMandatoryInformation(param config.StepParameters, furtherInfo string) (mandatory bool, mandatoryString string, mandatoryInfo string) {
mandatory = param.Mandatory
mandatoryInfo = furtherInfo
mandatoryIf := param.MandatoryIf
if len(mandatoryIf) > 0 {
mandatory = true
if len(mandatoryInfo) > 0 {
mandatoryInfo += "<br />"
}
furtherInfoConditions := []string{"mandatory in case of:"}
for _, mandatoryCondition := range mandatoryIf {
furtherInfoConditions = append(furtherInfoConditions, fmt.Sprintf("- [`%v`](#%v)=`%v`", mandatoryCondition.Name, strings.ToLower(mandatoryCondition.Name), mandatoryCondition.Value))
}
mandatoryInfo += strings.Join(furtherInfoConditions, "<br />")
}
mandatoryString = "**yes**"
if len(mandatoryInfo) > 0 {
mandatoryString = "**(yes)**"
}
return
}
func createParameterOverview(stepData *config.StepData, executionEnvironment bool) string {
var table = "| Name | Mandatory | Additional information |\n"
table += "| ---- | --------- | ---------------------- |\n"
for _, param := range stepData.Spec.Inputs.Parameters {
furtherInfo, err := parameterFurtherInfo(param.Name, stepData, executionEnvironment)
if err == nil {
var mandatory bool
var mandatoryString string
mandatory, mandatoryString, furtherInfo = parameterMandatoryInformation(param, furtherInfo)
table += fmt.Sprintf("| [%v](#%v) | %v | %v |\n", param.Name, strings.ToLower(param.Name), ifThenElse(mandatory, mandatoryString, "no"), furtherInfo)
}
}
table += "\n"
return table
}
func parameterFurtherInfo(paramName string, stepData *config.StepData, executionEnvironment bool) (string, error) {
// handle general parameters
// ToDo: add special handling once we have more than one general parameter to consider
if paramName == "verbose" {
return checkParameterInfo("activates debug output", true, executionEnvironment)
}
if paramName == "script" {
return checkParameterInfo("[![Jenkins only](https://img.shields.io/badge/-Jenkins%20only-yellowgreen)](#) reference to Jenkins main pipeline script", true, executionEnvironment)
}
// handle non-step parameters (e.g. Jenkins-specific parameters as well as execution environment parameters)
jenkinsParams := []string{"containerCommand", "containerName", "containerShell", "dockerVolumeBind", "dockerWorkspace", "sidecarReadyCommand", "sidecarWorkspace", "stashContent"}
if !contains(stepParameterNames, paramName) {
for _, secret := range stepData.Spec.Inputs.Secrets {
if paramName == secret.Name && secret.Type == "jenkins" {
return checkParameterInfo("[![Jenkins only](https://img.shields.io/badge/-Jenkins%20only-yellowgreen)](#) id of credentials ([using credentials](https://www.jenkins.io/doc/book/using/using-credentials/))", true, executionEnvironment)
}
}
if contains(jenkinsParams, paramName) {
return checkParameterInfo("[![Jenkins only](https://img.shields.io/badge/-Jenkins%20only-yellowgreen)](#)", false, executionEnvironment)
}
return checkParameterInfo("", false, executionEnvironment)
}
// handle step-parameters (incl. secrets)
for _, param := range stepData.Spec.Inputs.Parameters {
if paramName == param.Name {
furtherInfo := ""
if param.DeprecationMessage != "" {
furtherInfo += "![deprecated](https://img.shields.io/badge/-deprecated-red)"
}
if param.Secret {
secretInfo := "[![Secret](https://img.shields.io/badge/-Secret-yellowgreen)](#) pass via ENV or Jenkins credentials"
if param.GetReference("vaultSecret") != nil || param.GetReference("vaultSecretFile") != nil {
secretInfo = " [![Vault](https://img.shields.io/badge/-Vault-lightgrey)](#) [![Secret](https://img.shields.io/badge/-Secret-yellowgreen)](/) pass via ENV, Vault or Jenkins credentials"
}
for _, res := range param.ResourceRef {
if res.Type == "secret" {
secretInfo += fmt.Sprintf(" ([`%v`](#%v))", res.Name, strings.ToLower(res.Name))
}
}
return checkParameterInfo(furtherInfo+secretInfo, true, executionEnvironment)
}
return checkParameterInfo(furtherInfo, true, executionEnvironment)
}
}
return checkParameterInfo("", true, executionEnvironment)
}
func checkParameterInfo(furtherInfo string, stepParam bool, executionEnvironment bool) (string, error) {
if stepParam && !executionEnvironment || !stepParam && executionEnvironment {
return furtherInfo, nil
}
if executionEnvironment {
return "", fmt.Errorf("step parameter not relevant as execution environment parameter")
}
return "", fmt.Errorf("execution environment parameter not relevant as step parameter")
}
func createParameterDetails(stepData *config.StepData) string {
details := ""
//jenkinsParameters := append(jenkinsParameters(stepData), "script")
for _, param := range stepData.Spec.Inputs.Parameters {
details += fmt.Sprintf("#### %v\n\n", param.Name)
if !contains(stepParameterNames, param.Name) {
details += "**Jenkins-specific:** Used for proper environment setup.\n\n"
}
if len(param.LongDescription) > 0 {
details += param.LongDescription + "\n\n"
} else {
details += param.Description + "\n\n"
}
details += "[back to overview](#parameters)\n\n"
details += "| Scope | Details |\n"
details += "| ---- | --------- |\n"
if param.DeprecationMessage != "" {
details += fmt.Sprintf("| Deprecated | %v |\n", param.DeprecationMessage)
}
details += fmt.Sprintf("| Aliases | %v |\n", aliasList(param.Aliases))
details += fmt.Sprintf("| Type | `%v` |\n", param.Type)
mandatory, mandatoryString, furtherInfo := parameterMandatoryInformation(param, "")
if mandatory && len(furtherInfo) > 0 {
mandatoryString = furtherInfo
}
details += fmt.Sprintf("| Mandatory | %v |\n", ifThenElse(mandatory, mandatoryString, "no"))
details += fmt.Sprintf("| Default | %v |\n", formatDefault(param, stepParameterNames))
if param.PossibleValues != nil {
details += fmt.Sprintf("| Possible values | %v |\n", possibleValueList(param.PossibleValues))
}
details += fmt.Sprintf("| Secret | %v |\n", ifThenElse(param.Secret, "**yes**", "no"))
details += fmt.Sprintf("| Configuration scope | %v |\n", scopeDetails(param.Scope))
details += fmt.Sprintf("| Resource references | %v |\n", resourceReferenceDetails(param.ResourceRef))
details += "\n\n"
}
for _, secret := range stepData.Spec.Inputs.Secrets {
details += fmt.Sprintf("#### %v\n\n", secret.Name)
if !contains(stepParameterNames, secret.Name) {
details += "**Jenkins-specific:** Used for proper environment setup. See *[using credentials](https://www.jenkins.io/doc/book/using/using-credentials/)* for details.\n\n"
}
details += secret.Description + "\n\n"
details += "[back to overview](#parameters)\n\n"
details += "| Scope | Details |\n"
details += "| ---- | --------- |\n"
details += fmt.Sprintf("| Aliases | %v |\n", aliasList(secret.Aliases))
details += fmt.Sprintf("| Type | `%v` |\n", "string")
details += fmt.Sprintf("| Configuration scope | %v |\n", scopeDetails([]string{"PARAMETERS", "GENERAL", "STEPS", "STAGES"}))
details += "\n\n"
}
return details
}
func formatDefault(param config.StepParameters, stepParameterNames []string) string {
if param.Default == nil {
// Return environment variable for all step parameters (not for Jenkins-specific parameters) in case no default is available
if contains(stepParameterNames, param.Name) {
return fmt.Sprintf("`$PIPER_%v` (if set)", param.Name)
}
return ""
}
//first consider conditional defaults
switch v := param.Default.(type) {
case []conditionDefault:
defaults := []string{}
for _, condDef := range v {
//ToDo: add type-specific handling of default
if len(condDef.key) > 0 && len(condDef.value) > 0 {
defaults = append(defaults, fmt.Sprintf("%v=`%v`: `%v`", condDef.key, condDef.value, condDef.def))
} else {
// containers with no condition will only hold def
defaults = append(defaults, fmt.Sprintf("`%v`", condDef.def))
}
}
return strings.Join(defaults, "<br />")
case []interface{}:
// handle for example stashes which possibly contain a mixture of fix and conditional values
defaults := []string{}
for _, def := range v {
if condDef, ok := def.(conditionDefault); ok {
defaults = append(defaults, fmt.Sprintf("%v=`%v`: `%v`", condDef.key, condDef.value, condDef.def))
} else {
defaults = append(defaults, fmt.Sprintf("- `%v`", def))
}
}
return strings.Join(defaults, "<br />")
case map[string]string:
defaults := []string{}
for key, def := range v {
defaults = append(defaults, fmt.Sprintf("`%v`: `%v`", key, def))
}
return strings.Join(defaults, "<br />")
case string:
if len(v) == 0 {
return "`''`"
}
return fmt.Sprintf("`%v`", v)
default:
return fmt.Sprintf("`%v`", param.Default)
}
}
func aliasList(aliases []config.Alias) string {
switch len(aliases) {
case 0:
return "-"
case 1:
alias := fmt.Sprintf("`%v`", aliases[0].Name)
if aliases[0].Deprecated {
alias += " (**deprecated**)"
}
return alias
default:
aList := make([]string, len(aliases))
for i, alias := range aliases {
aList[i] = fmt.Sprintf("- `%v`", alias.Name)
if alias.Deprecated {
aList[i] += " (**deprecated**)"
}
}
return strings.Join(aList, "<br />")
}
}
func possibleValueList(possibleValues []interface{}) string {
if len(possibleValues) == 0 {
return ""
}
pList := make([]string, len(possibleValues))
for i, possibleValue := range possibleValues {
pList[i] = fmt.Sprintf("- `%v`", fmt.Sprint(possibleValue))
}
return strings.Join(pList, "<br />")
}
func scopeDetails(scope []string) string {
scopeDetails := "<ul>"
scopeDetails += fmt.Sprintf("<li>%v parameter</li>", ifThenElse(contains(scope, "PARAMETERS"), "☒", "☐"))
scopeDetails += fmt.Sprintf("<li>%v general</li>", ifThenElse(contains(scope, "GENERAL"), "☒", "☐"))
scopeDetails += fmt.Sprintf("<li>%v steps</li>", ifThenElse(contains(scope, "STEPS"), "☒", "☐"))
scopeDetails += fmt.Sprintf("<li>%v stages</li>", ifThenElse(contains(scope, "STAGES"), "☒", "☐"))
scopeDetails += "</ul>"
return scopeDetails
}
func resourceReferenceDetails(resourceRef []config.ResourceReference) string {
if len(resourceRef) == 0 {
return "none"
}
resourceDetails := ""
for _, resource := range resourceRef {
if resource.Name == "commonPipelineEnvironment" {
resourceDetails += "_commonPipelineEnvironment_:<br />"
resourceDetails += fmt.Sprintf(" reference to: `%v`<br />", resource.Param)
continue
}
if resource.Type == "secret" {
resourceDetails += "Jenkins credential id:<br />"
for i, alias := range resource.Aliases {
if i == 0 {
resourceDetails += " aliases:<br />"
}
resourceDetails += fmt.Sprintf(" - `%v`%v<br />", alias.Name, ifThenElse(alias.Deprecated, " (**Deprecated**)", ""))
}
resourceDetails += fmt.Sprintf(" id: [`%v`](#%v)<br />", resource.Name, strings.ToLower(resource.Name))
if resource.Param != "" {
resourceDetails += fmt.Sprintf(" reference to: `%v`<br />", resource.Param)
}
continue
}
resourceDetails = addVaultResourceDetails(resource, resourceDetails)
}
return resourceDetails
}
func addVaultResourceDetails(resource config.ResourceReference, resourceDetails string) string {
if resource.Type == "vaultSecret" || resource.Type == "vaultSecretFile" {
resourceDetails += "<br/>Vault paths: <br />"
resourceDetails += "<ul>"
for _, rootPath := range config.VaultRootPaths {
resourceDetails += fmt.Sprintf("<li>`%s`</li>", path.Join(rootPath, resource.Default))
}
resourceDetails += "</ul>"
}
return resourceDetails
}
func sortStepParameters(stepData *config.StepData, considerMandatory bool) {
if stepData.Spec.Inputs.Parameters != nil {
parameters := stepData.Spec.Inputs.Parameters
if considerMandatory {
sort.SliceStable(parameters[:], func(i, j int) bool {
if (parameters[i].Mandatory || len(parameters[i].MandatoryIf) > 0) == (parameters[j].Mandatory || len(parameters[j].MandatoryIf) > 0) {
return strings.Compare(parameters[i].Name, parameters[j].Name) < 0
} else if parameters[i].Mandatory || len(parameters[i].MandatoryIf) > 0 {
return true
}
return false
})
} else {
sort.SliceStable(parameters[:], func(i, j int) bool {
return strings.Compare(parameters[i].Name, parameters[j].Name) < 0
})
}
}
}