Skip to content

Commit c84b5e1

Browse files
feat: [CDS-37215]: fix Stage individual schema generation in pipleine service (#34840)
* [CDS-37215]: fix Stage individual schema generation in pipleine service * [CDS-37215]: snow approavl step entity should belong to PMS * [CDS-37215]: Add a hack to prevent failure for step schema generation on pipeline side * [CDS-37215]: fix code format error
1 parent ff068a5 commit c84b5e1

3 files changed

Lines changed: 89 additions & 4 deletions

File tree

800-pipeline-service/src/main/java/io/harness/pms/pipeline/service/PMSYamlSchemaServiceImpl.java

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.harness.annotations.dev.HarnessTeam;
2424
import io.harness.annotations.dev.OwnedBy;
2525
import io.harness.beans.FeatureName;
26+
import io.harness.data.structure.EmptyPredicate;
2627
import io.harness.encryption.Scope;
2728
import io.harness.exception.InvalidYamlException;
2829
import io.harness.exception.JsonSchemaException;
@@ -50,16 +51,23 @@
5051
import io.harness.yaml.utils.YamlSchemaUtils;
5152
import io.harness.yaml.validator.YamlSchemaValidator;
5253

54+
import com.fasterxml.jackson.core.JsonProcessingException;
5355
import com.fasterxml.jackson.databind.JsonNode;
56+
import com.fasterxml.jackson.databind.ObjectMapper;
5457
import com.fasterxml.jackson.databind.node.ArrayNode;
5558
import com.fasterxml.jackson.databind.node.ObjectNode;
5659
import com.google.inject.Inject;
5760
import com.google.inject.name.Named;
5861
import java.io.IOException;
5962
import java.util.ArrayList;
63+
import java.util.HashMap;
64+
import java.util.HashSet;
6065
import java.util.Iterator;
66+
import java.util.LinkedList;
6167
import java.util.List;
68+
import java.util.Map;
6269
import java.util.Objects;
70+
import java.util.Set;
6371
import java.util.concurrent.Executor;
6472
import java.util.concurrent.Executors;
6573
import java.util.concurrent.TimeUnit;
@@ -305,18 +313,92 @@ public JsonNode getIndividualYamlSchema(String accountId, String orgIdentifier,
305313
return getPipelineYamlSchemaInternal(accountId, projectIdentifier, orgIdentifier, null);
306314
}
307315
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<>();
308320
if (StepCategory.STAGE.toString().equals(yamlGroup)) {
309321
List<ModuleType> enabledModules = obtainEnabledModules(accountId);
322+
enabledModules.add(ModuleType.PMS);
310323
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+
}
311357
}
312358
JsonNode jsonNode = schemaFetcher.fetchStepYamlSchema(
313359
accountId, projectIdentifier, orgIdentifier, scope, entityType, yamlGroup, yamlSchemaWithDetailsList);
314360

315361
// TODO: hack to remove v2 steps from stage yamls. Fix it properly
316362
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);
319386
}
320387
return jsonNode;
321388
}
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+
}
322404
}

840-template-service/src/main/java/io/harness/template/services/NGTemplateSchemaServiceImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ public void validateYamlSchemaInternal(TemplateEntity templateEntity) {
137137
return;
138138
}
139139
try {
140-
Scope scope = projectIdentifier != null ? Scope.PROJECT : orgIdentifier != null ? Scope.ORG : Scope.ACCOUNT;
140+
Scope scope = templateEntity.getTemplateScope() != null ? templateEntity.getTemplateScope()
141+
: projectIdentifier != null ? Scope.PROJECT
142+
: orgIdentifier != null ? Scope.ORG
143+
: Scope.ACCOUNT;
141144
JsonNode schema = getTemplateSchema(accountIdentifier, projectIdentifier, orgIdentifier, scope,
142145
templateEntity.getChildType(), templateEntity.getTemplateEntityType());
143146
String schemaString = JsonPipelineUtils.writeJsonString(schema);

970-ng-commons/src/main/java/io/harness/EntityType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public enum EntityType {
187187
FEATURE_FLAGS(
188188
ModuleType.CF, EntityTypeConstants.FEATURE_FLAGS, IdentifierRef.class, EntityYamlRootNames.FEATURE_FLAGS),
189189
@JsonProperty(EntityTypeConstants.SERVICENOW_APPROVAL)
190-
SERVICENOW_APPROVAL_STEP(ModuleType.CD, EntityTypeConstants.SERVICENOW_APPROVAL, IdentifierRef.class,
190+
SERVICENOW_APPROVAL_STEP(ModuleType.PMS, EntityTypeConstants.SERVICENOW_APPROVAL, IdentifierRef.class,
191191
EntityYamlRootNames.SERVICENOW_APPROVAL),
192192
@JsonProperty(EntityTypeConstants.SERVICENOW_CREATE)
193193
SERVICENOW_CREATE_STEP(ModuleType.PMS, EntityTypeConstants.SERVICENOW_CREATE, IdentifierRef.class,

0 commit comments

Comments
 (0)