Add ability to search images by their tag

Change-Id: Ic3e05896018bb160fdc6b3a9b2ed591d2b69e99f
diff --git a/src/com/mirantis/mcp/MCPArtifactory.groovy b/src/com/mirantis/mcp/MCPArtifactory.groovy
index 5b63082..1e38ff6 100644
--- a/src/com/mirantis/mcp/MCPArtifactory.groovy
+++ b/src/com/mirantis/mcp/MCPArtifactory.groovy
@@ -118,6 +118,50 @@
 }
 
 /**
+ * Find docker images by tag
+ * Returns Array of image' hashes with names as full path in @repo
+ *
+ * Example:
+ *
+ *   [ {
+ *       "path" : "mirantis/ccp/ci-cd/gerrit-manage/test"
+ *     },
+ *     {
+ *       "path" : "mirantis/ccp/ci-cd/gerrit/test"
+ *     }
+ *   ]
+ *
+ * @param artifactoryURL String, an URL to Artifactory
+ * @param repo String, a name of repo where should be executed search
+ * @param tag String, tag of searched image
+ */
+def getImagesByTag(String artifactoryURL, String repo, String tag) {
+    def url = "${artifactoryURL}/api/search/aql"
+    def result
+    writeFile file: "query",
+            text: """\
+                   items.find(
+                     {
+                       \"repo\": \"${repo}\",
+                       \"@docker.manifest\": { \"\$match\" : \"${tag}*\" }
+                     }
+                   ).
+                   include(\"path\")
+            """.stripIndent()
+    withCredentials([
+        [$class: 'UsernamePasswordMultiBinding',
+         credentialsId: 'artifactory',
+         passwordVariable: 'ARTIFACTORY_PASSWORD',
+         usernameVariable: 'ARTIFACTORY_LOGIN']
+    ]) {
+       result = sh(script: "bash -c \"curl -X POST -u ${ARTIFACTORY_LOGIN}:${ARTIFACTORY_PASSWORD} -d @query  \'${url}\'\"",
+                   returnStdout: true).trim()
+    }
+    def images = new groovy.json.JsonSlurperClassic().parseText(result)
+    return images.get("results")
+}
+
+/**
  * Upload docker image to Artifactory
  *
  * @param server ArtifactoryServer, the instance of Artifactory server