Add methods for Gerrit CR upload
The patch adds git and gerrit methods to
upload CR to Gerrit using pipelines.
Git`s "commitGitChanges" method has been
modified to support "-- amend" option.
Change-Id: I6f61784d0edf956418b033aae2407f1f868d5067
Related-PROD: PROD-30632
diff --git a/src/com/mirantis/mk/Gerrit.groovy b/src/com/mirantis/mk/Gerrit.groovy
index 33da618..b65432d 100644
--- a/src/com/mirantis/mk/Gerrit.groovy
+++ b/src/com/mirantis/mk/Gerrit.groovy
@@ -312,3 +312,74 @@
}
return dependentPatches
}
+
+/**
+ * Find Gerrit change(s) according to various input parameters like owner, topic, etc.
+ * @param gerritAuth A map containing information about Gerrit. Should include
+ * HOST, PORT and USER
+ * @param changeParams Parameters to identify Geriit change e.g.: owner, topic,
+ * status, branch, project
+ */
+def findGerritChange(credentialsId, LinkedHashMap gerritAuth, LinkedHashMap changeParams) {
+ scriptText = """
+ ssh -p ${gerritAuth['PORT']} ${gerritAuth['USER']}@${gerritAuth['HOST']} \
+ gerrit query \
+ --format JSON \
+ """
+ changeParams.each {
+ scriptText += " ${it.key}:${it.value}"
+ }
+ scriptText += " | fgrep -v runTimeMilliseconds || :"
+ sshagent([credentialsId]) {
+ jsonChange = sh(
+ script:scriptText,
+ returnStdout: true,
+ ).trim()
+ }
+ return jsonChange
+}
+
+/**
+ * Download Gerrit review by number
+ *
+ * @param credentialsId credentials ID
+ * @param virtualenv virtualenv path
+ * @param repoDir repository directory
+ * @param gitRemote the value of git remote
+ * @param changeNum the number of change to download
+ */
+def getGerritChangeByNum(credentialsId, virtualEnv, repoDir, gitRemote, changeNum) {
+ def python = new com.mirantis.mk.Python()
+ sshagent([credentialsId]) {
+ dir(repoDir) {
+ python.runVirtualenvCommand(virtualEnv, "git review -r ${gitRemote} -d ${changeNum}")
+ }
+ }
+}
+
+/**
+ * Post Gerrit review
+ * @param credentialsId credentials ID
+ * @param virtualenv virtualenv path
+ * @param repoDir repository directory
+ * @param gitName committer name
+ * @param gitEmail committer email
+ * @param gitRemote the value of git remote
+ * @param gitTopic the name of the topic
+ * @param gitBranch the name of git branch
+ */
+def postGerritReview(credentialsId, virtualEnv, repoDir, gitName, gitEmail, gitRemote, gitTopic, gitBranch) {
+ def python = new com.mirantis.mk.Python()
+ def cmdText = """
+ GIT_COMMITTER_NAME=${gitName} \
+ GIT_COMMITTER_EMAIL=${gitEmail} \
+ git review -r ${gitRemote} \
+ -t ${gitTopic} \
+ ${gitBranch}
+ """
+ sshagent([credentialsId]) {
+ dir(repoDir) {
+ python.runVirtualenvCommand(virtualEnv, cmdText)
+ }
+ }
+}
diff --git a/src/com/mirantis/mk/Git.groovy b/src/com/mirantis/mk/Git.groovy
index 575fb80..d9006fa 100644
--- a/src/com/mirantis/mk/Git.groovy
+++ b/src/com/mirantis/mk/Git.groovy
@@ -100,13 +100,20 @@
* @param path Path to the git repository
* @param message A commit message
* @param global Use global config
+ * @param amend Whether to use "--amend" in commit command
*/
-def commitGitChanges(path, message, gitEmail='jenkins@localhost', gitName='jenkins-slave', global=false) {
+def commitGitChanges(path, message, gitEmail='jenkins@localhost', gitName='jenkins-slave', global=false, amend=false) {
def git_cmd
+ def gitOpts
def global_arg = ''
if (global) {
global_arg = '--global'
}
+ if (amend) {
+ gitOpts = '--amend'
+ } else {
+ gitOpts = ''
+ }
dir(path) {
sh "git config ${global_arg} user.email '${gitEmail}'"
sh "git config ${global_arg} user.name '${gitName}'"
@@ -116,14 +123,13 @@
returnStdout: true
).trim()
git_cmd = sh(
- script: "git commit -m '${message}'",
+ script: "git commit ${gitOpts} -m '${message}'",
returnStdout: true
).trim()
}
return git_cmd
}
-
/**
* Push git changes to remote repo
*
@@ -231,4 +237,5 @@
returnStdout: true
).trim()
return branchesList.tokenize('\n')
-}
\ No newline at end of file
+}
+