Merge "Refactor test-salt-models-pipeline"
diff --git a/test-salt-models-pipeline.groovy b/test-salt-models-pipeline.groovy
index 4236fdd..78b0920 100644
--- a/test-salt-models-pipeline.groovy
+++ b/test-salt-models-pipeline.groovy
@@ -80,11 +80,9 @@
 
     stage("test-nodes") {
       if(checkouted) {
-        def infraYMLs = sh(script: "find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
-        def branches = [:]
-        def acc = 0
 
         def modifiedClusters = null
+
         if (gerritRef) {
           checkChange = sh(script: "git diff-tree --no-commit-id --name-only -r HEAD | grep -v classes/cluster", returnStatus: true)
           if (checkChange == 1) {
@@ -92,6 +90,15 @@
           }
         }
 
+        def infraYMLs = sh(script: "find ./classes/ -regex '.*cluster/[-_a-zA-Z0-9]*/[infra/]*init\\.yml' -exec grep -il 'cluster_name' {} \\;", returnStdout: true).tokenize()
+        if (modifiedClusters) {
+          infraYMLs.removeAll { !modifiedClusters.contains(it.tokenize('/')[3]) }
+          common.infoMsg("Testing only modified clusters: ${infraYMLs}")
+        }
+
+        def branches = [:]
+        def acc = 0
+
         for (int i = 0; i < infraYMLs.size(); i++) {
           def infraYMLConfig = readYaml(file: infraYMLs[i])
           if(!infraYMLConfig["parameters"].containsKey("_param")){
@@ -113,10 +120,6 @@
             acc = 0
           }
 
-          if (gerritRef && modifiedClusters && !modifiedClusters.contains(clusterName)) {
-            continue
-          }
-
           branches[testTarget] = {
             build job: "test-salt-model-node", parameters: [
               [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],