1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-03 15:02:35 +02:00

extract StepConfiguration generation to separate file

This commit is contained in:
Christopher Fenner 2020-04-27 18:19:38 +02:00
parent 0fc5cbcd9c
commit df80dab8cd
No known key found for this signature in database
GPG Key ID: 1D72FE34B3FECE9C
4 changed files with 84 additions and 21 deletions

View File

@ -0,0 +1,8 @@
package docs
func ifThenElse(condition bool, positive string, negative string) string {
if condition {
return positive
}
return negative
}

View File

@ -0,0 +1,27 @@
package docs
import (
"fmt"
"github.com/SAP/jenkins-library/pkg/config"
SliceUtils "github.com/SAP/jenkins-library/pkg/piperutils"
)
//BuildStepConfiguration creates a string with the content from the step configuration data.
func BuildStepConfiguration(stepData config.StepData) (content string) {
content = "Step Configuration\n\n"
content += "We recommend to define values of step parameters via [config.yml file](../configuration.md).\n\n"
content += "In following sections of the config.yml the configuration is possible:\n\n"
content += "| parameter | general | step/stage |\n"
content += "| --------- | ------- | ---------- |\n"
for _, parameter := range stepData.Spec.Inputs.Parameters {
if len(parameter.Scope) > 0 {
content += fmt.Sprintf("| `%v` | %v | %v |\n",
parameter.Name,
ifThenElse(SliceUtils.ContainsString(parameter.Scope, "GENERAL"), "X", ""),
ifThenElse(SliceUtils.ContainsString(parameter.Scope, "STEPS"), "X", ""))
}
}
return
}

View File

@ -0,0 +1,46 @@
package docs
import (
"testing"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/stretchr/testify/assert"
)
func TestGenerateStepDocumentationSuccess(t *testing.T) {
// init
testData := config.StepData{
Spec: config.StepSpec{
Inputs: config.StepInputs{
Parameters: []config.StepParameters{
{Name: "param0", Scope: []string{"GENERAL"}, Type: "string", Default: "default0"},
{Name: "param1", Scope: []string{"GENERAL", "STEPS"}, Type: "string", Default: "default1"},
{Name: "param2", Scope: []string{"PARAMETERS", "STAGES", "STEPS"}, Type: "string", Default: "default2"},
},
},
},
}
expected := "Step Configuration\n\n" +
"We recommend to define values of step parameters via [config.yml file](../configuration.md).\n\n" +
"In following sections of the config.yml the configuration is possible:\n\n" +
"| parameter | general | step/stage |\n" +
"| --------- | ------- | ---------- |\n" +
"| `param0` | X | |\n" +
"| `param1` | X | X |\n" +
"| `param2` | | X |\n"
// test
result := BuildStepConfiguration(testData)
t.Run("default", func(t *testing.T) {
// assert
assert.Equal(t, expected, result)
})
t.Run("display global parameters", func(t *testing.T) {
t.Skip("Not yet implemented.")
// assert
assert.Contains(t, result, "| `noTelemetry` | X | X |\n")
assert.Contains(t, result, "| `script` | X | X |\n")
assert.Contains(t, result, "| `verbose` | X | X |\n")
})
}

View File

@ -10,6 +10,7 @@ import (
"text/template"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/generator/docs"
)
// generates the step documentation and replaces the template with the generated documentation
@ -37,7 +38,7 @@ func generateStepDocumentation(stepData config.StepData, docuHelperData DocuHelp
"docGenStepName": docGenStepName,
"docGenDescription": docGenDescription,
"docGenParameters": docGenParameters,
"docGenConfiguration": docGenConfiguration,
"docGenConfiguration": docs.BuildStepConfiguration,
}
tmpl, err := template.New("doc").Funcs(funcMap).Parse(content)
checkError(err)
@ -123,11 +124,7 @@ func docGenParameters(stepData config.StepData) string {
// Replaces the docGenConfiguration placeholder with the content from the yaml
func docGenConfiguration(stepData config.StepData) string {
var configuration = "We recommend to define values of step parameters via [config.yml file](../configuration.md).\n\n"
configuration += "In following sections of the config.yml the configuration is possible:\n\n"
// create step configuration table
configuration += createConfigurationTable(stepData.Spec.Inputs.Parameters)
return "Step Configuration\n\n" + configuration
return docs.BuildStepConfiguration(stepData)
}
func createParametersTable(parameters []config.StepParameters) string {
@ -200,21 +197,6 @@ func addNewParameterWithCondition(param config.StepParameters, m map[string]stri
}
}
func createConfigurationTable(parameters []config.StepParameters) string {
var table = "| parameter | general | step/stage |\n"
table += "| --------- | ------- | ---------- |\n"
for _, param := range parameters {
if len(param.Scope) > 0 {
general := contains(param.Scope, "GENERAL")
step := contains(param.Scope, "STEPS")
table += fmt.Sprintf("| `%v` | %v | %v |\n", param.Name, ifThenElse(general, "X", ""), ifThenElse(step, "X", ""))
}
}
return table
}
func handleStepParameters(stepData *config.StepData) {
//add secrets to step parameters
appendSecretsToParameters(stepData)