Add method for setting dependencies versions in requirements of Helm chart

Change-Id: Id8d5a43b917924826df795d794712f01691c8505
Related-Prod: https://mirantis.jira.com/browse/PROD-33153
diff --git a/src/com/mirantis/mk/Helm.groovy b/src/com/mirantis/mk/Helm.groovy
index af3fcf9..cfd7d4c 100644
--- a/src/com/mirantis/mk/Helm.groovy
+++ b/src/com/mirantis/mk/Helm.groovy
@@ -76,4 +76,42 @@
         }
         error "Version ${versionData} doesn't contain required suffix ${tagSuffix} or not in semver2 format"
     }
+}
+
+/**
+ * Takes a list of dependencies and a version, and sets a version for each dependency in requirements.yaml. If dependency isn't
+ * found in requirements.yaml or requirements.yaml does not exist - does nothing.
+ *
+ * @param chartPath      string, path to a directory with helm chart
+ * @param dependencies   list of hashes with names and versions of dependencies in format:
+ *                       [['name': 'chart-name1', 'version': '0.1.0-myversion'], ['name': 'chart-name2', 'version': '0.2.0-myversion']]
+ */
+
+def setChartDependenciesVersion(chartPath, List dependencies){
+    def common = new com.mirantis.mk.Common()
+    if (!dependencies){
+        error 'No list of target dependencies is specified'
+    }
+    def reqsFilePath = "${chartPath}/requirements.yaml"
+    def chartYaml = readYaml file: "${chartPath}/Chart.yaml"
+    def reqsUpdateNeeded = false
+    def reqsMap = [:]
+    if (fileExists(reqsFilePath)){
+        reqsMap = readYaml file: reqsFilePath
+        for (i in dependencies) {
+            for (item in reqsMap.get('dependencies', [])){
+                if (item['name'] == i['name']){
+                    common.infoMsg("Set version ${version} for dependency ${i} in chart ${chartYaml['name']}")
+                    item['version'] = i['version']
+                    reqsUpdateNeeded = true
+                }
+            }
+        }
+    }
+    if (reqsUpdateNeeded){
+        sh "rm ${reqsFilePath}"
+        writeYaml file: reqsFilePath, data: reqsMap
+    } else {
+        common.warningMsg("requirements.yaml doesn't exist at path ${reqsFilePath} or chart doesn't contain ${dependencies}, nothing to set")
+    }
 }
\ No newline at end of file