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

     1  import com.sap.piper.ConfigurationHelper
     2  import com.sap.piper.GenerateStageDocumentation
     3  import com.sap.piper.StageNameProvider
     4  import com.sap.piper.Utils
     5  import groovy.transform.Field
     6  
     7  import static com.sap.piper.Prerequisites.checkScript
     8  
     9  @Field String STEP_NAME = getClass().getName()
    10  @Field String TECHNICAL_STAGE_NAME = 'pullRequestVoting'
    11  
    12  @Field Set GENERAL_CONFIG_KEYS = [
    13      /**
    14       * Defines the build tool used.
    15       * @possibleValues `docker`, `kaniko`, `maven`, `mta`, `npm`
    16       */
    17      'buildTool'
    18  ]
    19  @Field STAGE_STEP_KEYS = [
    20      /** Triggers the build execution. */
    21      'buildExecute',
    22      /** Publishes check results to Jenkins. It will always be active. */
    23      'checksPublishResults',
    24      /**
    25       * Executes karma tests. For example suitable for OPA5 testing as well as QUnit testing of SAP UI5 apps.<br />
    26       * This step is not active by default. It can be activated by:
    27       *
    28       * * using pull request comments or pull request lables (see [Advanced Pull-Request Voting](#advanced-pull-request-voting).
    29       * * explicit activation via stage configuration.
    30       */
    31      'karmaExecuteTests',
    32      /** Runs backend integration tests via maven in the module integration-tests/pom.xml */
    33      'mavenExecuteIntegration',
    34      /** Executes static code checks for Maven based projects. The plugins SpotBugs and PMD are used. */
    35      'mavenExecuteStaticCodeChecks',
    36      /** Executes linting for npm projects. */
    37      'npmExecuteLint',
    38      /** Executes npm scripts to run frontend unit tests.
    39       * If custom names for the npm scripts are configured via the `runScripts` parameter the step npmExecuteScripts needs **explicit activation via stage configuration**. */
    40      'npmExecuteScripts',
    41      /** Executes a Sonar scan.*/
    42      'sonarExecuteScan',
    43      /** Publishes test results to Jenkins. It will always be active. */
    44      'testsPublishResults',
    45      /** Executes a WhiteSource scan
    46       * This step is not active by default. It can be activated by:
    47       *
    48       * * using pull request comments or pull request lables (see [Advanced Pull-Request Voting](#advanced-pull-request-voting).
    49       * * explicit activation via stage configuration.
    50       */
    51      'whitesourceExecuteScan'
    52  ]
    53  @Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus(STAGE_STEP_KEYS)
    54  @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
    55  
    56  /**
    57   * This stage is executed for every pull-request.<br />
    58   * For non-Docker builds it will execute the respective build (including unit tests, static checks, ...).
    59   *
    60   * !!! note "Build Tool not in the list?"
    61   *
    62   *     For build tools which are currently not in the list a custom `dockerImage` can be used with a custom `dockerCommand` as per step [buildExecute](../steps/buildExecute.md)
    63   *
    64   * For `buildTool: docker` a local Docker build will be executed in case a Docker deamon is available, if not `buildTool: 'kaniko'` will be used instead.
    65   *
    66   * ## Advanced Pull-Request Voting
    67   *
    68   * It is possible to trigger dedicated tests/checks
    69   *
    70   * * pull request comments
    71   * * pull request labels
    72   *
    73   * Following steps are currently supported
    74   *
    75   * | step name | comment | pull-request label |
    76   * | --------- | ------- | ------------------ |
    77   * | karmaExecuteTests | `/piper karma` | `pr_karma`
    78   * | whitesourceExecuteScan | `/piper whitesource` | `pr_whitesource`
    79   *
    80   */
    81  @GenerateStageDocumentation(defaultStageName = 'Pull-Request Voting')
    82  void call(Map parameters = [:]) {
    83  
    84      def script = checkScript(this, parameters) ?: this
    85      def utils = parameters.juStabUtils ?: new Utils()
    86      def stageName = StageNameProvider.instance.getStageName(script, parameters, this)
    87  
    88      Map config = ConfigurationHelper.newInstance(this)
    89          .loadStepDefaults()
    90          .mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
    91          .mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
    92          .mixin(parameters, PARAMETER_KEYS)
    93          .addIfEmpty('karmaExecuteTests', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.karmaExecuteTests)
    94          .addIfEmpty('mavenExecuteIntegration', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.mavenExecuteIntegration)
    95          .addIfEmpty('mavenExecuteStaticCodeChecks', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.mavenExecuteStaticCodeChecks)
    96          .addIfEmpty('npmExecuteLint', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.npmExecuteLint)
    97          .addIfEmpty('npmExecuteScripts', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.npmExecuteScripts)
    98          .addIfEmpty('whitesourceExecuteScan', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.whitesourceExecuteScan)
    99          .use()
   100  
   101      piperStageWrapper (script: script, stageName: stageName) {
   102  
   103          // telemetry reporting
   104          utils.pushToSWA([step: STEP_NAME], config)
   105  
   106          durationMeasure(script: script, measurementName: 'voter_duration') {
   107  
   108              //prevent push to registry in case of docker/kaniko
   109              def dockerRegistryUrl = null
   110              if (config.buildTool in ['docker', 'kaniko']) {
   111                  dockerRegistryUrl = ''
   112              }
   113  
   114              buildExecute script: script, buildTool: config.buildTool, dockerRegistryUrl: dockerRegistryUrl
   115              try {
   116                  //needs to run right after build, otherwise we may face "ERROR: Test reports were found but none of them are new"
   117                  testsPublishResults script: script
   118                  checksPublishResults script: script
   119              } finally {
   120                  if (config.sonarExecuteScan) {
   121                      sonarExecuteScan script: script
   122                  }
   123              }
   124  
   125              if (config.karmaExecuteTests) {
   126                  karmaExecuteTests script: script
   127                  testsPublishResults script: script
   128              }
   129  
   130              if (config.mavenExecuteIntegration) {
   131                  runMavenIntegrationTests(script)
   132              }
   133  
   134              if (config.mavenExecuteStaticCodeChecks) {
   135                  mavenExecuteStaticCodeChecks script: script
   136              }
   137  
   138              if (config.npmExecuteLint) {
   139                  npmExecuteLint script: script
   140              }
   141  
   142              if (config.npmExecuteScripts) {
   143                  npmExecuteScripts script: script
   144                  testsPublishResults script: script
   145              }
   146  
   147              if (config.whitesourceExecuteScan) {
   148                  whitesourceExecuteScan script: script, productVersion: env.BRANCH_NAME
   149              }
   150          }
   151      }
   152  }
   153  
   154  private runMavenIntegrationTests(script){
   155      boolean publishResults = false
   156      try {
   157          writeTemporaryCredentials(script: script) {
   158              publishResults = true
   159              mavenExecuteIntegration script: script
   160          }
   161      }
   162      finally {
   163          if (publishResults) {
   164              testsPublishResults script: script
   165          }
   166      }
   167  }