From 6a907cff2332188bc29699aec83f6460c9ae178c Mon Sep 17 00:00:00 2001 From: Sven Merk Date: Wed, 6 Mar 2019 13:12:23 +0100 Subject: [PATCH] Add creation capabilities for whitesource products --- resources/default_pipeline_environment.yml | 2 ++ .../WhitesourceOrgAdminRepository.groovy | 33 +++++++++++++++++-- vars/whitesourceExecuteScan.groovy | 11 +++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/resources/default_pipeline_environment.yml b/resources/default_pipeline_environment.yml index 5afbe760e..c81e2f407 100644 --- a/resources/default_pipeline_environment.yml +++ b/resources/default_pipeline_environment.yml @@ -256,6 +256,8 @@ steps: npmExecute: dockerImage: 'node:8-stretch' whitesourceExecuteScan: + createProductFromPipeline: true + emailAddressesOfInitialProductAdmins: [] buildDescriptorExcludeList: [] parallelLimit: 15 licensingVulnerabilities: true diff --git a/src/com/sap/piper/integration/WhitesourceOrgAdminRepository.groovy b/src/com/sap/piper/integration/WhitesourceOrgAdminRepository.groovy index 52b056646..eb1593d44 100644 --- a/src/com/sap/piper/integration/WhitesourceOrgAdminRepository.groovy +++ b/src/com/sap/piper/integration/WhitesourceOrgAdminRepository.groovy @@ -42,13 +42,40 @@ class WhitesourceOrgAdminRepository implements Serializable { } } - if (!foundMetaProduct) - script.error "[WhiteSource] Could not fetch/find requested product '${config.productName}'" - return foundMetaProduct } + def createProduct() { + def requestBody = [ + requestType: "createProduct", + orgToken: config.orgToken, + productName: config.productName + ] + def response = issueHttpRequest(requestBody) + def parsedResponse = new JsonUtils().parseJsonSerializable(response.content) + def metaInfo = parsedResponse + def groups = [] + def users = [] + config.emailAddressesOfInitialProductAdmins.each { + email -> users.add(["email": config.emailOfInitialProductAdmin]) + } + + requestBody = [ + "requestType" : "setProductAssignments", + "productToken" : metaInfo.productToken, + "productMembership" : ["userAssignments":[], "groupAssignments":groups], + "productAdmins" : ["userAssignments":users], + "alertsEmailReceivers" : ["userAssignments":[]] + ] + issueHttpRequest(requestBody) + + return metaInfo + } + + def issueHttpRequest(requestBody) { + internalWhitesource ? internalWhitesource.httpWhitesource(requestBody) : httpWhitesource(requestBody) + } @NonCPS protected def httpWhitesource(requestBody) { diff --git a/vars/whitesourceExecuteScan.groovy b/vars/whitesourceExecuteScan.groovy index 54deceb96..1ffb2871c 100644 --- a/vars/whitesourceExecuteScan.groovy +++ b/vars/whitesourceExecuteScan.groovy @@ -224,8 +224,15 @@ private def triggerWhitesourceScanWithUserKey(script, config, utils, descriptorU void analyseWhitesourceResults(Map config, WhitesourceRepository repository, WhitesourceOrgAdminRepository orgAdminRepository) { if (!config.productToken) { def metaInfo = orgAdminRepository.fetchProductMetaInfo() - echo "Meta Information: ${metaInfo}" - config.productToken = metaInfo.token + def key = "token" + if(!metaInfo && config.createProductFromPipeline) { + metaInfo = orgAdminRepository.createProduct() + key = "productToken" + } else if(!metaInfo) { + error "[WhiteSource] Could not fetch/find requested product '${config.productName}' and automatic creation has been disabled" + } + echo "Meta Info: ${metaInfo}" + config.productToken = metaInfo[key] } def pdfName = "whitesource-riskReport.pdf"