String getFilePattern() { if (null != settings && !isBlank(settings.getFilePattern())) { return settings.getFilePattern(); } return DEFAULT_FILE_PATTERN; }
public void addError(String message, String location) { this.addError(new PluginError(message, location)); }
/** * fetches plugin settings if we haven't yet */ private void ensureConfigured() { if (null == settings) { settings = fetchPluginSettings(); } }
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())); }); }
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())); }); }
private void testInverseTransform(String caseFile, String expectedFile) throws IOException { String expectedObject = loadString("parts/" + expectedFile + ".yaml"); Map<String, Object> actual = parser.inverseTransform(readJsonGson("parts/" + caseFile + ".json")); assertYamlEquivalent(expectedObject, YamlUtils.dump(actual)); } }
private void testInverseTransform(String caseFile, String expectedFile) throws IOException { Map<String, Object> inverse = parser.inverseTransform(readJsonGson("parts/jobs/" + caseFile + ".json")); assertYamlEquivalent(loadString("parts/jobs/" + expectedFile + ".yaml"), YamlUtils.dump(inverse)); } }
private void testInverseTransform(String caseFile, String expectedFile) throws IOException { String expectedObject = loadString("parts/env_vars/" + expectedFile + ".yaml"); Map<String, Object> actual = parser.inverseTransform(readJsonArrayGson("parts/env_vars/" + caseFile + ".json")); assertYamlEquivalent(expectedObject, YamlUtils.dump(actual)); } }
private void testTransform(String caseFile, String expectedFile) throws IOException { JsonObject expectedObject = (JsonObject) readJsonObject("parts/materials/" + expectedFile + ".json"); JsonObject jsonObject = parser.transform(readYamlObject("parts/materials/" + caseFile + ".yaml")); assertThat(jsonObject, is(new JsonObjectMatcher(expectedObject))); }
private void testTransform(String caseFile, String expectedFile) throws IOException { JsonObject expectedObject = (JsonObject) readJsonObject("parts/stages/" + expectedFile + ".json"); JsonObject jsonObject = parser.transform(readYamlObject("parts/stages/" + caseFile + ".yaml")); assertThat(jsonObject, is(new JsonObjectMatcher(expectedObject))); }
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; }
@Test public void shouldFailToUpdateTargetVersionWhenItIs_NOT_TheSameAcrossAllFiles() throws Exception { FileUtils.copyInputStreamToFile(getResourceAsStream("/parts/roots/version_1.yaml"), tempDir.newFile("v1_1.gocd.yaml")); FileUtils.copyInputStreamToFile(getResourceAsStream("/parts/roots/version_1.yaml"), tempDir.newFile("v1_2.gocd.yaml")); FileUtils.copyInputStreamToFile(getResourceAsStream("/parts/roots/version_2.yaml"), tempDir.newFile("v2_1.gocd.yaml")); GoPluginApiResponse response = parseAndGetResponseForDir(tempDir.getRoot()); String expectedFailureMessage = "java.lang.RuntimeException: Versions across files are not unique. Found" + " versions: [1, 2]. There can only be one version across the whole repository."; assertFirstError(getJsonObjectFromResponse(response), expectedFailureMessage, "YAML config plugin"); }
static PluginSettings fromJson(String json) { Map<String, String> raw = JSONUtils.fromJSON(json); return new PluginSettings(raw.get(PLUGIN_SETTINGS_FILE_PATTERN)); }
public static List<Map<String, Object>> readJsonArrayGson(String path) { return fromJson(TestUtils.createReader(path)); }
public static void addRequiredString(JsonObject jsonObject, Map<String, Object> yamlSource, String jsonField, String yamlFieldName) { String value = getOptionalString(yamlSource, yamlFieldName); if (value == null) throw new YamlConfigException("String field " + yamlFieldName + ": is required"); jsonObject.addProperty(jsonField, value); }
@Override public GoPluginIdentifier pluginIdentifier() { return getGoPluginIdentifier(); }
public static void addOptionalInteger(JsonObject jsonObject, Map<String, Object> yamlSource, String jsonField, String yamlFieldName) { Integer value = getOptionalInteger(yamlSource, yamlFieldName); if (value != null) jsonObject.addProperty(jsonField, value); }
public static void addRequiredValue(Map<String, Object> dest, Map<String, Object> src, String jsonField, String yamlFieldName) { Object value = src.get(jsonField); if (value == null) throw new YamlConfigException("field " + yamlFieldName + ": is required"); dest.put(yamlFieldName, value); } }
private File setupCase(String caseName) throws IOException { return setupCase(caseName, "gocd.yaml"); }
public static Map<String, Object> readJsonGson(String path) { return fromJson(TestUtils.createReader(path)); }