1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-03 15:02:35 +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' - 'opensourceConfiguration'
verbose: false verbose: false
timeout: 0 timeout: 0
pipelineExecute:
branch: 'master'
path: 'Jenkinsfile'
credentialsId: ''
pipelineRestartSteps: pipelineRestartSteps:
sendMail: true sendMail: true
timeoutInSeconds: 900 timeoutInSeconds: 900

View File

@ -7,19 +7,6 @@ import groovy.text.SimpleTemplateEngine
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.security.MessageDigest 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) { def stash(name, include = '**/*.*', exclude = '', useDefaultExcludes = true) {
echo "Stash content: ${name} (include: ${include}, exclude: ${exclude}, useDefaultExcludes: ${useDefaultExcludes})" echo "Stash content: ${name} (include: ${include}, exclude: ${exclude}, useDefaultExcludes: ${useDefaultExcludes})"

View File

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

View File

@ -1,5 +1,5 @@
import com.sap.piper.GenerateDocumentation import com.sap.piper.GenerateDocumentation
import com.sap.piper.Utils import com.sap.piper.ConfigurationHelper
import groovy.transform.Field import groovy.transform.Field
@ -17,7 +17,11 @@ import groovy.transform.Field
'branch', 'branch',
/** The path to the Jenkinsfile, inside the repository, to be loaded.*/ /** The path to the Jenkinsfile, inside the repository, to be loaded.*/
'path', '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' 'credentialsId'
] ]
@ -34,38 +38,34 @@ void call(Map parameters = [:]) {
node() { node() {
def path Map config
handlePipelineStepErrors (stepName: 'pipelineExecute', stepParameters: parameters, failOnError: true) { 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 config = configHelper.use()
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', '')
deleteDir() deleteDir()
checkout([$class: 'GitSCM', branches: [[name: branch]], checkout([$class: 'GitSCM', branches: [[name: config.branch]],
doGenerateSubmoduleConfigurations: false, doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'SparseCheckoutPaths', extensions: [[$class: 'SparseCheckoutPaths',
sparseCheckoutPaths: [[path: path]] sparseCheckoutPaths: [[path: config.path]]
]], ]],
submoduleCfg: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: credentialsId, userRemoteConfigs: [[credentialsId: config.credentialsId,
url: repo url: config.repoUrl
]] ]]
]) ])
} }
load path load config.path
} }
} }