github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/src/com/sap/piper/GitUtils.groovy (about)

     1  package com.sap.piper
     2  
     3  boolean insideWorkTree() {
     4      return sh(returnStatus: true, script: 'git rev-parse --is-inside-work-tree 1>/dev/null 2>&1') == 0
     5  }
     6  
     7  boolean isMergeCommit() throws MissingPropertyException{
     8      for (def extension : scm.getExtensions()) {
     9          if(extension instanceof jenkins.plugins.git.MergeWithGitSCMExtension){
    10              return true;
    11          }
    12      }
    13  
    14      return false;
    15  }
    16  
    17  String getMergeCommitSha() throws MissingPropertyException{
    18      return pullRequest.mergeCommitSha
    19  }
    20  
    21  boolean isWorkTreeDirty() {
    22  
    23      if(!insideWorkTree()) error 'Method \'isWorkTreeClean\' called outside a git work tree.'
    24  
    25      def gitCmd = 'git diff --quiet HEAD'
    26      def rc = sh(returnStatus: true, script: gitCmd)
    27  
    28      // from git man page:
    29      // "it exits with 1 if there were differences and 0 means no differences"
    30      //
    31      // in case of general git trouble, e.g. outside work tree this is indicated by
    32      // a return code higher than 1.
    33      if(rc == 0) return false
    34      else if (rc == 1) return true
    35      else error "git command '${gitCmd}' return with code '${rc}'. This indicates general trouble with git."
    36  }
    37  
    38  String getGitCommitIdOrNull() {
    39      if ( insideWorkTree() ) {
    40          return getGitCommitId()
    41      } else {
    42          return null
    43      }
    44  }
    45  
    46  String getGitCommitId() {
    47      return sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
    48  }
    49  
    50  String[] extractLogLines(
    51      String filter = '',
    52      String from = 'origin/master',
    53      String to = 'HEAD',
    54      String format = '%b'
    55  ) {
    56  
    57      // Checks below: there was an value provided from outside, but the value was null.
    58      // Throwing an exception is more transparent than making a fallback to the defaults
    59      // used in case the parameter is omitted in the signature.
    60      if(filter == null) throw new IllegalArgumentException('Parameter \'filter\' not provided.')
    61      if(! from?.trim()) throw new IllegalArgumentException('Parameter \'from\' not provided.')
    62      if(! to?.trim()) throw new IllegalArgumentException('Parameter \'to\' not provided.')
    63      if(! format?.trim()) throw new IllegalArgumentException('Parameter \'format\' not provided.')
    64  
    65      def gitLogLines = sh ( returnStdout: true,
    66          script: """#!/bin/bash
    67              git log --pretty=format:${format} ${from}..${to}
    68          """
    69      )?.split('\n')
    70  
    71      // spread not supported here (CPS)
    72      if(gitLogLines) {
    73          def trimmedGitLogLines = []
    74          for(def gitLogLine : gitLogLines) {
    75              trimmedGitLogLines << gitLogLine.trim()
    76          }
    77          return trimmedGitLogLines.findAll { line -> line ==~ /${filter}/ }
    78      }
    79      return new String[0]
    80  
    81  }
    82  
    83  static String handleTestRepository(Script steps, Map config){
    84      def stashName = "testContent-${UUID.randomUUID()}".toString()
    85      def options = [url: config.testRepository]
    86      if (config.gitSshKeyCredentialsId)
    87          options.put('credentialsId', config.gitSshKeyCredentialsId)
    88      if (config.gitBranch)
    89          options.put('branch', config.gitBranch)
    90      // checkout test repository
    91      steps.git options
    92      // stash test content
    93      steps.stash stashName //TODO: should use new Utils().stash
    94      // return stash name
    95      return stashName
    96  }