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

Moved admin auth handling into script

This commit is contained in:
Sven Merk 2019-03-11 10:46:22 +01:00
parent 61a9710fab
commit 61d6280c3a
3 changed files with 40 additions and 28 deletions

View File

@ -9,7 +9,9 @@ class WhitesourceOrgAdminRepository implements Serializable {
final internalWhitesource
final Map config
def orgAdminUserKey
WhitesourceOrgAdminRepository() {
super()
}
WhitesourceOrgAdminRepository(Script script, Map config) {
this.script = script
@ -79,34 +81,29 @@ class WhitesourceOrgAdminRepository implements Serializable {
@NonCPS
protected def httpWhitesource(requestBody) {
script.withCredentials ([script.string(
credentialsId: config.orgAdminUserTokenCredentialsId,
variable: 'orgAdminUserKey'
)]) {
requestBody["userKey"] = orgAdminUserKey
def serializedBody = new JsonUtils().jsonToString(requestBody)
def params = [
url : config.serviceUrl,
httpMode : 'POST',
acceptType : 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
requestBody: serializedBody,
quiet : !config.verbose,
timeout : config.timeout
]
requestBody["userKey"] = config.orgAdminUserKey
def serializedBody = new JsonUtils().jsonToString(requestBody)
def params = [
url : config.serviceUrl,
httpMode : 'POST',
acceptType : 'APPLICATION_JSON',
contentType: 'APPLICATION_JSON',
requestBody: serializedBody,
quiet : !config.verbose,
timeout : config.timeout
]
if (script.env.HTTP_PROXY)
params["httpProxy"] = script.env.HTTP_PROXY
if (script.env.HTTP_PROXY)
params["httpProxy"] = script.env.HTTP_PROXY
if (config.verbose)
script.echo "Sending http request with parameters ${params}"
if (config.verbose)
script.echo "Sending http request with parameters ${params}"
def response = script.httpRequest(params)
def response = script.httpRequest(params)
if (config.verbose)
script.echo "Received response ${response}"
if (config.verbose)
script.echo "Received response ${response}"
return response
}
return response
}
}

View File

@ -1,5 +1,7 @@
package com.sap.piper.integration
import com.lesfurets.jenkins.unit.PipelineTestHelper
import org.codehaus.groovy.runtime.InvokerHelper
import org.junit.After
import org.junit.Before
import org.junit.Rule
@ -71,8 +73,8 @@ class WhitesourceOrgAdminRepositoryTest extends BasePiperTest {
@Test
void testHttpWhitesourceInternalCallUserKey() {
repository.orgAdminUserKey = "4711"
def config = [ serviceUrl: "http://some.host.whitesource.com/api/", verbose: false, orgAdminUserKey: "4711"]
repository.config.putAll(config)
def requestBody = ["someJson" : [ "someObject" : "abcdef" ]]
def requestParams

View File

@ -217,14 +217,27 @@ void call(Map parameters = [:]) {
def whitesourceRepository = parameters.whitesourceRepositoryStub ?: new WhitesourceRepository(this, config)
def whitesourceOrgAdminRepository = parameters.whitesourceOrgAdminRepositoryStub ?: new WhitesourceOrgAdminRepository(this, config)
statusCode = triggerWhitesourceScanWithUserKey(script, config, utils, descriptorUtils, parameters, whitesourceRepository, whitesourceOrgAdminRepository)
if(config.orgAdminUserTokenCredentialsId) {
statusCode = triggerWhitesourceScanWithOrgAdminUserKey(script, config, utils, descriptorUtils, parameters, whitesourceRepository, whitesourceOrgAdminRepository)
} else {
statusCode = triggerWhitesourceScanWithUserKey(script, config, utils, descriptorUtils, parameters, whitesourceRepository, whitesourceOrgAdminRepository)
}
checkStatus(statusCode, config)
script.commonPipelineEnvironment.setInfluxStepData('whitesource', true)
}
}
private def triggerWhitesourceScanWithOrgAdminUserKey(script, config, utils, descriptorUtils, parameters, repository, orgAdminRepository) {
withCredentials ([script.string(
credentialsId: config.orgAdminUserTokenCredentialsId,
variable: 'orgAdminUserKey'
)]) {
config.orgAdminUserKey = orgAdminUserKey
triggerWhitesourceScanWithUserKey(script, config, utils, descriptorUtils, parameters, repository, orgAdminRepository)
}
}
private def triggerWhitesourceScanWithUserKey(script, config, utils, descriptorUtils, parameters, repository, orgAdminRepository) {
withCredentials ([string(
credentialsId: config.userTokenCredentialsId,