1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-12-13 23:45:47 +02:00

add step transportRequestCreate

This commit is contained in:
Alejandra Ferreiro Vidal
2018-06-20 11:36:41 +02:00
parent cf4eea82a6
commit e34f99deac
4 changed files with 227 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
# transportRequestCreate
## Description
Creates a Transport Request for a Change Document on the Solution Manager.
## Prerequisites
* **[Change Management Client 2.0.0 or compatible version](http://central.maven.org/maven2/com/sap/devops/cmclient/dist.cli/)** - available for download on Maven Central.
## Parameters
| parameter | mandatory | default | possible values |
| -----------------|-----------|--------------------------------------------------------|--------------------|
| `script` | yes | | |
| `changeId` | yes | | |
| `cmCredentialsId` | yes | | |
| `cmEndpoint` | yes | | |
* `script` - The common script environment of the Jenkinsfile running. Typically the reference to the script calling the pipeline step is provided with the `this` parameter, as in `script: this`. This allows the function to access the [`commonPipelineEnvironment`](commonPipelineEnvironment.md) for retrieving, for example, configuration parameters.
* `changeId` - The id of the change document to transport.
* `cmCredentialsId` - The credentials to connect to the Solution Manager.
* `cmEndpoint` - The address of the Solution Manager.
## Step configuration
The following parameters can also be specified as step parameters using the global configuration file:
* `cmCredentialsId`
* `cmEndpoint`
## Return value
The id of the Transport Request that has been created.
## Exceptions
* `AbortException`:
* If the change id is not provided.
* If the creation of the transport request fails.
## Example
```groovy
def transportRequestId = transportRequestCreate script:this, changeId: '001'
```

View File

@@ -1,8 +1,31 @@
package com.sap.piper.cm
import hudson.AbortException
public class ChangeManagement implements Serializable {
private script
public ChangeManagement(def script) {
this.script = script
}
String createTransportRequest(String changeId, String developmentSystemId, String endpoint, String username, String password) {
try {
String transportRequest = script.sh(returnStdout: true,
script:
"""#!/bin/bash
cmclient -e '$endpoint' \
-u '$username' \
-p '$password' \
-t SOLMAN \
create-transport -cID '$changeId' -dID '$developmentSystemId'
""")
return transportRequest.trim()
} catch(AbortException e) {
throw new ChangeManagementException("Cannot create a transport request for change id '$changeId'. $e.message.")
}
}
}

View File

@@ -0,0 +1,96 @@
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsStepRule
import util.JenkinsLoggingRule
import util.Rules
import hudson.AbortException
public class TransportRequestCreateTest extends BasePiperTest {
private ExpectedException thrown = new ExpectedException()
private JenkinsStepRule jsr = new JenkinsStepRule(this)
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
@Rule
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(thrown)
.around(jsr)
.around(jlr)
@Before
public void setup() {
helper.registerAllowedMethod('usernamePassword', [Map.class], {m -> return m})
helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
credentialsId = l[0].credentialsId
binding.setProperty('username', 'anonymous')
binding.setProperty('password', '********')
try {
c()
} finally {
binding.setProperty('username', null)
binding.setProperty('password', null)
}
})
helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 })
nullScript.commonPipelineEnvironment.configuration = [steps:
[transportRequestCreate:
[
cmCredentialsId: 'CM',
cmEndpoint: 'https://example.org/cm'
]
]
]
}
@Test
public void changeIdNotProvidedTest() {
thrown.expect(AbortException)
thrown.expectMessage("Change id not provided (parameter: 'changeId').")
jsr.step.call(script: nullScript, developmentSystemId: '001')
}
@Test
public void developmentSystemIdNotProvidedTest() {
thrown.expect(AbortException)
thrown.expectMessage("Development system id not provided (parameter: 'developmentSystemId').")
jsr.step.call(script: nullScript, changeId: '001')
}
@Test
public void createTransportRequestFailureTest() {
helper.registerAllowedMethod('sh', [Map], { Map m -> throw new AbortException('Exception message.') })
thrown.expect(AbortException)
thrown.expectMessage("Cannot create a transport request for change id '001'. Exception message.")
jsr.step.call(script: nullScript, changeId: '001', developmentSystemId: '001')
}
@Test
public void createTransportRequestSuccessTest() {
helper.registerAllowedMethod('sh', [Map], { Map m -> return '001' })
jsr.step.call(script: nullScript, changeId: '001', developmentSystemId: '001')
assert jlr.log.contains("[INFO] Creating transport request for change document '001' and development system '001'.")
assert jlr.log.contains("[INFO] Transport Request '001' has been successfully created.")
}
}

View File

@@ -0,0 +1,68 @@
import com.sap.piper.GitUtils
import groovy.transform.Field
import com.sap.piper.ConfigurationMerger
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException
import hudson.AbortException
@Field def STEP_NAME = 'transportRequestCreate'
@Field Set parameterKeys = [
'changeId',
'developmentSystemId',
'cmCredentialsId',
'cmEndpoint'
]
@Field Set stepConfigurationKeys = [
'cmCredentialsId',
'cmEndpoint'
]
def call(parameters = [:]) {
handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters) {
def script = parameters?.script ?: [commonPipelineEnvironment: commonPipelineEnvironment]
ChangeManagement cm = new ChangeManagement(script)
Map configuration = ConfigurationMerger.merge(parameters.script, STEP_NAME,
parameters, parameterKeys,
stepConfigurationKeys)
def changeId = configuration.changeId
if(!changeId) throw new AbortException('Change id not provided (parameter: \'changeId\').')
def developmentSystemId = configuration.developmentSystemId
if(!developmentSystemId) throw new AbortException('Development system id not provided (parameter: \'developmentSystemId\').')
def cmCredentialsId = configuration.cmCredentialsId
if(!cmCredentialsId) throw new AbortException('Credentials id not provided (parameter: \'cmCredentialsId\').')
def cmEndpoint = configuration.cmEndpoint
if(!cmEndpoint) throw new AbortException('Solution Manager endpoint not provided (parameter: \'cmEndpoint\').')
def transportRequestId
echo "[INFO] Creating transport request for change document '$changeId' and development system '$developmentSystemId'."
withCredentials([usernamePassword(
credentialsId: cmCredentialsId,
passwordVariable: 'password',
usernameVariable: 'username')]) {
try {
transportRequestId = cm.createTransportRequest(changeId, developmentSystemId, cmEndpoint, username, password)
} catch(ChangeManagementException ex) {
throw new AbortException(ex.getMessage())
}
}
echo "[INFO] Transport Request '$transportRequestId' has been successfully created."
return transportRequestId
}
}