Add gerrit.listRepoTags method

- Indent getGerritChangeInfoByRefspec method to 4

Related: PRODX-48007
Change-Id: Ie606d9005683e252c9265fd9adf37b4f4b16bca7
diff --git a/src/com/mirantis/mk/Gerrit.groovy b/src/com/mirantis/mk/Gerrit.groovy
index 2e92444..cba856f 100644
--- a/src/com/mirantis/mk/Gerrit.groovy
+++ b/src/com/mirantis/mk/Gerrit.groovy
@@ -577,38 +577,70 @@
  * @return A map containing information about the Gerrit change.
  */
 def getGerritChangeInfoByRefspec(Map gerritAuth, String repoName, String refSpec) {
-  def common = new com.mirantis.mk.Common()
-  String commitHash = ''
-  if (refSpec.startsWith('refs/') || refSpec == 'master') {
-    // why we have retry here? bz infra is awesome and stable!
-    common.retry(15, 10) {
-      sshagent([gerritAuth['credentialsId']]) {
-        commitHash = sh(script: "git ls-remote ssh://${gerritAuth['USER']}@${gerritAuth['HOST']}:${gerritAuth['PORT']}/${repoName} ${refSpec}", returnStdout: true).trim().split('\\t')[0]
-      }
+    def common = new com.mirantis.mk.Common()
+    String commitHash = ''
+    if (refSpec.startsWith('refs/') || refSpec == 'master') {
+        // why we have retry here? bz infra is awesome and stable!
+        common.retry(15, 10) {
+            sshagent([gerritAuth['credentialsId']]) {
+                commitHash = sh(script: "git ls-remote ssh://${gerritAuth['USER']}@${gerritAuth['HOST']}:${gerritAuth['PORT']}/${repoName} ${refSpec}", returnStdout: true).trim().split('\\t')[0]
+            }
+        }
+    } else {
+        commitHash = refSpec
     }
-  } else {
-    commitHash = refSpec
-  }
-  if (!commitHash) {
-    common.errorMsg("Could not get commit hash for refspec '${refSpec}'")
-    return [:]
-  }
-  LinkedHashMap gerritQuery = [
-    'commit': commitHash,
-  ]
-  String gerritFlags = '--current-patch-set'
-  String patchsetRawJson = findGerritChange(gerritAuth['credentialsId'], gerritAuth, gerritQuery, gerritFlags)
-  if (!patchsetRawJson) {
-    // WARNING(alexz): gerrit search only by CR. so merge event,like https://gerrit.mcp.mirantis.com/plugins/gitiles/kaas/si-tests/+/cabb45ea73ac538c653d62cb0a2ffb4802521251
-    // will not be catched here. In that case, return dummy raw_commit_hash only
-    common.errorMsg("Could not find gerrit change as refspec '${refSpec}' (commit: ${commitHash})")
-    return ['raw_commit_hash': commitHash]
-  }
-  try {
-    // It can (and should) return only one json for given commitHash
-    return readYaml(text: patchsetRawJson)
-  } catch (Exception e) {
-    common.errorMsg("Could not parse JSON: ${patchsetRawJson}\nError: ${e}")
-    return [:]
-  }
+    if (!commitHash) {
+        common.errorMsg("Could not get commit hash for refspec '${refSpec}'")
+        return [:]
+    }
+    LinkedHashMap gerritQuery = [
+        'commit': commitHash,
+    ]
+    String gerritFlags = '--current-patch-set'
+    String patchsetRawJson = findGerritChange(gerritAuth['credentialsId'], gerritAuth, gerritQuery, gerritFlags)
+    if (!patchsetRawJson) {
+        // WARNING(alexz): gerrit search only by CR. so merge event,like https://gerrit.mcp.mirantis.com/plugins/gitiles/kaas/si-tests/+/cabb45ea73ac538c653d62cb0a2ffb4802521251
+        // will not be catched here. In that case, return dummy raw_commit_hash only
+        common.errorMsg("Could not find gerrit change as refspec '${refSpec}' (commit: ${commitHash})")
+        return ['raw_commit_hash': commitHash]
+    }
+    try {
+        // It can (and should) return only one json for given commitHash
+        return readYaml(text: patchsetRawJson)
+    } catch (Exception e) {
+        common.errorMsg("Could not parse JSON: ${patchsetRawJson}\nError: ${e}")
+        return [:]
+    }
 }
+
+/**
+ * Get gerrit repository tags with gerrit API.
+ *
+ * @param repoName The name of the repository.
+ * @param credentialsId jenkins credentials id.
+ * @param trimTag Whether to trim the leading 'v' from tags.
+ * @return A list containing all repository tags.
+ */
+def listRepoTags(String repoName, String credentialsId, Boolean trimTag = false) {
+    def common = new com.mirantis.mk.Common()
+    def replacePattern = trimTag ? 'refs/tags/v' : 'refs/tags/'
+    def coreTags = ''
+    def tags = []
+    withCredentials([usernamePassword(credentialsId: credentialsId,
+                     usernameVariable: 'GERRIT_USERNAME',
+                     passwordVariable: 'GERRIT_PASSWORD')]) {
+        common.retry(15, 10) {
+            coreTags = sh(script: "echo \${GERRIT_PASSWORD} | curl -u ${GERRIT_USERNAME} https://gerrit.mcp.mirantis.com/a/projects/${repoName}/tags", returnStdout: true).trim()
+        }
+    }
+    def jsonResponse = coreTags.split('\n').last()
+    def jsonTags = readJSON text: jsonResponse
+    jsonTags.each { tagData ->
+        def tag = tagData['ref']?.replace(replacePattern, '')
+        if (tag) {
+            tags.add(tag)
+        }
+    }
+
+    return tags
+}
\ No newline at end of file