1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/vars/debugReportArchive.groovy
Stephan Aßmus 9658f3b480
writeFile() cannot be passed a Map (#1163)
* writeFile() cannot be passed a Map

I've changed the return type of DebugReport.generateReport() from
String to Map in order to get the generated file name as part of the
return value instead of getting it from a field of DebugReport. The
UnitTest checks whether writeFile() creates the debug_report file
successfully and whether it has the expected contents. The effect
of passing the Map instead of map.contents to writeFile() should
have been an unnecessary wrapping via Map.toString() as in the test,
but in the execution context of Jenkins, this throws an
IllegalArgumentException: Could not instantiate {... and then the
results of map.toString().

* Improve JenkinsWriteFileRule compatibility

Calling m.text.toString() is wrong, since the type stored at m.text
already needs to be a String (or GString). Expecting valid parameters
here makes sure problems are detected by tests already. (All tests
pass as before.)
2020-02-10 12:25:33 +01:00

74 lines
3.0 KiB
Groovy

import com.sap.piper.ConfigurationHelper
import com.sap.piper.DebugReport
import com.sap.piper.GenerateDocumentation
import com.sap.piper.Utils
import groovy.transform.Field
import static com.sap.piper.Prerequisites.checkScript
@Field def STEP_NAME = getClass().getName()
@Field Set GENERAL_CONFIG_KEYS = []
@Field Set STEP_CONFIG_KEYS = [
/**
* Flag to control whether potentially confidential information will be included in the
* debug_report.txt. Default value is `false`. Additional information written to the log
* when this flag is `true` includes MTA modules, NPM modules, the GitHub repository and
* branch, the global extension repository if used, a shared config file path, and all
* used global and local shared libraries.
* @possibleValues `true`, `false`
*/
'shareConfidentialInformation'
]
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS + [
/**
* Flag to enable printing the generated debug_report.txt also to the console.
*/
'printToConsole'
]
/**
* Archives the debug_report.txt artifact which facilitates analyzing pipeline errors by collecting
* information about the Jenkins environment in which the pipeline was run. There is a single
* config option 'shareConfidentialInformation' to enable including (possibly) confidential
* information in the debug report, which could be helpful depending on the specific error.
* By default this information is not included.
*/
@GenerateDocumentation
void call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this
try {
String stageName = parameters.stageName ?: env.STAGE_NAME
// ease handling extension
stageName = stageName?.replace('Declarative: ', '')
def utils = parameters.juStabUtils ?: new Utils()
Map configuration = ConfigurationHelper.newInstance(this)
.loadStepDefaults()
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
.mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
.mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
.mixin(parameters, PARAMETER_KEYS)
.use()
utils.pushToSWA([
step: STEP_NAME,
stepParamKey1: 'scriptMissing',
stepParam1: parameters?.script == null
], configuration)
boolean shareConfidentialInformation = configuration?.get('shareConfidentialInformation') ?: false
Map result = DebugReport.instance.generateReport(script, shareConfidentialInformation)
if (parameters.printToConsole) {
echo result.contents
}
script.writeFile file: result.fileName, text: result.contents
script.archiveArtifacts artifacts: result.fileName
echo "Successfully archived debug report as '${result.fileName}'"
} catch (Exception e) {
println("WARNING: The debug report was not created, it threw the following error message:")
println("${e}")
}
}