public JsonConfigCollection transform(Object rootObj, String location) { JsonConfigCollection partialConfig = new JsonConfigCollection(); Map<String, Object> rootMap = (Map<String, Object>) rootObj; for (Map.Entry<String, Object> pe : rootMap.entrySet()) { try { JsonElement jsonPipeline = pipelineTransform.transform(pipe); partialConfig.addPipeline(jsonPipeline, location); } catch (Exception ex) { partialConfig.addError(new PluginError( String.format("Failed to parse pipeline %s; %s", pipe.getKey(), ex.getMessage()), location)); try { JsonElement jsonEnvironment = environmentsTransform.transform(env); partialConfig.addEnvironment(jsonEnvironment, location); } catch (Exception ex) { partialConfig.addError(new PluginError( String.format("Failed to parse environment %s; %s", env.getKey(), ex.getMessage()), location)); partialConfig.updateFormatVersionFound(Integer.valueOf((String) pe.getValue())); } else if (!"common".equalsIgnoreCase(pe.getKey())) throw new YamlConfigException(pe.getKey() + " is invalid, expected format_version, pipelines, environments, or common");
private GoPluginApiResponse handlingErrors(Supplier<GoPluginApiResponse> exec) { try { return exec.get(); } catch (ParsedRequest.RequestParseException e) { return badRequest(e.getMessage()); } catch (Exception e) { LOGGER.error("Unexpected error occurred in YAML configuration plugin.", e); JsonConfigCollection config = new JsonConfigCollection(); config.addError(new PluginError(e.toString(), "YAML config plugin")); return error(gson.toJson(config.getJsonObject())); } }
public void parseStream(JsonConfigCollection result, InputStream input, String location) { try (InputStreamReader contentReader = new InputStreamReader(input)) { if (input.available() < 1) { result.addError("File is empty", location); return; } YamlConfig config = new YamlConfig(); config.setAllowDuplicates(false); YamlReader reader = new YamlReader(contentReader, config); Object rootObject = reader.read(); JsonConfigCollection filePart = rootTransform.transform(rootObject, location); result.append(filePart); } catch (YamlReader.YamlReaderException e) { result.addError(e.getMessage(), location); } catch (IOException e) { result.addError(e.getMessage() + " : " + e.getCause().getMessage() + " : ", location); } } }
public JsonConfigCollection parseFiles(File baseDir, String[] files) { JsonConfigCollection collection = new JsonConfigCollection(); for (String file : files) { try { parseStream(collection, new FileInputStream(new File(baseDir, file)), file); } catch (FileNotFoundException ex) { collection.addError("File matching GoCD YAML pattern disappeared", file); } } return collection; }
private GoPluginApiResponse handleParseContentRequest(GoPluginApiRequest request) { return handlingErrors(() -> { ParsedRequest parsed = ParsedRequest.parse(request); YamlConfigParser parser = new YamlConfigParser(); Map<String, String> contents = parsed.getParam("contents"); JsonConfigCollection result = new JsonConfigCollection(); contents.forEach((filename, content) -> { parser.parseStream(result, new ByteArrayInputStream(content.getBytes()), filename); }); result.updateTargetVersionFromFiles(); return success(gson.toJson(result.getJsonObject())); }); }
JsonConfigCollection collection = new JsonConfigCollection(); parser.parseStream(collection, getFileAsStream(syntax.file), getLocation(syntax.file)); if (collection.getErrors().size() > 0) { JsonObject result = collection.getJsonObject(); result.remove("environments"); result.remove("pipelines");
@Test public void shouldTransformEmptyRoot() throws IOException { JsonConfigCollection empty = readRootYaml("empty"); assertThat(empty.getJsonObject().isJsonObject(), is(true)); assertThat(empty.getJsonObject().get("environments").isJsonArray(), is(true)); assertThat(empty.getJsonObject().get("pipelines").isJsonArray(), is(true)); assertThat(empty.getJsonObject().get("environments").getAsJsonArray().size(), is(0)); assertThat(empty.getJsonObject().get("pipelines").getAsJsonArray().size(), is(0)); }
@Test public void shouldAppendPipelinesToPipelinesCollection() { jsonCollection.addPipeline(pipe1, "pipe1.json"); jsonCollection.addPipeline(pipe2, "pipe2.json"); JsonObject jsonObject = jsonCollection.getJsonObject(); assertThat(jsonObject.getAsJsonArray("pipelines").size(), is(2)); }
@Test public void shouldReturnEnvironmentsInJsonObject() { jsonCollection.addEnvironment(devEnv, "dev.json"); JsonObject jsonObject = jsonCollection.getJsonObject(); assertThat(jsonObject.getAsJsonArray("environments").size(), is(1)); } }
private GoPluginApiResponse handleParseDirectoryRequest(GoPluginApiRequest request) { return handlingErrors(() -> { ParsedRequest parsed = ParsedRequest.parse(request); File baseDir = new File(parsed.getStringParam("directory")); String pattern = parsed.getConfigurationKey(PLUGIN_SETTINGS_FILE_PATTERN); if (isBlank(pattern)) { pattern = getFilePattern(); } YamlConfigParser parser = new YamlConfigParser(); String[] files = new AntDirectoryScanner().getFilesMatchingPattern(baseDir, pattern); JsonConfigCollection config = parser.parseFiles(baseDir, files); config.updateTargetVersionFromFiles(); return success(gson.toJson(config.getJsonObject())); }); }
@Before public void SetUp() { jsonCollection = new JsonConfigCollection(); pipe1 = new JsonObject(); pipe1.addProperty("name", "pipe1"); pipe2 = new JsonObject(); pipe2.addProperty("name", "pipe2"); pipeInGroup = new JsonObject(); pipeInGroup.addProperty("name", "pipe3"); pipeInGroup.addProperty("group", "mygroup"); devEnv = new JsonObject(); devEnv.addProperty("name", "dev"); }
public void updateFormatVersionFound(int version) { uniqueVersions.add(version); updateTargetVersionTo(version); }
public void addError(String message, String location) { this.addError(new PluginError(message, location)); }
@Test public void shouldTransformRootWhen2PipelinesAnd2Environments() throws IOException { JsonConfigCollection collection = readRootYaml("2"); assertThat(collection.getJsonObject().get("environments").getAsJsonArray().size(), is(2)); assertThat(collection.getJsonObject().get("pipelines").getAsJsonArray().size(), is(2)); }
public JsonConfigCollection() { gson = new Gson(); updateTargetVersionTo(DEFAULT_VERSION); mainObject.add("environments", environments); mainObject.add("pipelines", pipelines); mainObject.add("errors", errors); }
@Test public void shouldTransformRootWithCommonSection() throws IOException { JsonConfigCollection collection = readRootYaml("common_section"); assertThat(collection.getJsonObject().get("environments").getAsJsonArray().size(), is(1)); assertThat(collection.getJsonObject().get("pipelines").getAsJsonArray().size(), is(1)); }
public void updateTargetVersionFromFiles() { if (uniqueVersions.size() > 1) { throw new RuntimeException("Versions across files are not unique. Found versions: " + uniqueVersions + ". There can only be one version across the whole repository."); } updateTargetVersionTo(uniqueVersions.iterator().hasNext() ? uniqueVersions.iterator().next() : DEFAULT_VERSION); }
@Test public void shouldReturnEnvironmentsArrayInJsonObjectWhenEmpty() { JsonObject jsonObject = jsonCollection.getJsonObject(); assertThat(jsonObject.get("environments") instanceof JsonArray, is(true)); assertThat(jsonObject.getAsJsonArray("environments"), is(new JsonArray())); }
@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 public void shouldRecognizeAndUpdateVersion() throws Exception { assertTargetVersion(readRootYaml("version_not_present").getJsonObject(), 1); assertTargetVersion(readRootYaml("version_1").getJsonObject(), 1); assertTargetVersion(readRootYaml("version_2").getJsonObject(), 2); }