Add methods needed to organize gating on ci
Added methods for parsing gerrit event text and for downloading changes
from gerrit.
Related-Prod: https://mirantis.jira.com/browse/PRODX-269
Needed-By: https://gerrit.mcp.mirantis.com/c/mcp/mcp-pipelines/+/66777
Change-Id: Ie0316e698058afdee6a4fa767281f41543119a04
diff --git a/src/com/mirantis/mk/Gerrit.groovy b/src/com/mirantis/mk/Gerrit.groovy
index 85b0411..c33dc5e 100644
--- a/src/com/mirantis/mk/Gerrit.groovy
+++ b/src/com/mirantis/mk/Gerrit.groovy
@@ -460,3 +460,71 @@
return getGerritChange(gerritUser, gerritHost, jsonChange['number'], gerritCredentials, true)
}
}
+
+/**
+ * Download change from gerrit, if needed repository maybe pre cloned
+ *
+ * @param path Directory to checkout repository to
+ * @param url Source Gerrit repository URL
+ * @param reference Gerrit reference to download (e.g refs/changes/77/66777/16)
+ * @param type type of gerrit download
+ * @param credentialsId Credentials ID to use for source Git
+ * @param gitCheckoutParams map with additional parameters for git.checkoutGitRepository method e.g:
+ * [ branch: 'master', poll: true, timeout: 10, depth: 0 ]
+ * @param doGitClone boolean whether to pre clone and do some checkout
+ */
+def downloadChange(path, url, reference, credentialsId, type = 'cherry-pick', doGitClone = true, Map gitCheckoutParams = [:]){
+ def git = new com.mirantis.mk.Git()
+ def ssh = new com.mirantis.mk.Ssh()
+ def cmd
+ switch(type) {
+ case 'cherry-pick':
+ cmd = "git fetch ${url} ${reference} && git cherry-pick FETCH_HEAD"
+ break;
+ case 'format-patch':
+ cmd = "git fetch ${url} ${reference} && git format-patch -1 --stdout FETCH_HEAD"
+ break;
+ case 'pull':
+ cmd = "git pull ${url} ${reference}"
+ break;
+ default:
+ error("Unsupported gerrit download type")
+ }
+ if (doGitClone) {
+ def branch = gitCheckoutParams.get('branch', 'master')
+ def poll = gitCheckoutParams.get('poll', true)
+ def timeout = gitCheckoutParams.get('timeout', 10)
+ def depth = gitCheckoutParams.get('depth', 0)
+ git.checkoutGitRepository(path, url, branch, credentialsId, poll, timeout, depth, '')
+ }
+ ssh.prepareSshAgentKey(credentialsId)
+ dir(path){
+ ssh.agentSh(cmd)
+ }
+}
+
+/**
+ * Parse gerrit event text and if Workflow +1 is detected,
+ * return true
+ *
+ * @param gerritEventCommentText gerrit event comment text
+ * @param gerritEventType type of gerrit event
+ */
+def isGate(gerritEventCommentText, gerritEventType) {
+ def common = new com.mirantis.mk.Common()
+ def gerritEventCommentTextStr = ''
+ def res = false
+ if (gerritEventCommentText) {
+ try{
+ gerritEventCommentTextStr = new String(gerritEventCommentText.decodeBase64())
+ } catch (e) {
+ gerritEventCommentTextStr = gerritEventCommentText
+ }
+ common.infoMsg("GERRIT_EVENT_COMMENT_TEXT is ${gerritEventCommentTextStr}")
+ }
+ if (gerritEventType == 'comment-added' && gerritEventCommentTextStr =~ /^Patch Set \d+:\s.*Workflow\+1$/) {
+ common.infoMsg("Running in gate mode")
+ res = true
+ }
+ return res
+}