Skip to content

Commit 0f45468

Browse files
committed
Allow specifying target version in JSON
* All pipeline and environment JSONs should have the same version in a repo. * Missing versions are assumed to be the same as the default version, ie, 1.
1 parent 2d71dc1 commit 0f45468

File tree

3 files changed

+107
-25
lines changed

3 files changed

+107
-25
lines changed

src/com.tw.go.config.json/JsonConfigCollection.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import com.google.gson.*;
44

5+
import java.util.HashSet;
6+
import java.util.Set;
7+
58
public class JsonConfigCollection {
6-
private static final int TARGET_VERSION = 1;
9+
private static final int DEFAULT_VERSION = 1;
710
private final Gson gson;
811

912
private JsonObject mainObject = new JsonObject();
@@ -15,7 +18,7 @@ public JsonConfigCollection()
1518
{
1619
gson = new Gson();
1720

18-
mainObject.add("target_version",new JsonPrimitive(TARGET_VERSION));
21+
updateVersionTo(DEFAULT_VERSION);
1922
mainObject.add("environments",environments);
2023
mainObject.add("pipelines",pipelines);
2124
mainObject.add("errors",errors);
@@ -53,4 +56,27 @@ public void addError(PluginError error) {
5356
errors.add(gson.toJsonTree(error));
5457
}
5558

59+
public void updateVersionFromPipelinesAndEnvironments() {
60+
Set<Integer> uniqueVersions = new HashSet<>();
61+
62+
for (JsonElement pipeline : pipelines) {
63+
JsonElement versionElement = pipeline.getAsJsonObject().get("format_version");
64+
uniqueVersions.add(versionElement == null ? DEFAULT_VERSION : versionElement.getAsInt());
65+
}
66+
67+
for (JsonElement environment : environments) {
68+
JsonElement versionElement = environment.getAsJsonObject().get("format_version");
69+
uniqueVersions.add(versionElement == null ? DEFAULT_VERSION : versionElement.getAsInt());
70+
}
71+
72+
if (uniqueVersions.size() > 1) {
73+
throw new RuntimeException("Versions across files are not unique. Found versions: " + uniqueVersions + ". There can only be one version across the whole repository.");
74+
}
75+
updateVersionTo(uniqueVersions.iterator().hasNext() ? uniqueVersions.iterator().next() : DEFAULT_VERSION);
76+
}
77+
78+
private void updateVersionTo(int version) {
79+
mainObject.remove("target_version");
80+
mainObject.add("target_version", new JsonPrimitive(version));
81+
}
5682
}

src/com.tw.go.config.json/JsonConfigPlugin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private GoPluginApiResponse handleParseDirectoryRequest(GoPluginApiRequest reque
149149
scanner,parser, pipelinePattern, environmentPattern);
150150
JsonConfigCollection config = configDirectoryParser.parseDirectory(baseDir);
151151

152+
config.updateVersionFromPipelinesAndEnvironments();
152153
JsonObject responseJsonObject = config.getJsonObject();
153154

154155
return DefaultGoPluginApiResponse.success(gson.toJson(responseJsonObject));

test/com.tw.go.config.json/JsonConfigCollectionTest.java

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.junit.Test;
99

1010
import static junit.framework.TestCase.assertNull;
11+
import static junit.framework.TestCase.fail;
12+
import static org.hamcrest.CoreMatchers.containsString;
1113
import static org.hamcrest.core.Is.is;
1214
import static org.hamcrest.core.IsCollectionContaining.hasItem;
1315
import static org.junit.Assert.assertThat;
@@ -21,55 +23,108 @@ public class JsonConfigCollectionTest {
2123
private JsonObject pipeInGroup;
2224

2325
@Before
24-
public void SetUp()
25-
{
26+
public void setUp() {
2627
jsonCollection = new JsonConfigCollection();
2728

2829
pipe1 = new JsonObject();
29-
pipe1.addProperty("name","pipe1");
30+
pipe1.addProperty("name", "pipe1");
3031

3132
pipe2 = new JsonObject();
32-
pipe2.addProperty("name","pipe2");
33+
pipe2.addProperty("name", "pipe2");
3334

3435
pipeInGroup = new JsonObject();
35-
pipeInGroup.addProperty("name","pipe3");
36-
pipeInGroup.addProperty("group","mygroup");
36+
pipeInGroup.addProperty("name", "pipe3");
37+
pipeInGroup.addProperty("group", "mygroup");
3738

3839
devEnv = new JsonObject();
39-
devEnv.addProperty("name","dev");
40+
devEnv.addProperty("name", "dev");
4041
}
4142

4243
@Test
43-
public void shouldReturnTargetVersion()
44-
{
44+
public void shouldReturnDefaultTargetVersionWhenThereAreNoPipelinesOrEnvironmentsDefined() {
45+
jsonCollection.updateVersionFromPipelinesAndEnvironments();
4546
JsonObject jsonObject = jsonCollection.getJsonObject();
46-
assertThat(jsonObject.get("target_version") instanceof JsonPrimitive,is(true));
47-
assertThat(jsonObject.getAsJsonPrimitive("target_version").getAsInt(), is(1));
47+
assertTargetVersion(jsonObject, 1);
4848
}
4949

5050
@Test
51-
public void shouldReturnEnvironmentsArrayInJsonObjectWhenEmpty()
52-
{
51+
public void shouldReturnEnvironmentsArrayInJsonObjectWhenEmpty() {
5352
JsonObject jsonObject = jsonCollection.getJsonObject();
54-
assertThat(jsonObject.get("environments") instanceof JsonArray,is(true));
53+
assertThat(jsonObject.get("environments") instanceof JsonArray, is(true));
5554
assertThat(jsonObject.getAsJsonArray("environments"), is(new JsonArray()));
5655
}
5756

5857
@Test
59-
public void shouldAppendPipelinesToPipelinesCollection()
60-
{
61-
jsonCollection.addPipeline(pipe1,"pipe1.json");
62-
jsonCollection.addPipeline(pipe2,"pipe2.json");
58+
public void shouldAppendPipelinesToPipelinesCollection() {
59+
jsonCollection.addPipeline(pipe1, "pipe1.json");
60+
jsonCollection.addPipeline(pipe2, "pipe2.json");
6361
JsonObject jsonObject = jsonCollection.getJsonObject();
64-
assertThat(jsonObject.getAsJsonArray("pipelines").size(),is(2));
62+
assertThat(jsonObject.getAsJsonArray("pipelines").size(), is(2));
6563
}
6664

6765

6866
@Test
69-
public void shouldReturnEnvironmentsInJsonObject()
70-
{
71-
jsonCollection.addEnvironment(devEnv,"dev.json");
67+
public void shouldReturnEnvironmentsInJsonObject() {
68+
jsonCollection.addEnvironment(devEnv, "dev.json");
7269
JsonObject jsonObject = jsonCollection.getJsonObject();
73-
assertThat(jsonObject.getAsJsonArray("environments").size(),is(1));
70+
assertThat(jsonObject.getAsJsonArray("environments").size(), is(1));
71+
}
72+
73+
@Test
74+
public void shouldUpdateTargetVersionWhenItIsTheSameAcrossAllPipelinesAndEnvironments() {
75+
jsonCollection.addPipeline(pipelineWithVersion(2), "pipe1.json");
76+
jsonCollection.addPipeline(pipelineWithVersion(2), "pipe2.json");
77+
jsonCollection.addEnvironment(envWithVersion(2), "env1.json");
78+
jsonCollection.addEnvironment(envWithVersion(2), "env2.json");
79+
80+
jsonCollection.updateVersionFromPipelinesAndEnvironments();
81+
JsonObject jsonObject = jsonCollection.getJsonObject();
82+
83+
assertTargetVersion(jsonObject, 2);
84+
}
85+
86+
@Test
87+
public void shouldUpdateTargetVersionWhenItIsTheDefaultOrMissingAcrossAllPipelinesAndEnvironments() {
88+
int defaultVersionExpected = 1;
89+
90+
jsonCollection.addPipeline(new JsonObject(), "pipe1.json");
91+
jsonCollection.addPipeline(pipelineWithVersion(defaultVersionExpected), "pipe2.json");
92+
jsonCollection.addEnvironment(new JsonObject(), "env1.json");
93+
jsonCollection.addEnvironment(envWithVersion(defaultVersionExpected), "env2.json");
94+
95+
jsonCollection.updateVersionFromPipelinesAndEnvironments();
96+
JsonObject jsonObject = jsonCollection.getJsonObject();
97+
98+
assertTargetVersion(jsonObject, defaultVersionExpected);
99+
}
100+
101+
@Test
102+
public void shouldFailToUpdateTargetVersionWhenItIs_NOT_TheSameAcrossAllPipelinesAndEnvironments() {
103+
jsonCollection.addPipeline(pipelineWithVersion(1), "pipe1.json");
104+
jsonCollection.addPipeline(pipelineWithVersion(2), "pipe2.json");
105+
jsonCollection.addEnvironment(envWithVersion(1), "env1.json");
106+
jsonCollection.addEnvironment(new JsonObject(), "env2.json");
107+
108+
try {
109+
jsonCollection.updateVersionFromPipelinesAndEnvironments();
110+
fail("Should have failed to find a unique version");
111+
} catch (RuntimeException e) {
112+
assertThat(e.getMessage(), containsString("Versions across files are not unique"));
113+
}
114+
}
115+
116+
private JsonElement envWithVersion(int version) {
117+
return pipelineWithVersion(version);
118+
}
119+
120+
private JsonElement pipelineWithVersion(int version) {
121+
JsonObject jsonObject = new JsonObject();
122+
jsonObject.addProperty("format_version", version);
123+
return jsonObject;
124+
}
125+
126+
private void assertTargetVersion(JsonObject jsonObject, int expectedVersion) {
127+
assertThat(jsonObject.get("target_version") instanceof JsonPrimitive, is(true));
128+
assertThat(jsonObject.getAsJsonPrimitive("target_version").getAsInt(), is(expectedVersion));
74129
}
75130
}

0 commit comments

Comments
 (0)