Fixed working with approvals from gerrit

Change-Id: I18c545bed235d4cbd14e206b6243df5f4b68e531
diff --git a/src/com/mirantis/mk/Gerrit.groovy b/src/com/mirantis/mk/Gerrit.groovy
index 9ff0454..cb539e4 100644
--- a/src/com/mirantis/mk/Gerrit.groovy
+++ b/src/com/mirantis/mk/Gerrit.groovy
@@ -150,14 +150,19 @@
  * @param gerritHost gerrit host (usually GERRIT_HOST property)
  * @param gerritChangeNumber gerrit change number (usually GERRIT_CHANGE_NUMBER property)
  * @param credentialsId jenkins credentials id for gerrit
+ * @param includeCurrentPatchset do you want to include current (last) patchset
  * @return gerrit change object
  */
-def getGerritChange(gerritName, gerritHost, gerritChangeNumber, credentialsId){
+def getGerritChange(gerritName, gerritHost, gerritChangeNumber, credentialsId, includeCurrentPatchset = false){
     def common = new com.mirantis.mk.Common()
     def ssh = new com.mirantis.mk.Ssh()
     ssh.prepareSshAgentKey(credentialsId)
     ssh.ensureKnownHosts(gerritHost)
-    return common.parseJSON(ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit query --current-patch-set --format=JSON change:%s", gerritName, gerritHost, gerritChangeNumber)))
+    def curPatchset = "";
+    if(includeCurrentPatchset){
+        curPatchset = "--current-patch-set"
+    }
+    return common.parseJSON(ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit query ${curPatchset} --format=JSON change:%s", gerritName, gerritHost, gerritChangeNumber)))
 }
 
 /**
@@ -182,16 +187,22 @@
     }
 }
 /**
- * Returns boolean result of test given gerrit change for given approval type and value
- * @param gerritChange user gerrit change
+ * Returns boolean result of test given gerrit patchset for given approval type and value
+ * @param patchset gerrit patchset
  * @param approvalType type of tested approval (optional, default Verified)
  * @param approvalValue value of tested approval (optional, default 1)
  * @return boolean result
+ * @example patchsetHasApproval(gerrit.getGerritChange(*,*,*,*, true).currentPatchSet)
  */
-def changeHasApproval(gerritChange, approvalType="Verified", approvalValue="1"){
-  if(gerritChange.currentPatchSet && gerritChange.currentPatchSet.approvals){
-    def numberOfVerified = gerritChange.currentPatchSet.approvals.stream().filter{ approval -> approval.type.equals(approvalType) && approval.value.equals(approvalValue)}.collect(java.util.stream.Collectors.counting());
-    return numberOfVerified.intValue() > 0;
+@NonCPS
+def patchsetHasApproval(patchSet, approvalType="Verified", approvalValue="1"){
+  if(patchSet && patchSet.approvals){
+    for(int i=0; i < patchSet.approvals.size();i++){
+      def approval = patchSet.approvals.get(i)
+      if(approval.type.equals(approvalType) && approval.value.equals(approvalValue)){
+        return true
+      }
+    }
   }
   return false
 }