How to get commit message from the latest gerrit patchset?

Hi, there

Is there a way to get the latest patchset commit message when a event is triggered?

Current Gerrit Trigger Events:

  • PatchSet Created
  • Draft Published

Here is my trial:

  1. Using the Jenkinsfile: Only get the lastest merged commit message
pipeline {
    agent any
    stages {
        stage('SCM') {
            steps {
                // Please note that
                // 'checkout scm' is only available
                // when using "Multibranch Pipeline" or "Pipeline script from SCM"
                checkout scm
            }
        }
        stage('Conventional Analysis') {
            steps {
                script {
                    def commitMessage = sh(returnStdout: true, script: 'git log -1 --pretty=%B').trim()
                    def match = (commitMessage =~ /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z]+\))?:\s.+$/)
                    if (!match) {
                        echo "Non-Conventional Commit: ${commitMessage}"
                        error("Commit message does not follow conventional commit format")
                    } else {
                        echo "Conventional Commit: ${commitMessage}"
                    }
                }
            }
        }
    }
}
  1. Using Pipeline script: changeset.size is always 0
pipeline {
    agent any
    stages {
        stage('Print commit messages') {
            steps {
                script {
                    def changeLogSets = currentBuild.changeSets
                    echo "Size: ${changeLogSets.size()}"
                    for (int i = 0; i < changeLogSets.size(); i++) {
                        def entries = changeLogSets[i].items
                        echo "Length: ${entries.length}"
                        for (int j = 0; j < entries.length; j++) {
                            def entry = entries[j]
                            echo entry.msg
                        }
                    }
                }
            }
        }
    }
}

:smile: Could anyone give me some help? Thanks a lot.

FYI.
Here is my final solution:

pipeline {
    agent any
    stages {
        stage('Conventional Analysis') {
            steps {
                script {
                    echo "GERRIT_CHANGE_SUBJECT: ${GERRIT_CHANGE_SUBJECT}"
                    def changeSubj = "$GERRIT_CHANGE_SUBJECT".trim()
                    def match = (changeSubj =~ /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z]+\))?:\s.+$/)
                    if (!match) {
                        echo "Non-Conventional Commit: \n${changeSubj}"
                        error("Commit message does not follow conventional commit format")
                    } else {
                        echo "Conventional Commit: \n${changeSubj}"
                    }
                }
            }
        }
        stage('Change-Id Required') {
            steps {
                script {
                    echo "GERRIT_CHANGE_COMMIT_MESSAGE: ${GERRIT_CHANGE_COMMIT_MESSAGE}"
                    def encodedString = "$GERRIT_CHANGE_COMMIT_MESSAGE"
                    def decodedBytes = java.util.Base64.decoder.decode(encodedString)
                    def commitMessage = new String(decodedBytes, "UTF-8")
                    // trim for each line and filter out the empty line
                    def lines = commitMessage.split('\n').collect { it.trim() }.findAll { it }
                    def changeIdCount = lines.count { it.startsWith('Change-Id: ') }
                    if (changeIdCount < 1) {
                        errors("Change-Id is required.")
                    }
                    if (changeIdCount > 1) {
                        // Change-Id: occurrences are over than 1
                        errors("Change-Id occurrences are over than 1. Please remove the redundant.")
                    }
                }
            }
        }
    }
}