|
23 | 23 | import io.harness.annotations.dev.HarnessTeam; |
24 | 24 | import io.harness.annotations.dev.OwnedBy; |
25 | 25 | import io.harness.beans.FeatureName; |
| 26 | +import io.harness.data.structure.EmptyPredicate; |
26 | 27 | import io.harness.encryption.Scope; |
27 | 28 | import io.harness.exception.InvalidYamlException; |
28 | 29 | import io.harness.exception.JsonSchemaException; |
|
50 | 51 | import io.harness.yaml.utils.YamlSchemaUtils; |
51 | 52 | import io.harness.yaml.validator.YamlSchemaValidator; |
52 | 53 |
|
| 54 | +import com.fasterxml.jackson.core.JsonProcessingException; |
53 | 55 | import com.fasterxml.jackson.databind.JsonNode; |
| 56 | +import com.fasterxml.jackson.databind.ObjectMapper; |
54 | 57 | import com.fasterxml.jackson.databind.node.ArrayNode; |
55 | 58 | import com.fasterxml.jackson.databind.node.ObjectNode; |
56 | 59 | import com.google.inject.Inject; |
57 | 60 | import com.google.inject.name.Named; |
58 | 61 | import java.io.IOException; |
59 | 62 | import java.util.ArrayList; |
| 63 | +import java.util.HashMap; |
| 64 | +import java.util.HashSet; |
60 | 65 | import java.util.Iterator; |
| 66 | +import java.util.LinkedList; |
61 | 67 | import java.util.List; |
| 68 | +import java.util.Map; |
62 | 69 | import java.util.Objects; |
| 70 | +import java.util.Set; |
63 | 71 | import java.util.concurrent.Executor; |
64 | 72 | import java.util.concurrent.Executors; |
65 | 73 | import java.util.concurrent.TimeUnit; |
@@ -305,18 +313,92 @@ public JsonNode getIndividualYamlSchema(String accountId, String orgIdentifier, |
305 | 313 | return getPipelineYamlSchemaInternal(accountId, projectIdentifier, orgIdentifier, null); |
306 | 314 | } |
307 | 315 | List<YamlSchemaWithDetails> yamlSchemaWithDetailsList = null; |
| 316 | + Map<String, List<JsonNode>> nameSpaceToDefinitionMap = new HashMap<>(); |
| 317 | + Set<String> nameSpaces = new HashSet<>(); |
| 318 | + JsonNode mergedDefinition = null; |
| 319 | + Map<String, JsonNode> finalNameSpaceToDefinitionMap = new HashMap<>(); |
308 | 320 | if (StepCategory.STAGE.toString().equals(yamlGroup)) { |
309 | 321 | List<ModuleType> enabledModules = obtainEnabledModules(accountId); |
| 322 | + enabledModules.add(ModuleType.PMS); |
310 | 323 | yamlSchemaWithDetailsList = fetchSchemaWithDetailsFromModules(accountId, enabledModules); |
| 324 | + yamlSchemaWithDetailsList = |
| 325 | + filterYamlSchemaDetailsByModule(yamlSchemaWithDetailsList, entityType.getEntityProduct()); |
| 326 | + for (YamlSchemaWithDetails yamlSchemaWithDetails : yamlSchemaWithDetailsList) { |
| 327 | + String nameSpace = yamlSchemaWithDetails.getYamlSchemaMetadata().getNamespace(); |
| 328 | + JsonNode definition = yamlSchemaWithDetails.getSchema().get(DEFINITIONS_NODE); |
| 329 | + nameSpaces.add(nameSpace); |
| 330 | + if (EmptyPredicate.isEmpty(nameSpace)) { |
| 331 | + if (mergedDefinition == null) { |
| 332 | + mergedDefinition = definition; |
| 333 | + } else { |
| 334 | + JsonNodeUtils.merge(mergedDefinition, definition); |
| 335 | + } |
| 336 | + } else { |
| 337 | + if (nameSpaceToDefinitionMap.containsKey(nameSpace)) { |
| 338 | + nameSpaceToDefinitionMap.get(nameSpace).add(definition); |
| 339 | + } else { |
| 340 | + List<JsonNode> nameSpaceDefinition = new LinkedList<>(); |
| 341 | + nameSpaceDefinition.add(definition); |
| 342 | + nameSpaceToDefinitionMap.put(nameSpace, nameSpaceDefinition); |
| 343 | + } |
| 344 | + } |
| 345 | + } |
| 346 | + for (Map.Entry<String, List<JsonNode>> entry : nameSpaceToDefinitionMap.entrySet()) { |
| 347 | + JsonNode nameSpaceDefinition = null; |
| 348 | + for (JsonNode jsonNode : entry.getValue()) { |
| 349 | + if (nameSpaceDefinition == null) { |
| 350 | + nameSpaceDefinition = jsonNode; |
| 351 | + } else { |
| 352 | + JsonNodeUtils.merge(nameSpaceDefinition, jsonNode); |
| 353 | + } |
| 354 | + } |
| 355 | + finalNameSpaceToDefinitionMap.put(entry.getKey(), nameSpaceDefinition); |
| 356 | + } |
311 | 357 | } |
312 | 358 | JsonNode jsonNode = schemaFetcher.fetchStepYamlSchema( |
313 | 359 | accountId, projectIdentifier, orgIdentifier, scope, entityType, yamlGroup, yamlSchemaWithDetailsList); |
314 | 360 |
|
315 | 361 | // TODO: hack to remove v2 steps from stage yamls. Fix it properly |
316 | 362 | if (StepCategory.STAGE.toString().equals(yamlGroup)) { |
317 | | - YamlSchemaTransientHelper.removeV2StepEnumsFromStepElementConfig( |
318 | | - jsonNode.get(DEFINITIONS_NODE).fields().next().getValue().get(STEP_ELEMENT_CONFIG)); |
| 363 | + String stepNameSpace = null; |
| 364 | + if (jsonNode.get(DEFINITIONS_NODE).fields().hasNext()) { |
| 365 | + String nameSpace = jsonNode.get(DEFINITIONS_NODE).fields().next().getKey(); |
| 366 | + if (nameSpaces.contains(nameSpace)) { |
| 367 | + stepNameSpace = nameSpace; |
| 368 | + } |
| 369 | + } |
| 370 | + |
| 371 | + JsonNodeUtils.merge(jsonNode.get(DEFINITIONS_NODE), mergedDefinition); |
| 372 | + for (Map.Entry<String, JsonNode> entry : finalNameSpaceToDefinitionMap.entrySet()) { |
| 373 | + if (!stepNameSpace.equals(entry.getKey())) { |
| 374 | + JsonNodeUtils.merge(jsonNode.get(DEFINITIONS_NODE), entry.getValue()); |
| 375 | + } |
| 376 | + } |
| 377 | + for (String nameSpace : nameSpaces) { |
| 378 | + if (jsonNode.get(DEFINITIONS_NODE).get(nameSpace) != null) { |
| 379 | + YamlSchemaTransientHelper.removeV2StepEnumsFromStepElementConfig( |
| 380 | + jsonNode.get(DEFINITIONS_NODE).get(nameSpace).get(STEP_ELEMENT_CONFIG)); |
| 381 | + } |
| 382 | + } |
| 383 | + } else { |
| 384 | + JsonNode stepSpecTypeNode = getStepSpecType(); |
| 385 | + JsonNodeUtils.merge(jsonNode.get(DEFINITIONS_NODE), stepSpecTypeNode); |
319 | 386 | } |
320 | 387 | return jsonNode; |
321 | 388 | } |
| 389 | + |
| 390 | + // TODO: Brijesh to look at the intermittent issue and remove this |
| 391 | + private JsonNode getStepSpecType() { |
| 392 | + String stepSpecTypeNodeString = "{\"StepSpecType\": {\n" |
| 393 | + + " \"type\": \"object\",\n" |
| 394 | + + " \"discriminator\": \"type\",\n" |
| 395 | + + " \"$schema\": \"http://json-schema.org/draft-07/schema#\"\n" |
| 396 | + + " }}"; |
| 397 | + ObjectMapper mapper = new ObjectMapper(); |
| 398 | + try { |
| 399 | + return mapper.readTree(stepSpecTypeNodeString); |
| 400 | + } catch (JsonProcessingException e) { |
| 401 | + throw new RuntimeException(e); |
| 402 | + } |
| 403 | + } |
322 | 404 | } |
0 commit comments