Add methods to wokr with git
- method for uploading a review using git push with defined
topic
- method for generating a commit message with defined
change id.
Change-Id: I1999bfb4dba10432a58065930d93c842d19779b8
Related-Prod: https://mirantis.jira.com/browse/PROD-31022
diff --git a/src/com/mirantis/mk/Git.groovy b/src/com/mirantis/mk/Git.groovy
index 36292aa..00df961 100644
--- a/src/com/mirantis/mk/Git.groovy
+++ b/src/com/mirantis/mk/Git.groovy
@@ -407,3 +407,68 @@
return "${next_version}-${pre_release_meta.join('.')}-${commit_sha}"
}
}
+
+
+/**
+ * Method for uploading a change request
+ *
+ * @param repo String which contains path to directory with git repository
+ * @param credentialsId Credentials id to use for accessing target repositories
+ * @param commit Id of commit which should be uploaded
+ * @param branch Name of the branch for uploading
+ * @param topic Topic of the change
+ *
+ */
+def pushForReview(repo, credentialsId, commit, branch, topic='', remote='origin') {
+ def common = new com.mirantis.mk.Common()
+ def ssh = new com.mirantis.mk.Ssh()
+ common.infoMsg("Uploading commit ${commit} to ${branch} for review...")
+
+ def pushArg = "${commit}:refs/for/${branch}"
+ def process = [:]
+ if (topic){
+ pushArg += '%topic=' + topic
+ }
+ dir(repo){
+ ssh.prepareSshAgentKey(credentialsId)
+ ssh.runSshAgentCommand("git push ${remote} ${pushArg}")
+ }
+}
+
+/**
+ * Generates a commit message with predefined or auto generate change id. If change
+ * id isn't provided, changeIdSeed and current sha of git head will be used in
+ * generation of commit change id.
+ *
+ * @param repo String which contains path to directory with git repository
+ * @param message Commit message main part
+ * @param changeId User defined change-id usually sha1 hash
+ * @param changeIdSeed Custom part of change id which can be added during change id generation
+ *
+ *
+ * @return commitMessage Multiline String with generated commit message
+ */
+def genCommitMessage(repo, message, changeId = '', changeIdSeed = ''){
+ def git = new com.mirantis.mk.Git()
+ def common = new com.mirantis.mk.Common()
+ def commitMessage
+ def id = changeId
+ def seed = changeIdSeed
+ if (!id) {
+ if (!seed){
+ seed = common.generateRandomHashString(32)
+ }
+ def head_sha
+ dir(repo){
+ head_sha = git.getGitCommit()
+ }
+ id = 'I' + sh(script: 'echo -n ' + seed + head_sha + ' | sha1sum | awk \'{print $1}\'', returnStdout: true)
+ }
+ commitMessage =
+ """${message}
+
+ |Change-Id: ${id}
+ """.stripMargin()
+
+ return commitMessage
+}