2020-04-08 12:55:46 +02:00
|
|
|
import com.sap.piper.JenkinsUtils
|
2019-05-13 12:29:07 +02:00
|
|
|
import com.sap.piper.Utils
|
2020-05-14 13:46:40 +02:00
|
|
|
import com.sap.piper.analytics.InfluxData
|
2019-05-13 12:29:07 +02:00
|
|
|
import static com.sap.piper.Prerequisites.checkScript
|
|
|
|
import groovy.transform.Field
|
2019-08-11 22:42:34 +02:00
|
|
|
import java.nio.charset.StandardCharsets
|
|
|
|
|
2019-05-13 12:29:07 +02:00
|
|
|
@Field String STEP_NAME = getClass().getName()
|
2021-11-15 15:20:20 +02:00
|
|
|
@Field String METADATA_FILE = 'metadata/sonarExecuteScan.yaml'
|
2019-05-13 12:29:07 +02:00
|
|
|
|
|
|
|
void call(Map parameters = [:]) {
|
|
|
|
handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) {
|
2020-04-08 12:55:46 +02:00
|
|
|
def script = checkScript(this, parameters) ?: this
|
|
|
|
def utils = parameters.juStabUtils ?: new Utils()
|
|
|
|
def jenkinsUtils = parameters.jenkinsUtilsStub ?: new JenkinsUtils()
|
2020-08-06 14:22:40 +02:00
|
|
|
String piperGoPath = parameters.piperGoPath ?: './piper'
|
2020-04-08 12:55:46 +02:00
|
|
|
|
2020-09-08 17:28:29 +02:00
|
|
|
piperExecuteBin.prepareExecution(script, utils, parameters)
|
2020-08-06 14:22:40 +02:00
|
|
|
piperExecuteBin.prepareMetadataResource(script, METADATA_FILE)
|
|
|
|
Map stepParameters = piperExecuteBin.prepareStepParameters(parameters)
|
2020-04-08 12:55:46 +02:00
|
|
|
|
2020-08-06 14:22:40 +02:00
|
|
|
List credentialInfo = [
|
|
|
|
[type: 'token', id: 'sonarTokenCredentialsId', env: ['PIPER_token']],
|
|
|
|
[type: 'token', id: 'githubTokenCredentialsId', env: ['PIPER_githubToken']],
|
|
|
|
]
|
2020-04-08 12:55:46 +02:00
|
|
|
|
|
|
|
withEnv([
|
|
|
|
"PIPER_parametersJSON=${groovy.json.JsonOutput.toJson(stepParameters)}",
|
2020-08-06 14:22:40 +02:00
|
|
|
"PIPER_correlationID=${env.BUILD_URL}",
|
2020-04-08 12:55:46 +02:00
|
|
|
]) {
|
|
|
|
String customDefaultConfig = piperExecuteBin.getCustomDefaultConfigsArg()
|
|
|
|
String customConfigArg = piperExecuteBin.getCustomConfigArg(script)
|
|
|
|
// get context configuration
|
2020-08-06 14:22:40 +02:00
|
|
|
Map config
|
|
|
|
piperExecuteBin.handleErrorDetails(STEP_NAME) {
|
|
|
|
config = piperExecuteBin.getStepContextConfig(script, piperGoPath, METADATA_FILE, customDefaultConfig, customConfigArg)
|
|
|
|
echo "Context Config: ${config}"
|
|
|
|
}
|
2020-09-11 13:39:17 +02:00
|
|
|
// get step configuration to access `instance` & `customTlsCertificateLinks` & `owner` & `repository`
|
|
|
|
// & `legacyPRHandling` & `inferBranchName`
|
2021-06-15 14:34:56 +02:00
|
|
|
// writePipelineEnv needs to be called here as owner and repository may come from the pipeline environment
|
|
|
|
writePipelineEnv(script: script, piperGoPath: piperGoPath)
|
2020-08-06 14:22:40 +02:00
|
|
|
Map stepConfig = readJSON(text: sh(returnStdout: true, script: "${piperGoPath} getConfig --stepMetadata '.pipeline/tmp/${METADATA_FILE}'${customDefaultConfig}${customConfigArg}"))
|
|
|
|
echo "Step Config: ${stepConfig}"
|
2020-04-08 12:55:46 +02:00
|
|
|
|
|
|
|
List environment = []
|
2020-09-08 17:28:29 +02:00
|
|
|
if (isPullRequest()) {
|
2020-04-08 12:55:46 +02:00
|
|
|
checkMandatoryParameter(stepConfig, "owner")
|
|
|
|
checkMandatoryParameter(stepConfig, "repository")
|
|
|
|
if(stepConfig.legacyPRHandling) {
|
|
|
|
checkMandatoryParameter(config, "githubTokenCredentialsId")
|
2019-05-13 12:29:07 +02:00
|
|
|
}
|
2020-09-11 13:39:17 +02:00
|
|
|
} else if (!isProductiveBranch(script) && stepConfig.inferBranchName && env.BRANCH_NAME) {
|
|
|
|
environment.add("PIPER_branchName=${env.BRANCH_NAME}")
|
2019-05-13 12:29:07 +02:00
|
|
|
}
|
2020-04-29 11:11:23 +02:00
|
|
|
try {
|
|
|
|
// load certificates into cacerts file
|
2021-11-25 08:42:54 +02:00
|
|
|
if(script.env.PIPER_ENABLE_LEGACY_CERT_LOADING && Boolean.valueOf(script.env.PIPER_ENABLE_LEGACY_CERT_LOADING)){
|
|
|
|
loadCertificates(customTlsCertificateLinks: stepConfig.customTlsCertificateLinks, verbose: stepConfig.verbose)
|
|
|
|
}
|
2020-04-29 11:11:23 +02:00
|
|
|
// execute step
|
2020-10-01 16:27:50 +02:00
|
|
|
piperExecuteBin.dockerWrapper(script, STEP_NAME, config){
|
2020-04-29 11:11:23 +02:00
|
|
|
if(!fileExists('.git')) utils.unstash('git')
|
2020-05-11 12:46:16 +02:00
|
|
|
piperExecuteBin.handleErrorDetails(STEP_NAME) {
|
2021-06-16 11:46:55 +02:00
|
|
|
writePipelineEnv(script: script, piperGoPath: piperGoPath)
|
2022-02-25 14:09:50 +02:00
|
|
|
withEnv(environment) {
|
|
|
|
influxWrapper(script) {
|
|
|
|
piperExecuteBin.credentialWrapper(config, credentialInfo) {
|
|
|
|
if (stepConfig.instance) {
|
|
|
|
withSonarQubeEnv(stepConfig.instance) {
|
|
|
|
echo "Instance is deprecated - please use serverUrl parameter to set URL to the Sonar backend."
|
|
|
|
sh "${piperGoPath} ${STEP_NAME}${customDefaultConfig}${customConfigArg}"
|
|
|
|
jenkinsUtils.handleStepResults(STEP_NAME, false, false)
|
|
|
|
readPipelineEnv(script: script, piperGoPath: piperGoPath)
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-06 14:22:40 +02:00
|
|
|
sh "${piperGoPath} ${STEP_NAME}${customDefaultConfig}${customConfigArg}"
|
2020-05-14 13:46:40 +02:00
|
|
|
}
|
2020-05-11 12:46:16 +02:00
|
|
|
}
|
2020-04-29 11:11:23 +02:00
|
|
|
}
|
2020-04-08 12:55:46 +02:00
|
|
|
}
|
2019-05-13 12:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-29 11:11:23 +02:00
|
|
|
} finally {
|
|
|
|
def ignore = sh script: 'rm -rf .sonar-scanner .certificates', returnStatus: true
|
2019-05-13 12:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 12:55:46 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-13 12:29:07 +02:00
|
|
|
|
2020-08-06 14:22:40 +02:00
|
|
|
private void influxWrapper(Script script, body){
|
|
|
|
try {
|
|
|
|
body()
|
|
|
|
} finally {
|
|
|
|
InfluxData.readFromDisk(script)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 12:55:46 +02:00
|
|
|
private void checkMandatoryParameter(config, key){
|
|
|
|
if (!config[key]) {
|
|
|
|
throw new IllegalArgumentException( "ERROR - NO VALUE AVAILABLE FOR ${key}")
|
2019-05-13 12:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Boolean isPullRequest(){
|
|
|
|
return env.CHANGE_ID
|
|
|
|
}
|
|
|
|
|
2020-09-11 13:39:17 +02:00
|
|
|
private Boolean isProductiveBranch(Script script) {
|
|
|
|
def productiveBranch = script.commonPipelineEnvironment?.getStepConfiguration('', '')?.productiveBranch
|
|
|
|
return env.BRANCH_NAME == productiveBranch
|
|
|
|
}
|
|
|
|
|
2019-08-11 22:42:34 +02:00
|
|
|
private void loadCertificates(Map config) {
|
|
|
|
String certificateFolder = '.certificates/'
|
|
|
|
List wgetOptions = [
|
|
|
|
"--directory-prefix ${certificateFolder}"
|
|
|
|
]
|
|
|
|
List keytoolOptions = [
|
|
|
|
'-import',
|
|
|
|
'-noprompt',
|
|
|
|
'-storepass changeit',
|
2019-12-20 11:51:33 +02:00
|
|
|
"-keystore ${certificateFolder}cacerts"
|
2019-08-11 22:42:34 +02:00
|
|
|
]
|
|
|
|
if (config.customTlsCertificateLinks){
|
|
|
|
if(config.verbose){
|
|
|
|
wgetOptions.push('--verbose')
|
|
|
|
keytoolOptions.push('-v')
|
|
|
|
}else{
|
|
|
|
wgetOptions.push('--no-verbose')
|
|
|
|
}
|
|
|
|
config.customTlsCertificateLinks.each { url ->
|
|
|
|
def filename = new File(url).getName()
|
|
|
|
filename = URLDecoder.decode(filename, StandardCharsets.UTF_8.name())
|
|
|
|
sh "wget ${wgetOptions.join(' ')} ${url}"
|
|
|
|
sh "keytool ${keytoolOptions.join(' ')} -alias '${filename}' -file '${certificateFolder}${filename}'"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|