1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

remove utils.getMandatoryParameter()

This commit is contained in:
Alejandra Ferreiro Vidal 2019-04-05 11:30:02 +02:00
parent 9fa9b8a4f2
commit 86978becfc
5 changed files with 32 additions and 64 deletions

View File

@ -348,6 +348,10 @@ steps:
- 'opensourceConfiguration'
verbose: false
timeout: 0
pipelineExecute:
branch: 'master'
path: 'Jenkinsfile'
credentialsId: ''
pipelineRestartSteps:
sendMail: true
timeoutInSeconds: 900

View File

@ -7,19 +7,6 @@ import groovy.text.SimpleTemplateEngine
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
@NonCPS
def getMandatoryParameter(Map map, paramName, defaultValue = null) {
def paramValue = map[paramName]
if (paramValue == null)
paramValue = defaultValue
if (paramValue == null)
throw new Exception("ERROR - NO VALUE AVAILABLE FOR ${paramName}")
return paramValue
}
def stash(name, include = '**/*.*', exclude = '', useDefaultExcludes = true) {
echo "Stash content: ${name} (include: ${include}, exclude: ${exclude}, useDefaultExcludes: ${useDefaultExcludes})"

View File

@ -11,6 +11,7 @@ import util.JenkinsReadYamlRule
import util.JenkinsStepRule
class PipelineExecuteTest extends BasePiperTest {
private ExpectedException thrown = new ExpectedException().none()
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
@ -27,9 +28,6 @@ class PipelineExecuteTest extends BasePiperTest {
@Before
void init() {
pipelinePath = null
checkoutParameters.clear()
load = null
helper.registerAllowedMethod('deleteDir', [], null)
helper.registerAllowedMethod('checkout', [Map], { m ->
@ -44,17 +42,19 @@ class PipelineExecuteTest extends BasePiperTest {
@Test
void straightForwardTest() {
stepRule.step.pipelineExecute(repoUrl: "https://test.com/myRepo.git")
assert load == "Jenkinsfile"
assert checkoutParameters.branch == 'master'
assert checkoutParameters.repoUrl == "https://test.com/myRepo.git"
assert checkoutParameters.credentialsId == ''
assert checkoutParameters.path == 'Jenkinsfile'
}
@Test
void parameterizeTest() {
stepRule.step.pipelineExecute(repoUrl: "https://test.com/anotherRepo.git",
branch: 'feature',
path: 'path/to/Jenkinsfile',
@ -65,11 +65,11 @@ class PipelineExecuteTest extends BasePiperTest {
assert checkoutParameters.repoUrl == "https://test.com/anotherRepo.git"
assert checkoutParameters.credentialsId == 'abcd1234'
assert checkoutParameters.path == 'path/to/Jenkinsfile'
}
@Test
void noRepoUrlTest() {
thrown.expect(Exception)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR repoUrl")

View File

@ -41,29 +41,6 @@ class UtilsTest extends BasePiperTest {
}
@Test
void noValueGetMandatoryParameterTest() {
thrown.expect(Exception)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR test")
utils.getMandatoryParameter(parameters, 'test', null)
}
@Test
void defaultValueGetMandatoryParameterTest() {
assert utils.getMandatoryParameter(parameters, 'test', 'default') == 'default'
}
@Test
void valueGetmandatoryParameterTest() {
parameters.put('test', 'value')
assert utils.getMandatoryParameter(parameters, 'test', null) == 'value'
}
@Test
void testGenerateSHA1() {
def result = utils.generateSha1('ContinuousDelivery')

View File

@ -1,5 +1,5 @@
import com.sap.piper.GenerateDocumentation
import com.sap.piper.Utils
import com.sap.piper.ConfigurationHelper
import groovy.transform.Field
@ -17,7 +17,11 @@ import groovy.transform.Field
'branch',
/** The path to the Jenkinsfile, inside the repository, to be loaded.*/
'path',
/** The Jenkins credentials containing user and password needed to access a private git repository.*/
/**
* The Jenkins credentials containing user and password needed to access a private git repository.
* In case access to the repository containing the pipeline script is restricted the credentialsId of the credentials used for
* accessing the repository needs to be provided. The corresponding credentials needs to be configured in Jenkins accordingly.
*/
'credentialsId'
]
@ -34,38 +38,34 @@ void call(Map parameters = [:]) {
node() {
def path
Map config
handlePipelineStepErrors (stepName: 'pipelineExecute', stepParameters: parameters, failOnError: true) {
def utils = new Utils()
ConfigurationHelper configHelper = ConfigurationHelper.newInstance(this)
.loadStepDefaults()
.mixin(parameters, PARAMETER_KEYS)
.withMandatoryProperty('repoUrl')
.withMandatoryProperty('branch')
.withMandatoryProperty('path')
.withMandatoryProperty('credentialsId')
// The coordinates of the pipeline script
def repo = utils.getMandatoryParameter(parameters, 'repoUrl', null)
def branch = utils.getMandatoryParameter(parameters, 'branch', 'master')
path = utils.getMandatoryParameter(parameters, 'path', 'Jenkinsfile')
// In case access to the repository containing the pipeline
// script is restricted the credentialsId of the credentials used for
// accessing the repository needs to be provided below. The corresponding
// credentials needs to be configured in Jenkins accordingly.
def credentialsId = utils.getMandatoryParameter(parameters, 'credentialsId', '')
config = configHelper.use()
deleteDir()
checkout([$class: 'GitSCM', branches: [[name: branch]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SparseCheckoutPaths',
sparseCheckoutPaths: [[path: path]]
checkout([$class: 'GitSCM', branches: [[name: config.branch]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SparseCheckoutPaths',
sparseCheckoutPaths: [[path: config.path]]
]],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: credentialsId,
url: repo
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: config.credentialsId,
url: config.repoUrl
]]
])
}
load path
load config.path
}
}