1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-04 04:07:16 +02:00

Merge pull request #235 from thorstenwillenbacher/pr/refactorwithcredentials

Pr/refactorwithcredentials
This commit is contained in:
thorstenwillenbacher 2018-08-09 11:11:57 +02:00 committed by GitHub
commit 5ea6eac6ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 90 additions and 121 deletions

View File

@ -64,60 +64,62 @@ public class ChangeManagement implements Serializable {
return items[0]
}
boolean isChangeInDevelopment(String changeId, String endpoint, String username, String password, String clientOpts = '') {
boolean isChangeInDevelopment(String changeId, String endpoint, String credentialsId, String clientOpts = '') {
int rc = executeWithCredentials(endpoint, credentialsId, 'is-change-in-development', ['-cID', "'${changeId}'", '--return-code'],
clientOpts) as int
int rc = script.sh(returnStatus: true,
script: getCMCommandLine(endpoint, username, password,
'is-change-in-development', ['-cID', "'${changeId}'",
'--return-code'],
clientOpts))
if(rc == 0) {
return true
} else if(rc == 3) {
return false
} else {
throw new ChangeManagementException("Cannot retrieve status for change document '${changeId}'. Does this change exist? Return code from cmclient: ${rc}.")
}
}
String createTransportRequest(String changeId, String developmentSystemId, String endpoint, String username, String password, String clientOpts = '') {
try {
String transportRequest = script.sh(returnStdout: true,
script: getCMCommandLine(endpoint, username, password, 'create-transport', ['-cID', changeId,
'-dID', developmentSystemId],
clientOpts))
return transportRequest.trim()
} catch(AbortException e) {
throw new ChangeManagementException("Cannot create a transport request for change id '$changeId'. $e.message.")
if (rc == 0) {
return true
} else if (rc == 3) {
return false
} else {
throw new ChangeManagementException("Cannot retrieve status for change document '${changeId}'. Does this change exist? Return code from cmclient: ${rc}.")
}
}
void uploadFileToTransportRequest(String changeId, String transportRequestId, String applicationId, String filePath, String endpoint, String username, String password, String cmclientOpts = '') {
String createTransportRequest(String changeId, String developmentSystemId, String endpoint, String credentialsId, String clientOpts = '') {
try {
def transportRequest = executeWithCredentials(endpoint, credentialsId, 'create-transport', ['-cID', changeId, '-dID', developmentSystemId],
clientOpts)
return transportRequest.trim() as String
}catch(AbortException e) {
throw new ChangeManagementException("Cannot create a transport request for change id '$changeId'. $e.message.")
}
}
int rc = script.sh(returnStatus: true,
script: getCMCommandLine(endpoint, username, password,
'upload-file-to-transport', ['-cID', changeId,
'-tID', transportRequestId,
applicationId, filePath],
cmclientOpts))
void uploadFileToTransportRequest(String changeId, String transportRequestId, String applicationId, String filePath, String endpoint, String credentialsId, String cmclientOpts = '') {
int rc = executeWithCredentials(endpoint, credentialsId, 'upload-file-to-transport', ['-cID', changeId,
'-tID', transportRequestId,
applicationId, filePath],
cmclientOpts) as int
if(rc == 0) {
return
} else {
throw new ChangeManagementException("Cannot upload file '$filePath' for change document '$changeId' with transport request '$transportRequestId'. Return code from cmclient: $rc.")
}
}
void releaseTransportRequest(String changeId, String transportRequestId, String endpoint, String username, String password, String clientOpts = '') {
def executeWithCredentials(String endpoint, String credentialsId, String command, List<String> args, String clientOpts = '') {
script.withCredentials([script.usernamePassword(
credentialsId: credentialsId,
passwordVariable: 'password',
usernameVariable: 'username')]) {
def returnValue = script.sh(returnStatus: true,
script: getCMCommandLine(endpoint, script.username, script.password,
command, args,
clientOpts))
return returnValue;
int rc = script.sh(returnStatus: true,
script: getCMCommandLine(endpoint, username, password,
'release-transport', ['-cID', changeId,
'-tID', transportRequestId],
clientOpts))
}
}
void releaseTransportRequest(String changeId, String transportRequestId, String endpoint, String credentialsId, String clientOpts = '') {
int rc = executeWithCredentials( endpoint, credentialsId, 'release-transport', ['-cID', changeId,
'-tID', transportRequestId], clientOpts) as int
if(rc == 0) {
return
} else {

View File

@ -1,5 +1,5 @@
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
@ -26,7 +26,7 @@ class CheckChangeInDevelopmentTest extends BasePiperTest {
.around(thrown)
.around(jsr)
.around(new JenkinsCredentialsRule(this)
.withCredentials('CM', 'anonymous', '********'))
.withCredentials('CM', 'anonymous', '********'))
@After
public void tearDown() {
@ -44,12 +44,10 @@ class CheckChangeInDevelopmentTest extends BasePiperTest {
changeManagement: [endpoint: 'https://example.org/cm'])
assert inDevelopment
assert cmUtilReceivedParams == [
changeId: '001',
endpoint: 'https://example.org/cm',
userName: 'anonymous',
password: '********',
credentialsId: 'CM',
cmclientOpts: ''
]
}
@ -163,11 +161,10 @@ class CheckChangeInDevelopmentTest extends BasePiperTest {
return changeDocumentId
}
boolean isChangeInDevelopment(String changeId, String endpoint, String userName, String password, String cmclientOpts) {
boolean isChangeInDevelopment(String changeId, String endpoint, String credentialsId, String cmclientOpts) {
cmUtilReceivedParams.changeId = changeId
cmUtilReceivedParams.endpoint = endpoint
cmUtilReceivedParams.userName = userName
cmUtilReceivedParams.password = password
cmUtilReceivedParams.credentialsId = credentialsId
cmUtilReceivedParams.cmclientOpts = cmclientOpts
return inDevelopment

View File

@ -36,6 +36,7 @@ public class TransportRequestCreateTest extends BasePiperTest {
nullScript.commonPipelineEnvironment.configuration = [general:
[changeManagement:
[
credentialsId: 'CM',
endpoint: 'https://example.org/cm',
@ -85,8 +86,7 @@ public class TransportRequestCreateTest extends BasePiperTest {
String createTransportRequest(String changeId,
String developmentSystemId,
String cmEndpoint,
String username,
String password,
String credentialId,
String clientOpts) {
throw new ChangeManagementException('Exception message.')
@ -110,15 +110,14 @@ public class TransportRequestCreateTest extends BasePiperTest {
String createTransportRequest(String changeId,
String developmentSystemId,
String cmEndpoint,
String username,
String password,
String credentialId,
String clientOpts) {
result.changeId = changeId
result.developmentSystemId = developmentSystemId
result.cmEndpoint = cmEndpoint
result.username = username
result.password = password
result.credentialId = credentialId
result.clientOpts = clientOpts
return '001'
}
@ -130,8 +129,7 @@ public class TransportRequestCreateTest extends BasePiperTest {
assert result == [changeId: '001',
developmentSystemId: '001',
cmEndpoint: 'https://example.org/cm',
username: 'anonymous',
password: '********',
credentialId: 'CM',
clientOpts: '-DmyProp=myVal'
]

View File

@ -116,8 +116,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
String applicationId,
String filePath,
String endpoint,
String username,
String password,
String credentialsId,
String cmclientOpts) {
throw new ChangeManagementException('Exception message')
}
@ -146,8 +145,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
String applicationId,
String filePath,
String endpoint,
String username,
String password,
String credentialsId,
String cmclientOpts) {
cmUtilReceivedParams.changeId = changeId
@ -155,8 +153,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
cmUtilReceivedParams.applicationId = applicationId
cmUtilReceivedParams.filePath = filePath
cmUtilReceivedParams.endpoint = endpoint
cmUtilReceivedParams.username = username
cmUtilReceivedParams.password = password
cmUtilReceivedParams.credentialsId = credentialsId
cmUtilReceivedParams.cmclientOpts = cmclientOpts
}
}
@ -175,8 +172,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
applicationId: 'app',
filePath: '/path',
endpoint: 'https://example.org/cm',
username: 'anonymous',
password: '********',
credentialsId: 'CM',
cmclientOpts: ''
]
}
@ -193,8 +189,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
String applicationId,
String filePath,
String endpoint,
String username,
String password,
String credentialsId,
String cmclientOpts) {
cmUtilReceivedParams.filePath = filePath
@ -223,8 +218,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
String applicationId,
String filePath,
String endpoint,
String username,
String password,
String credentialsId,
String cmclientOpts) {
cmUtilReceivedParams.filePath = filePath
@ -252,8 +246,7 @@ public class TransportRequestUploadFileTest extends BasePiperTest {
String applicationId,
String filePath,
String endpoint,
String username,
String password,
String credentialsId,
String cmclientOpts) {
throw new ChangeManagementException('Upload failure.')
}

View File

@ -21,6 +21,7 @@ import util.BasePiperTest
import util.JenkinsLoggingRule
import util.JenkinsScriptLoaderRule
import util.JenkinsShellCallRule
import util.JenkinsCredentialsRule
import util.Rules
import hudson.AbortException
@ -37,6 +38,7 @@ public class ChangeManagementTest extends BasePiperTest {
.around(thrown)
.around(script)
.around(logging)
.around(new JenkinsCredentialsRule(this).withCredentials('me','user','password'))
@Test
public void testRetrieveChangeDocumentIdOutsideGitWorkTreeTest() {
@ -90,8 +92,7 @@ public class ChangeManagementTest extends BasePiperTest {
public void testIsChangeInDevelopmentReturnsTrueWhenChangeIsInDevelopent() {
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, "cmclient.*is-change-in-development -cID '001'", 0)
boolean inDevelopment = new ChangeManagement(nullScript, null).isChangeInDevelopment('001', 'endpoint', 'user', 'password')
boolean inDevelopment = new ChangeManagement(nullScript, null).isChangeInDevelopment('001', 'endpoint', 'me')
assertThat(inDevelopment, is(equalTo(true)))
assertThat(script.shell[0], allOf(containsString("cmclient"),
@ -111,8 +112,7 @@ public class ChangeManagementTest extends BasePiperTest {
boolean inDevelopment = new ChangeManagement(nullScript, null)
.isChangeInDevelopment('001',
'endpoint',
'user',
'password')
'me')
assertThat(inDevelopment, is(equalTo(false)))
}
@ -124,8 +124,7 @@ public class ChangeManagementTest extends BasePiperTest {
thrown.expectMessage('Cannot retrieve status for change document \'001\'. Does this change exist? Return code from cmclient: 1.')
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, "cmclient.*is-change-in-development -cID '001'", 1)
new ChangeManagement(nullScript, null).isChangeInDevelopment('001', 'endpoint', 'user', 'password')
new ChangeManagement(nullScript, null).isChangeInDevelopment('001', 'endpoint', 'me')
}
@Test
@ -139,7 +138,7 @@ public class ChangeManagementTest extends BasePiperTest {
commandLine = commandLine.replaceAll(' +', " ")
assertThat(commandLine, not(containsString("CMCLIENT_OPTS")))
assertThat(commandLine, containsString("cmclient -e 'https://example.org/cm' -u 'me' -p 'topSecret' -t SOLMAN the-command -key1 val1 -key2 val2"))
}
}
@Test
public void testGetCommandLineWithCMClientOpts() {
@ -158,7 +157,7 @@ public void testGetCommandLineWithCMClientOpts() {
public void testCreateTransportRequestSucceeds() {
script.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".*cmclient.*create-transport -cID 001 -dID 002.*", '004')
def transportRequestId = new ChangeManagement(nullScript).createTransportRequest('001', '002', '003', 'me', 'openSesame')
def transportRequestId = new ChangeManagement(nullScript).createTransportRequest('001', '002', '003', 'me')
// the check for the transportRequestID is sufficient. This checks implicit the command line since that value is
// returned only in case the shell call matches.
@ -180,8 +179,7 @@ public void testGetCommandLineWithCMClientOpts() {
'XXX',
'/path',
'https://example.org/cm',
'me',
'openSesame')
'me')
}
@Test
@ -195,8 +193,7 @@ public void testGetCommandLineWithCMClientOpts() {
'XXX',
'/path',
'https://example.org/cm',
'me',
'openSesame')
'me')
// no assert required here, since the regex registered above to the script rule is an implicit check for
// the command line.
@ -216,8 +213,7 @@ public void testGetCommandLineWithCMClientOpts() {
'XXX',
'/path',
'https://example.org/cm',
'me',
'openSesame')
'me')
}
private GitUtils gitUtilsMock(boolean insideWorkTree, String[] changeIds) {

View File

@ -52,7 +52,7 @@ def call(parameters = [:]) {
if(changeId?.trim()) {
echo "[INFO] ChangeDocumentId retrieved from parameters."
echo "[INFO] ChangeDocumentId retrieved from parameters."
} else {
@ -85,22 +85,19 @@ def call(parameters = [:]) {
echo "[INFO] Checking if change document '${configuration.changeDocumentId}' is in development."
withCredentials([usernamePassword(
credentialsId: configuration.changeManagement.credentialsId,
passwordVariable: 'password',
usernameVariable: 'username')]) {
try {
try {
isInDevelopment = cm.isChangeInDevelopment(configuration.changeDocumentId,
configuration.changeManagement.endpoint,
username,
password,
configuration.changeManagement.clientOpts)
} catch(ChangeManagementException ex) {
throw new AbortException(ex.getMessage())
}
isInDevelopment = cm.isChangeInDevelopment(configuration.changeDocumentId,
configuration.changeManagement.endpoint,
configuration.changeManagement.credentialsId,
configuration.changeManagement.clientOpts)
} catch(ChangeManagementException ex) {
throw new AbortException(ex.getMessage())
}
if(isInDevelopment) {
echo "[INFO] Change '${changeId}' is in status 'in development'."
return true

View File

@ -79,22 +79,16 @@ def call(parameters = [:]) {
echo "[INFO] Creating transport request for change document '${configuration.changeDocumentId}' and development system '${configuration.developmentSystemId}'."
withCredentials([usernamePassword(
credentialsId: configuration.changeManagement.credentialsId,
passwordVariable: 'password',
usernameVariable: 'username')]) {
try {
transportRequestId = cm.createTransportRequest(configuration.changeDocumentId,
configuration.developmentSystemId,
configuration.changeManagement.endpoint,
username,
password,
configuration.changeManagement.credentialsId,
configuration.changeManagement.clientOpts)
} catch(ChangeManagementException ex) {
throw new AbortException(ex.getMessage())
}
}
echo "[INFO] Transport Request '$transportRequestId' has been successfully created."
return transportRequestId

View File

@ -108,22 +108,17 @@ def call(parameters = [:]) {
echo "[INFO] Closing transport request '${configuration.transportRequestId}' for change document '${configuration.changeDocumentId}'."
withCredentials([usernamePassword(
credentialsId: configuration.changeManagement.credentialsId,
passwordVariable: 'password',
usernameVariable: 'username')]) {
try {
cm.releaseTransportRequest(configuration.changeDocumentId,
configuration.transportRequestId,
configuration.changeManagement.endpoint,
username,
password,
configuration.changeManagement.credentialsId,
configuration.changeManagement.clientOpts)
} catch(ChangeManagementException ex) {
throw new AbortException(ex.getMessage())
}
}
echo "[INFO] Transport Request '${configuration.transportRequestId}' has been successfully closed."
}

View File

@ -113,24 +113,21 @@ def call(parameters = [:]) {
echo "[INFO] Uploading file '${configuration.filePath}' to transport request '${configuration.transportRequestId}' of change document '${configuration.changeDocumentId}'."
withCredentials([usernamePassword(
credentialsId: configuration.changeManagement.credentialsId,
passwordVariable: 'password',
usernameVariable: 'username')]) {
try {
cm.uploadFileToTransportRequest(configuration.changeDocumentId,
configuration.transportRequestId,
configuration.applicationId,
configuration.filePath,
configuration.changeManagement.endpoint,
username,
password,
configuration.changeManagement.credentialsId,
configuration.changeManagement.clientOpts)
} catch(ChangeManagementException ex) {
throw new AbortException(ex.getMessage())
}
}
echo "[INFO] File '${configuration.filePath}' has been successfully uploaded to transport request '${configuration.transportRequestId}' of change document '${configuration.changeDocumentId}'."
}