2018-09-21 16:55:31 +02:00
import static com . sap . piper . Prerequisites . checkScript
2018-01-29 17:47:22 +02:00
import com.cloudbees.groovy.cps.NonCPS
2018-02-01 13:57:25 +02:00
2019-03-28 14:27:41 +02:00
import com.sap.piper.GenerateDocumentation
2018-08-15 11:53:28 +02:00
import com.sap.piper.ConfigurationHelper
2018-02-08 11:21:38 +02:00
import com.sap.piper.MapUtils
2018-08-15 11:53:28 +02:00
import com.sap.piper.Utils
2018-01-29 17:47:22 +02:00
2018-02-01 13:57:25 +02:00
import groovy.transform.Field
2018-11-29 10:54:05 +02:00
@Field def STEP_NAME = getClass ( ) . getName ( )
2018-08-15 11:53:28 +02:00
2018-02-19 13:53:08 +02:00
@Field Set TOOLS = [
2019-03-28 14:27:41 +02:00
/ * *
* Allows to publish the check results .
* @possibleValues ` true ` , ` false ` , ` Map `
* /
'aggregation' ,
/ * *
* Searches and publishes TODOs in files with the [ Task Scanner Plugin ] ( https: //wiki.jenkins-ci.org/display/JENKINS/Task+Scanner+Plugin).
* @possibleValues ` true ` , ` false ` , ` Map `
* /
'tasks' ,
/ * *
* Publishes PMD findings with the [ PMD plugin ] ( https: //plugins.jenkins.io/pmd).
* @possibleValues ` true ` , ` false ` , ` Map `
* /
'pmd' ,
/ * *
* Publishes CPD findings with the [ DRY plugin ] ( https: //plugins.jenkins.io/dry).
* @possibleValues ` true ` , ` false ` , ` Map `
* /
'cpd' ,
/ * *
* Publishes Findbugs findings with the [ Findbugs plugin ] ( https: //plugins.jenkins.io/findbugs).
* @possibleValues ` true ` , ` false ` , ` Map `
* /
'findbugs' ,
/ * *
* Publishes Checkstyle findings with the [ Checkstyle plugin ] ( https: //plugins.jenkins.io/checkstyle).
* @possibleValues ` true ` , ` false ` , ` Map `
* /
'checkstyle' ,
/ * *
* Publishes ESLint findings ( in [ JSLint format ] ( https: //eslint.org/docs/user-guide/formatters/)) with the [Warnings plugin](https://plugins.jenkins.io/warnings).
* @possibleValues ` true ` , ` false ` , ` Map `
* /
'eslint' ,
/ * *
* Publishes PyLint findings with the [ Warnings plugin ] ( https: //plugins.jenkins.io/warnings), pylint needs to run with `--output-format=parseable` option.
* @possibleValues ` true ` , ` false ` , ` Map `
* /
'pylint'
2018-02-16 11:08:48 +02:00
]
2018-01-31 17:56:18 +02:00
2018-08-15 11:53:28 +02:00
@Field Set GENERAL_CONFIG_KEYS = [ ]
@Field Set STEP_CONFIG_KEYS = TOOLS . plus ( [ 'archive' ] )
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
2018-01-29 17:47:22 +02:00
/ * *
2019-03-28 14:27:41 +02:00
* This step can publish static check results from various sources .
2018-01-29 17:47:22 +02:00
* /
2019-03-28 14:27:41 +02:00
@GenerateDocumentation
2018-08-30 16:33:07 +02:00
void call ( Map parameters = [ : ] ) {
2018-02-01 13:57:25 +02:00
handlePipelineStepErrors ( stepName: STEP_NAME , stepParameters: parameters ) {
2018-09-21 16:55:31 +02:00
def script = checkScript ( this , parameters )
2018-02-01 13:57:25 +02:00
if ( script = = null )
2018-10-31 09:40:12 +02:00
script = this
2018-08-15 11:53:28 +02:00
2018-02-08 11:53:48 +02:00
prepare ( parameters )
2018-03-06 13:48:16 +02:00
2018-08-15 11:53:28 +02:00
// load default & individual configuration
2018-10-17 11:05:20 +02:00
Map configuration = ConfigurationHelper . newInstance ( this )
2018-09-07 10:08:16 +02:00
. loadStepDefaults ( )
2018-08-15 11:53:28 +02:00
. mixinGeneralConfig ( script . commonPipelineEnvironment , GENERAL_CONFIG_KEYS )
. mixinStepConfig ( script . commonPipelineEnvironment , STEP_CONFIG_KEYS )
. mixinStageConfig ( script . commonPipelineEnvironment , parameters . stageName ? : env . STAGE_NAME , STEP_CONFIG_KEYS )
. mixin ( parameters , PARAMETER_KEYS )
. use ( )
2018-02-08 11:53:48 +02:00
2019-01-21 09:47:34 +02:00
new Utils ( ) . pushToSWA ( [
step: STEP_NAME ,
stepParamKey1: 'scriptMissing' ,
stepParam1: parameters ? . script = = null
] , configuration )
2018-02-01 13:57:25 +02:00
2018-01-29 17:29:00 +02:00
// JAVA
2018-08-15 11:53:28 +02:00
report ( 'PmdPublisher' , configuration . pmd , configuration . archive )
report ( 'DryPublisher' , configuration . cpd , configuration . archive )
report ( 'FindBugsPublisher' , configuration . findbugs , configuration . archive )
report ( 'CheckStylePublisher' , configuration . checkstyle , configuration . archive )
2018-01-29 17:29:00 +02:00
// JAVA SCRIPT
2018-08-15 11:53:28 +02:00
reportWarnings ( 'JSLint' , configuration . eslint , configuration . archive )
2018-01-29 17:29:00 +02:00
// PYTHON
2018-08-15 11:53:28 +02:00
reportWarnings ( 'PyLint' , configuration . pylint , configuration . archive )
2018-02-01 13:57:25 +02:00
// GENERAL
2018-08-15 11:53:28 +02:00
reportTasks ( configuration . tasks )
aggregateReports ( configuration . aggregation )
2018-01-29 17:29:00 +02:00
}
}
2018-02-08 11:52:10 +02:00
def aggregateReports ( settings ) {
2018-02-01 13:57:25 +02:00
if ( settings . active ) {
2018-01-29 17:29:00 +02:00
def options = createCommonOptionsMap ( 'AnalysisPublisher' , settings )
// publish
step ( options )
}
}
2018-02-01 13:57:25 +02:00
def reportTasks ( settings ) {
if ( settings . active ) {
def options = createCommonOptionsMap ( 'TasksPublisher' , settings )
options . put ( 'pattern' , settings . get ( 'pattern' ) )
options . put ( 'high' , settings . get ( 'high' ) )
options . put ( 'normal' , settings . get ( 'normal' ) )
options . put ( 'low' , settings . get ( 'low' ) )
// publish
step ( options )
}
}
def report ( publisherName , settings , doArchive ) {
if ( settings . active ) {
def pattern = settings . get ( 'pattern' )
2018-02-01 09:22:12 +02:00
def options = createCommonOptionsMap ( publisherName , settings )
2018-01-30 14:14:05 +02:00
options . put ( 'pattern' , pattern )
2018-01-29 17:29:00 +02:00
// publish
step ( options )
// archive check results
2018-02-01 13:57:25 +02:00
archiveResults ( doArchive & & settings . get ( 'archive' ) , pattern , true )
2018-01-29 17:29:00 +02:00
}
}
2018-02-01 13:57:25 +02:00
def reportWarnings ( parserName , settings , doArchive ) {
if ( settings . active ) {
def pattern = settings . get ( 'pattern' )
2018-01-29 17:29:00 +02:00
def options = createCommonOptionsMap ( 'WarningsPublisher' , settings )
options . put ( 'parserConfigurations' , [ [
parserName: parserName ,
2018-01-30 14:14:05 +02:00
pattern: pattern
2018-01-29 17:29:00 +02:00
] ] )
// publish
step ( options )
// archive check results
2018-02-01 13:57:25 +02:00
archiveResults ( doArchive & & settings . get ( 'archive' ) , pattern , true )
2018-01-29 17:29:00 +02:00
}
}
def archiveResults ( archive , pattern , allowEmpty ) {
if ( archive ) {
2018-02-01 13:57:25 +02:00
echo "[${STEP_NAME}] archive ${pattern}"
2018-01-29 17:29:00 +02:00
archiveArtifacts artifacts: pattern , allowEmptyArchive: allowEmpty
}
}
2018-01-30 14:15:49 +02:00
@NonCPS
2018-01-30 11:06:35 +02:00
def createCommonOptionsMap ( publisherName , settings ) {
2018-01-30 14:15:49 +02:00
Map result = [ : ]
2018-02-08 10:58:58 +02:00
def thresholds = settings . get ( 'thresholds' , [ : ] )
def fail = thresholds . get ( 'fail' , [ : ] )
def unstable = thresholds . get ( 'unstable' , [ : ] )
2018-01-29 17:29:00 +02:00
2018-01-30 11:06:35 +02:00
result . put ( '$class' , publisherName )
2018-02-01 13:57:25 +02:00
result . put ( 'healthy' , settings . get ( 'healthy' ) )
result . put ( 'unHealthy' , settings . get ( 'unHealthy' ) )
2018-01-29 17:29:00 +02:00
result . put ( 'canRunOnFailed' , true )
2018-02-08 10:58:58 +02:00
result . put ( 'failedTotalAll' , fail . get ( 'all' ) )
result . put ( 'failedTotalHigh' , fail . get ( 'high' ) )
result . put ( 'failedTotalNormal' , fail . get ( 'normal' ) )
result . put ( 'failedTotalLow' , fail . get ( 'low' ) )
result . put ( 'unstableTotalAll' , unstable . get ( 'all' ) )
result . put ( 'unstableTotalHigh' , unstable . get ( 'high' ) )
result . put ( 'unstableTotalNormal' , unstable . get ( 'normal' ) )
result . put ( 'unstableTotalLow' , unstable . get ( 'low' ) )
2018-02-08 11:43:24 +02:00
// filter empty values
result = result . findAll {
return it . value ! = null & & it . value ! = ''
}
2018-01-29 17:29:00 +02:00
return result
}
2018-02-01 13:57:25 +02:00
def prepare ( parameters ) {
2018-02-08 11:51:39 +02:00
// ensure tool maps are initialized correctly
2018-02-16 11:08:48 +02:00
for ( String tool : TOOLS ) {
2018-02-08 11:51:39 +02:00
parameters [ tool ] = toMap ( parameters [ tool ] )
}
2018-02-01 13:57:25 +02:00
return parameters
}
2018-02-08 11:50:11 +02:00
def toMap ( parameter ) {
if ( MapUtils . isMap ( parameter ) )
parameter . put ( 'active' , parameter . active = = null ? true : parameter . active )
else if ( Boolean . TRUE . equals ( parameter ) )
parameter = [ active: true ]
else if ( Boolean . FALSE . equals ( parameter ) )
parameter = [ active: false ]
else
parameter = [ : ]
return parameter
}