private void addTimer(JsonObject pipeline, Map<String, Object> pipeMap) { Object timer = pipeMap.get(YAML_PIPELINE_TIMER_FIELD); if (timer == null) return; JsonObject timerJson = new JsonObject(); Map<String, Object> timerMap = (Map<String, Object>) timer; addRequiredString(timerJson, timerMap, "spec", "spec"); addOptionalBoolean(timerJson, timerMap, "only_on_changes", "only_on_changes"); pipeline.add(JSON_PIPELINE_TIMER_FIELD, timerJson); }
private void addApproval(JsonObject stage, Map<String, Object> stageMap) { Object approval = stageMap.get(YAML_STAGE_APPROVAL_FIELD); if (approval == null) return; JsonObject approvalJson = new JsonObject(); if (approval instanceof String) { // shorthand if ("auto".equals(approval) || "success".equals(approval)) approvalJson.addProperty(JSON_STAGE_APPROVAL_TYPE_FIELD, "success"); else if ("manual".equals(approval)) approvalJson.addProperty(JSON_STAGE_APPROVAL_TYPE_FIELD, "manual"); else throw new YamlConfigException("Approval should be a hash or string [auto or manual]"); } else { Map<String, Object> approvalMap = (Map<String, Object>) approval; addRequiredString(approvalJson, approvalMap, JSON_STAGE_APPROVAL_TYPE_FIELD, YAML_STAGE_APPROVAL_TYPE_FIELD); addOptionalStringList(approvalJson, approvalMap, JSON_STAGE_APPROVAL_USERS_FIELD, YAML_STAGE_APPROVAL_USERS_FIELD); addOptionalStringList(approvalJson, approvalMap, JSON_STAGE_APPROVAL_ROLES_FIELD, YAML_STAGE_APPROVAL_ROLES_FIELD); } stage.add(JSON_STAGE_APPROVAL_FIELD, approvalJson); }
public static void addOptionalBoolean(JsonObject material, Map<String, Object> map, String jsonFieldName, String yamlFieldName) { Boolean autoUpdate = getOptionalBoolean(map, yamlFieldName); if (autoUpdate != null) { material.addProperty(jsonFieldName, autoUpdate); } }
public String inverseTransformPipeline(Map<String, Object> pipeline) { Map<String, Object> result = new LinkedTreeMap<>(); result.put("format_version", 3); result.put("pipelines", pipelineTransform.inverseTransform(pipeline)); return YamlUtils.dump(result); }
public JsonObject transform(Map.Entry<String, Object> entry) { String pipelineName = entry.getKey(); JsonObject pipeline = new JsonObject(); pipeline.addProperty(JSON_PIPELINE_NAME_FIELD, pipelineName); Map<String, Object> pipeMap = (Map<String, Object>) entry.getValue(); addOptionalString(pipeline, pipeMap, JSON_PIPELINE_GROUP_FIELD, YAML_PIPELINE_GROUP_FIELD); addOptionalString(pipeline, pipeMap, JSON_PIPELINE_TEMPLATE_FIELD, YAML_PIPELINE_TEMPLATE_FIELD); addOptionalString(pipeline, pipeMap, JSON_PIPELINE_LABEL_TEMPLATE_FIELD, YAML_PIPELINE_LABEL_TEMPLATE_FIELD); addOptionalBoolean(pipeline, pipeMap, JSON_PIPELINE_PIPE_LOCKING_FIELD, YAML_PIPELINE_PIPE_LOCKING_FIELD); addOptionalString(pipeline, pipeMap, JSON_PIPELINE_LOCK_BEHAVIOR_FIELD, YAML_PIPELINE_LOCK_BEHAVIOR_FIELD); addOptionalObject(pipeline, pipeMap, JSON_PIPELINE_TRACKING_TOOL_FIELD, YAML_PIPELINE_TRACKING_TOOL_FIELD); addOptionalObject(pipeline, pipeMap, JSON_PIPELINE_MINGLE_FIELD, YAML_PIPELINE_MINGLE_FIELD); addTimer(pipeline, pipeMap); JsonArray jsonEnvVariables = variablesTransform.transform(pipeMap); if (jsonEnvVariables != null && jsonEnvVariables.size() > 0) pipeline.add(JSON_ENV_VAR_FIELD, jsonEnvVariables); addMaterials(pipeline, pipeMap); if (!pipeline.has(JSON_PIPELINE_TEMPLATE_FIELD)) { addStages(pipeline, pipeMap); } JsonArray params = parameterTransform.transform(pipeMap); if (params != null && params.size() > 0) { pipeline.add(YAML_PIPELINE_PARAMETERS_FIELD, params); } return pipeline; }
public JsonObject transform(String jobName, Map<String, Object> jobMap) { JsonObject jobJson = new JsonObject(); jobJson.addProperty(JSON_JOB_NAME_FIELD, jobName); addOptionalInteger(jobJson, jobMap, JSON_JOB_TIMEOUT_FIELD, YAML_JOB_TIMEOUT_FIELD); addRunInstances(jobMap, jobJson); JsonArray jsonEnvVariables = environmentTransform.transform(jobMap); if (jsonEnvVariables != null && jsonEnvVariables.size() > 0) jobJson.add(JSON_ENV_VAR_FIELD, jsonEnvVariables); addTabs(jobJson, jobMap); addOptionalStringList(jobJson, jobMap, JSON_JOB_RESOURCES_FIELD, YAML_JOB_RESOURCES_FIELD); addOptionalString(jobJson, jobMap, JSON_JOB_ELASTIC_PROFILE_FIELD, YAML_JOB_ELASTIC_PROFILE_FIELD); addArtifacts(jobJson, jobMap); addProperties(jobJson, jobMap); addTasks(jobJson, jobMap); return jobJson; }
material.addProperty(JSON_MATERIAL_NAME_FIELD, materialName); Map<String, Object> materialMap = (Map<String, Object>) entry.getValue(); addOptionalString(material, materialMap, JSON_MATERIAL_TYPE_FIELD, YAML_MATERIAL_TYPE_FIELD); addOptionalBoolean(material, materialMap, JSON_MATERIAL_AUTO_UPDATE_FIELD, YAML_MATERIAL_AUTO_UPDATE_FIELD); addOptionalBoolean(material, materialMap, JSON_MATERIAL_SHALLOW_CLONE_FIELD, YAML_MATERIAL_SHALLOW_CLONE_FIELD); addOptionalBoolean(material, materialMap, JSON_MATERIAL_CHECK_EXTERNALS_FIELD, YAML_MATERIAL_CHECK_EXTERNALS_FIELD); addOptionalBoolean(material, materialMap, JSON_MATERIAL_USE_TICKETS_FIELD, YAML_MATERIAL_USE_TICKETS_FIELD); if (materialMap.containsKey("blacklist")) addFilter(material, materialMap.get("blacklist"), "ignore"); String git = getOptionalString(materialMap, YAML_SHORT_KEYWORD_GIT); if (git != null) { material.addProperty(JSON_MATERIAL_TYPE_FIELD, "git"); material.addProperty("url", git); String svn = getOptionalString(materialMap, YAML_SHORT_KEYWORD_SVN); if (svn != null) { material.addProperty(JSON_MATERIAL_TYPE_FIELD, "svn"); material.addProperty("url", svn); String hg = getOptionalString(materialMap, YAML_SHORT_KEYWORD_HG); if (hg != null) { material.addProperty(JSON_MATERIAL_TYPE_FIELD, "hg"); material.addProperty("url", hg); String dependency = getOptionalString(materialMap, YAML_SHORT_KEYWORD_DEPENDENCY); if (dependency != null) { material.addProperty(JSON_MATERIAL_TYPE_FIELD, "dependency");
addOptionalObject(taskJson, taskMap, JSON_PLUGIN_CONFIGURATION_FIELD, YAML_PLUGIN_CONFIGURATION_FIELD); super.addConfiguration(taskJson, (Map<String, Object>) taskMap.get(JSON_PLUGIN_CONFIGURATION_FIELD)); } else { addOptionalObject(taskJson, taskMap, JSON_TASK_PLUGIN_CONFIGURATION_FIELD, YAML_PLUGIN_CONFIGURATION_FIELD); super.addConfiguration(taskJson, taskMap); addOptionalBoolean(taskJson, taskMap, JSON_TASK_IS_FILE_FIELD, YAML_TASK_IS_FILE_FIELD); addOptionalStringList(taskJson, taskMap, JSON_TASK_EXEC_ARGS_FIELD, YAML_TASK_EXEC_ARGS_FIELD);
addRequiredString(artifactJson, artMapValue, JSON_JOB_ARTIFACT_ARTIFACT_ID_FIELD, YAML_JOB_ARTIFACT_ARTIFACT_ID_FIELD); addRequiredString(artifactJson, artMapValue, JSON_JOB_ARTIFACT_STORE_ID_FIELD, YAML_JOB_ARTIFACT_STORE_ID_FIELD); super.addConfiguration(artifactJson, (Map<String, Object>) artMapValue.get("configuration")); } else { addRequiredString(artifactJson, artMapValue, JSON_JOB_ARTIFACT_SOURCE_FIELD, YAML_JOB_ARTIFACT_SOURCE_FIELD); addOptionalString(artifactJson, artMapValue, JSON_JOB_ARTIFACT_DEST_FIELD, YAML_JOB_ARTIFACT_DEST_FIELD);
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 addOptionalObject(JsonObject jsonObject, Map<String, Object> yamlSource, String jsonField, String yamlFieldName) { Object obj = getOptionalObject(yamlSource, yamlFieldName); if (obj != null) jsonObject.add(jsonField, new Gson().toJsonTree(obj)); }
public JsonObject transform(Map.Entry<String, Object> env) { String envName = env.getKey(); JsonObject envJson = new JsonObject(); envJson.addProperty(JSON_ENV_NAME_FIELD, envName); Object envObj = env.getValue(); if (envObj == null) return envJson; if (!(envObj instanceof Map)) throw new YamlConfigException("Expected environment to be a hash"); Map<String, Object> envMap = (Map<String, Object>) envObj; addOptionalStringList(envJson, envMap, "agents", "agents"); addOptionalStringList(envJson, envMap, "pipelines", "pipelines"); JsonArray jsonEnvVariables = environmentVariablesTransform.transform(envMap); if (jsonEnvVariables != null && jsonEnvVariables.size() > 0) envJson.add(JSON_ENV_VAR_FIELD, jsonEnvVariables); return envJson; } }
public JsonObject transform(Map.Entry<String, Object> entry) { String stageName = entry.getKey(); JsonObject stage = new JsonObject(); stage.addProperty(JSON_STAGE_NAME_FIELD, stageName); Map<String, Object> stageMap = (Map<String, Object>) entry.getValue(); addOptionalBoolean(stage, stageMap, JSON_STAGE_FETCH_FIELD, YAML_STAGE_FETCH_FIELD); addOptionalBoolean(stage, stageMap, JSON_STAGE_NEVER_CLEAN_FIELD, YAML_STAGE_KEEP_ARTIFACTS_FIELD); addOptionalBoolean(stage, stageMap, JSON_STAGE_CLEAN_WORK_FIELD, YAML_STAGE_CLEAN_WORK_FIELD); addApproval(stage, stageMap); JsonArray jsonEnvVariables = environmentTransform.transform(stageMap); if (jsonEnvVariables != null && jsonEnvVariables.size() > 0) stage.add(JSON_ENV_VAR_FIELD, jsonEnvVariables); addJobs(stage, stageMap); return stage; }
private void addProperties(JsonObject jobJson, Map<String, Object> jobMap) { Object props = jobMap.get(YAML_JOB_PROPS_FIELD); if (props == null) return; if (!(props instanceof Map)) throw new YamlConfigException("properties should be a hash"); JsonArray propsJson = new JsonArray(); Map<String, Object> propsMap = (Map<String, Object>) props; for (Map.Entry<String, Object> propEntry : propsMap.entrySet()) { String propName = propEntry.getKey(); Object propObj = propEntry.getValue(); if (!(propObj instanceof Map)) throw new YamlConfigException("property " + propName + " should be a hash"); Map<String, Object> propMap = (Map<String, Object>) propObj; JsonObject propJson = new JsonObject(); propJson.addProperty(JSON_JOB_PROP_NAME_FIELD, propName); addRequiredString(propJson, propMap, JSON_JOB_PROP_SOURCE_FIELD, YAML_JOB_PROP_SOURCE_FIELD); addRequiredString(propJson, propMap, JSON_JOB_PROP_XPATH_FIELD, YAML_JOB_PROP_XPATH_FIELD); propsJson.add(propJson); } jobJson.add(JSON_JOB_PROPS_FIELD, propsJson); }
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)); } }