Last active
April 10, 2023 08:21
-
-
Save pplmx/7111c6e5ca0bb408aca1f6699dbcf7c5 to your computer and use it in GitHub Desktop.
Conventional Commit Check for Gerrit PatchSet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://community.jenkins.io/t/how-to-get-commit-message-from-the-latest-gerrit-patchset/6747/3?u=pplmx