Skip to content

Commit

Permalink
Preserve order of pipelines from YAML when converting to JSON
Browse files Browse the repository at this point in the history
Fixes tomzo#4

Changed to use recent release of EsotericSoftware's yamlbeans, rather than the
old sanjusoftware fork. This reads yaml mappings into LinkedHashMap, which
preserves ordering.
(It also opens the door for supporting YAML merge key syntax - issue tomzo#55)
  • Loading branch information
michaelbannister committed Jan 27, 2018
1 parent a944591 commit 1891338
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '2.6.2'
compile group: 'commons-io', name: 'commons-io', version: '2.4'
compile group: 'org.apache.ant', name: 'ant', version: '1.7.1'
compile group: 'com.github.sanjusoftware', name: 'yamlbeans', version: '1.11'
compile group: 'com.esotericsoftware.yamlbeans', name: 'yamlbeans', version: '1.13'
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile group: 'org.mockito', name: 'mockito-core', version: '1.10.19'
testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public JsonObject transform(Map.Entry<String, Object> env) {
JsonObject envJson = new JsonObject();
envJson.addProperty(JSON_ENV_NAME_FIELD, envName);
Object envObj = env.getValue();
if ("".equals(envObj))
if (envObj == null)
return envJson;
if (!(envObj instanceof Map))
throw new YamlConfigException("Expected environment to be a hash");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public JsonConfigCollection transform(Object rootObj, String location) {
Map<String, Object> rootMap = (Map<String, Object>) rootObj;
for (Map.Entry<String, Object> pe : rootMap.entrySet()) {
if ("pipelines".equalsIgnoreCase(pe.getKey())) {
if ("".equals(pe.getValue()))
if (pe.getValue() == null)
continue;
Map<String, Object> pipelines = (Map<String, Object>) pe.getValue();
for (Map.Entry<String, Object> pipe : pipelines.entrySet()) {
Expand All @@ -44,7 +44,7 @@ public JsonConfigCollection transform(Object rootObj, String location) {
}
}
} else if ("environments".equalsIgnoreCase(pe.getKey())) {
if ("".equals(pe.getValue()))
if (pe.getValue() == null)
continue;
Map<String, Object> environments = (Map<String, Object>) pe.getValue();
for (Map.Entry<String, Object> env : environments.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public JsonObject transform(Map.Entry<String, Object> taskEntry) {
JsonObject taskJson = new JsonObject();

String taskType = taskEntry.getKey();
if (taskEntry.getValue() == null) {
taskJson.addProperty(JSON_TASK_TYPE_FIELD, taskType);
return taskJson;
}
if (taskEntry.getValue() instanceof String) {
if (taskType.equalsIgnoreCase("script")) {
taskJson.addProperty(JSON_TASK_TYPE_FIELD, "plugin");
Expand All @@ -64,10 +68,6 @@ public JsonObject transform(Map.Entry<String, Object> taskEntry) {
taskJson.add(JSON_TASK_PLUGIN_CONFIGURATION_FIELD, pluginConfig);
return taskJson;
}
if ("".equals(taskEntry.getValue())) {
taskJson.addProperty(JSON_TASK_TYPE_FIELD, taskType);
return taskJson;
}
}
taskJson.addProperty(JSON_TASK_TYPE_FIELD, taskType);
if (!(taskEntry.getValue() instanceof Map))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cd.go.plugin.config.yaml.JsonConfigCollection;
import cd.go.plugin.config.yaml.YamlConfigException;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.junit.Before;
Expand Down Expand Up @@ -58,6 +59,22 @@ public void shouldRecognizeAndUpdateVersion() throws Exception {
assertTargetVersion(readRootYaml("version_1").getJsonObject(), 1);
assertTargetVersion(readRootYaml("version_2").getJsonObject(), 2);
}

@Test
public void shouldPreserveOrderOfPipelines() throws IOException {
MaterialTransform materialTransform = mock(MaterialTransform.class);
StageTransform stageTransform = mock(StageTransform.class);
EnvironmentVariablesTransform environmentTransform = mock(EnvironmentVariablesTransform.class);
ParameterTransform parameterTransform = mock(ParameterTransform.class);
pipelineTransform = new PipelineTransform(materialTransform, stageTransform, environmentTransform, parameterTransform);
rootTransform = new RootTransform(pipelineTransform, environmentsTransform);

JsonConfigCollection collection = readRootYaml("pipeline_order");
JsonArray pipelines = collection.getJsonObject().get("pipelines").getAsJsonArray();
assertThat(pipelines.get(0).getAsJsonObject().get("name").getAsString(), is("pipe1"));
assertThat(pipelines.get(1).getAsJsonObject().get("name").getAsString(), is("pipe2"));
assertThat(pipelines.get(2).getAsJsonObject().get("name").getAsString(), is("pipe3"));
}

@Test(expected = YamlReader.YamlReaderException.class)
public void shouldNotTransformRootWhenYAMLHasDuplicateKeys() throws IOException {
Expand Down
24 changes: 24 additions & 0 deletions src/test/resources/parts/roots/pipeline_order.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
environments:
env1:
pipelines:
- pipe1

pipelines:
pipe1:
group: simple
materials:
mygit:
stages:
- null
pipe2:
group: simple
materials:
mygit:
stages:
- null
pipe3:
group: simple
materials:
mygit:
stages:
- null

0 comments on commit 1891338

Please sign in to comment.