1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +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 internalWhitesource
final Map config final Map config
def orgAdminUserKey WhitesourceOrgAdminRepository() {
super()
}
WhitesourceOrgAdminRepository(Script script, Map config) { WhitesourceOrgAdminRepository(Script script, Map config) {
this.script = script this.script = script
@ -79,34 +81,29 @@ class WhitesourceOrgAdminRepository implements Serializable {
@NonCPS @NonCPS
protected def httpWhitesource(requestBody) { protected def httpWhitesource(requestBody) {
script.withCredentials ([script.string( requestBody["userKey"] = config.orgAdminUserKey
credentialsId: config.orgAdminUserTokenCredentialsId, def serializedBody = new JsonUtils().jsonToString(requestBody)
variable: 'orgAdminUserKey' def params = [
)]) { url : config.serviceUrl,
requestBody["userKey"] = orgAdminUserKey httpMode : 'POST',
def serializedBody = new JsonUtils().jsonToString(requestBody) acceptType : 'APPLICATION_JSON',
def params = [ contentType: 'APPLICATION_JSON',
url : config.serviceUrl, requestBody: serializedBody,
httpMode : 'POST', quiet : !config.verbose,
acceptType : 'APPLICATION_JSON', timeout : config.timeout
contentType: 'APPLICATION_JSON', ]
requestBody: serializedBody,
quiet : !config.verbose,
timeout : config.timeout
]
if (script.env.HTTP_PROXY) if (script.env.HTTP_PROXY)
params["httpProxy"] = script.env.HTTP_PROXY params["httpProxy"] = script.env.HTTP_PROXY
if (config.verbose) if (config.verbose)
script.echo "Sending http request with parameters ${params}" script.echo "Sending http request with parameters ${params}"
def response = script.httpRequest(params) def response = script.httpRequest(params)
if (config.verbose) if (config.verbose)
script.echo "Received response ${response}" script.echo "Received response ${response}"
return response return response
}
} }
} }

View File

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

View File

@ -217,14 +217,27 @@ void call(Map parameters = [:]) {
def whitesourceRepository = parameters.whitesourceRepositoryStub ?: new WhitesourceRepository(this, config) def whitesourceRepository = parameters.whitesourceRepositoryStub ?: new WhitesourceRepository(this, config)
def whitesourceOrgAdminRepository = parameters.whitesourceOrgAdminRepositoryStub ?: new WhitesourceOrgAdminRepository(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) checkStatus(statusCode, config)
script.commonPipelineEnvironment.setInfluxStepData('whitesource', true) 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) { private def triggerWhitesourceScanWithUserKey(script, config, utils, descriptorUtils, parameters, repository, orgAdminRepository) {
withCredentials ([string( withCredentials ([string(
credentialsId: config.userTokenCredentialsId, credentialsId: config.userTokenCredentialsId,