Search code examples
gitjenkinsjenkins-pipelinegerritgerrit-trigger

Recognize gerrit trivial rebase as event on jenkins


I need to find a way to perform part of my jenkins pipeline only when path trivial rebased.

if GERRIT_EVENT_TYPE=rebase{
stage ('A'){}
}
stage ('B'){}

Each rebase on gerrit creates new patch, I see on job parameters GERRIT_EVENT_TYPE=new_patch.

I know that as part of job configuration I can exclude build trigger on trivial rebase, so the plugin mechanism got the mechanism to recognize trivial rebase as an event, how can I explicit it ? Is there a way to that ?


Solution

  • cloning the repo with -2 depth and the comparing the parent ID doing the job

    if(env.GERRIT_PATCHSET_NUMBER.toInteger() > 1){  // the commit has previous revisions - let's compare their parents
            
            currParentSHA = checkoutRepoAndReturnParentSHA(env.GERRIT_PROJECT, env.GERRIT_REFSPEC)
            
            // calculate previous change refspec - reduce GERRIT_PATCHSET_NUMBER by 1
            tokenized_list = env.GERRIT_REFSPEC.tokenize('/') 
            tokenized_list[-1] = (env.GERRIT_PATCHSET_NUMBER.toInteger() - 1).toString()
            previous_patch_refspec = tokenized_list.join('/')
            
            prevParentSHA = checkoutRepoAndReturnParentSHA(env.GERRIT_PROJECT, previous_patch_refspec)
            
            isRebased = (currParentSHA != prevParentSHA) ? true : false
        }
    
    
    def checkoutRepoAndReturnParentSHA(gerrit_project, gerrit_refspec){ // clone current change into dedicated directory and return parent commit SHA
        dir(gerrit_refspec){ 
            dir(gerrit_project){
                return sh(returnStdout: true, script: 'git rev-parse HEAD^1').trim() // get parent commit SHA 
            }   
        }
    }