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); } }
public static Boolean getOptionalBoolean(Map<String, Object> map, String fieldName) { Object value = map.get(fieldName); if (value != null) { if (value instanceof Boolean) return (Boolean) value; String boolText = (String) value; if (truePattern.matcher(boolText).matches()) return true; else if (falsePattern.matcher(boolText).matches()) return false; throw new YamlConfigException("Expected boolean value in field " + fieldName + ", got " + boolText); } return null; }
private void addTasks(JsonObject jobJson, Map<String, Object> jobMap) { Object tasksObj = jobMap.get(YAML_JOB_TASKS_FIELD); if (tasksObj == null) throw new YamlConfigException("tasks are required in a job"); JsonArray tasksJson = new JsonArray(); List<Object> taskList = (List<Object>) tasksObj; addTasks(taskList, tasksJson); jobJson.add(JSON_JOB_TASKS_FIELD, tasksJson); }
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); }
private void addOnCancel(JsonObject taskJson, Map<String, Object> taskMap) { Object on_cancel = taskMap.get(YAML_TASK_CANCEL_FIELD); if (on_cancel != null) { if (!(on_cancel instanceof Map)) throw new YamlConfigException("expected on_cancel task to be a hash"); JsonObject onCancelJson = transform(on_cancel); taskJson.add(JSON_TASK_CANCEL_FIELD, onCancelJson); } } }
private void addStages(JsonObject pipeline, Map<String, Object> pipeMap) { Object stages = pipeMap.get(YAML_PIPELINE_STAGES_FIELD); if (!(stages instanceof List)) throw new YamlConfigException("expected a list of pipeline stages or a template reference"); List<Object> stagesList = (List<Object>) stages; JsonArray stagesArray = transformStages(stagesList); pipeline.add(JSON_PIPELINE_STAGES_FIELD, stagesArray); }
private void addMaterials(JsonObject pipeline, Map<String, Object> pipeMap) { Object materials = pipeMap.get(YAML_PIPELINE_MATERIALS_FIELD); if (!(materials instanceof Map)) throw new YamlConfigException("expected a hash of pipeline materials"); Map<String, Object> materialsMap = (Map<String, Object>) materials; JsonArray materialsArray = transformMaterials(materialsMap); pipeline.add(JSON_PIPELINE_MATERIALS_FIELD, materialsArray); }
private void addInverseOnCancel(Map<String, Object> taskData, Map<String, Object> task) { Object on_cancel = task.get(JSON_TASK_CANCEL_FIELD); if (on_cancel != null) { if (!(on_cancel instanceof LinkedTreeMap)) throw new YamlConfigException("expected on_cancel task to be a hash"); taskData.put(YAML_TASK_CANCEL_FIELD, inverseTransform((Map<String, Object>) on_cancel)); } }
private void addTabs(JsonObject jobJson, Map<String, Object> jobMap) { Object tabs = jobMap.get(YAML_JOB_TABS_FIELD); if (tabs == null) return; if (!(tabs instanceof Map)) throw new YamlConfigException("tabs should be a hash"); JsonArray tabsJson = new JsonArray(); Map<String, String> tabsMap = (Map<String, String>) tabs; for (Map.Entry<String, String> tab : tabsMap.entrySet()) { String tabName = tab.getKey(); String tabPath = tab.getValue(); JsonObject tabJson = new JsonObject(); tabJson.addProperty(JSON_JOB_TAB_NAME_FIELD, tabName); tabJson.addProperty(JSON_JOB_TAB_PATH_FIELD, tabPath); tabsJson.add(tabJson); } jobJson.add(JSON_JOB_TABS_FIELD, tabsJson); }
private void addRunInstances(Map<String, Object> jobMap, JsonObject jobJson) { String runInstancesText = getOptionalString(jobMap, YAML_JOB_RUN_INSTANCES_FIELD); if (runInstancesText != null) { if ("all".equalsIgnoreCase(runInstancesText)) jobJson.addProperty(JSON_JOB_RUN_INSTANCES_FIELD, "all"); else { try { jobJson.addProperty(JSON_JOB_RUN_INSTANCES_FIELD, NumberFormat.getInstance().parse(runInstancesText)); } catch (ParseException e) { throw new YamlConfigException(YAML_JOB_RUN_INSTANCES_FIELD + " must be 'all' or a number", e); } } } }
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 addJobs(JsonObject stage, Map<String, Object> stageMap) { JsonArray jsonJobs = new JsonArray(); Object jobsObj = stageMap.get("jobs"); if (jobsObj == null) { //there are no jobs, then it might be a single-job stage Object tasks = stageMap.get("tasks"); if (tasks == null) throw new YamlConfigException("Stage must have jobs defined, or tasks if only one job should exist"); // single-job stage - expect to find job definition in the stage definition JsonObject job = jobTransform.transform(stage.get(JSON_STAGE_NAME_FIELD).getAsString(), stageMap); jsonJobs.add(job); } else { // standard definition Map<String, Object> jobsMap = (Map<String, Object>) jobsObj; for (Map.Entry<String, Object> entry : jobsMap.entrySet()) { JsonObject job = jobTransform.transform(entry); jsonJobs.add(job); } } stage.add(JSON_STAGE_JOBS_FIELD, jsonJobs); } }
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; } }
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); }
return; if (!(artifacts instanceof List)) throw new YamlConfigException("artifacts should be a list of hashes"); JsonArray artifactArrayJson = new JsonArray(); List<Object> artifactsList = (List<Object>) artifacts; for (Object artifactObj : artifactsList) { if (!(artifactObj instanceof Map)) throw new YamlConfigException("artifact should be a hash - build:, test: or external:"); artifactJson.addProperty("type", "external"); } else throw new YamlConfigException("expected build:, test:, or external: in artifact, got " + artMap.getKey());
throw new YamlConfigException("expected task " + taskType + " to be a hash"); Map<String, Object> taskMap = (Map<String, Object>) taskEntry.getValue(); addOnCancel(taskJson, taskMap);
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");