github.com/xgoffin/jenkins-library@v1.154.0/vars/piperPublishWarnings.groovy (about)

     1  import static com.sap.piper.Prerequisites.checkScript
     2  
     3  import com.sap.piper.ConfigurationHelper
     4  import com.sap.piper.GenerateDocumentation
     5  import com.sap.piper.JenkinsUtils
     6  import com.sap.piper.Utils
     7  
     8  import groovy.transform.Field
     9  
    10  @Field String STEP_NAME = getClass().getName()
    11  @Field List PLUGIN_ID_LIST = ['warnings-ng']
    12  
    13  @Field Set GENERAL_CONFIG_KEYS = []
    14  @Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus([
    15      /**
    16       * The id of the Groovy script parser. If the id is not present in the current Jenkins configuration it is created.
    17       */
    18      'parserId',
    19      /**
    20       * The display name for the warnings parsed by the parser.
    21       * Only considered if a new parser is created.
    22       */
    23      'parserName',
    24      /**
    25       * The pattern used to parse the log file.
    26       * Only considered if a new parser is created.
    27       */
    28      'parserPattern',
    29      /**
    30       * The script used to parse the matches produced by the pattern into issues.
    31       * Only considered if a new parser is created.
    32       * see https://github.com/jenkinsci/analysis-model/blob/master/src/main/java/edu/hm/hafner/analysis/IssueBuilder.java
    33       */
    34      'parserScript',
    35      /**
    36       * Settings that are passed to the recordIssues step of the warnings-ng plugin.
    37       * see https://github.com/jenkinsci/warnings-ng-plugin/blob/master/doc/Documentation.md#configuration
    38       */
    39      'recordIssuesSettings'
    40  ])
    41  @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS.plus([])
    42  
    43  /**
    44   * This step scans the current build log for messages produces by the Piper library steps and publishes them on the Jenkins job run as *Piper warnings* via the warnings-ng plugin.
    45   *
    46   * The default parser detects log entries with the following pattern: `[<SEVERITY>] <MESSAGE> (<LIBRARY>/<STEP>)`
    47   */
    48  @GenerateDocumentation
    49  void call(Map parameters = [:]) {
    50      handlePipelineStepErrors (stepName: STEP_NAME, stepParameters: parameters, allowBuildFailure: true) {
    51  
    52          final script = checkScript(this, parameters) ?: this
    53          String stageName = parameters.stageName ?: env.STAGE_NAME
    54  
    55          for(String id : PLUGIN_ID_LIST){
    56              if (!JenkinsUtils.isPluginActive(id)) {
    57                  error("[ERROR][${STEP_NAME}] The step requires the plugin '${id}' to be installed and activated in the Jenkins.")
    58              }
    59          }
    60  
    61          // load default & individual configuration
    62          Map configuration = ConfigurationHelper.newInstance(this)
    63              .loadStepDefaults([:], stageName)
    64              .mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
    65              .mixinStepConfig(script.commonPipelineEnvironment, STEP_CONFIG_KEYS)
    66              .mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
    67              .mixin(parameters, PARAMETER_KEYS)
    68              .use()
    69          // report to SWA
    70          new Utils().pushToSWA([
    71              step: STEP_NAME,
    72              stepParamKey1: 'scriptMissing',
    73              stepParam1: parameters?.script == null
    74          ], configuration)
    75  
    76          // add Piper Notifications parser to config if missing
    77          if(new JenkinsUtils().addWarningsNGParser(
    78              configuration.parserId,
    79              configuration.parserName,
    80              configuration.parserPattern,
    81              configuration.parserScript
    82          )){
    83              echo "[${STEP_NAME}] Added warnings-ng plugin parser '${configuration.parserName}' configuration."
    84          }
    85  
    86          writeFile file: 'build.log', text: JenkinsUtils.getFullBuildLog(script.currentBuild)
    87          // parse log for Piper Notifications
    88          recordIssues(
    89              configuration.recordIssuesSettings.plus([
    90                  tools: [groovyScript(
    91                      parserId: configuration.parserId,
    92                      pattern: 'build.log'
    93                  )]
    94              ])
    95          )
    96      }
    97  }