use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.
the class AdminService method updateConfig.
public GoConfigValidity updateConfig(Map attributes, HttpLocalizedOperationResult result) {
GoConfigValidity validity;
String configXml = (String) attributes.get("content");
String configMd5 = (String) attributes.get("md5");
GoConfigService.XmlPartialSaver fileSaver = goConfigService.fileSaver(false);
validity = fileSaver.saveXml(configXml, configMd5);
if (!validity.isValid()) {
result.badRequest(LocalizedMessage.string("SAVE_FAILED"));
}
return validity;
}
use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.
the class PipelineConfigsService method updateXml.
public GoConfigOperationalResponse<PipelineConfigs> updateXml(String groupName, String xmlPartial, final String md5, Username username, HttpLocalizedOperationResult result) throws Exception {
if (!userHasPermissions(username, groupName, result)) {
return new GoConfigOperationalResponse<>(GoConfigValidity.valid(), null);
}
GoConfigValidity goConfigValidity = goConfigService.groupSaver(groupName).saveXml(xmlPartial, md5);
if (!goConfigValidity.isValid()) {
handleError(groupName, goConfigValidity, result);
return new GoConfigOperationalResponse<>(goConfigValidity, null);
}
Localizable savedSuccessMessage = LocalizedMessage.string("SAVED_CONFIGURATION_SUCCESSFULLY");
Localizable localizableMessage = goConfigValidity.wasMerged() ? LocalizedMessage.composite(savedSuccessMessage, LocalizedMessage.string("CONFIG_MERGED")) : savedSuccessMessage;
result.setMessage(localizableMessage);
PipelineConfigs pipelineConfigs = magicalGoConfigXmlLoader.fromXmlPartial(xmlPartial, BasicPipelineConfigs.class);
return new GoConfigOperationalResponse<>(goConfigValidity, pipelineConfigs);
}
use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.
the class GoConfigAdministrationController method postXmlPartial.
private RestfulAction postXmlPartial(String groupName, GoConfigService.XmlPartialSaver xmlPartialSaver, String xmlPartial, String successMessage, String expectedMd5) {
if (!isTemplate(groupName) && !isCurrentUserAdminOfGroup(groupName)) {
return JsonAction.jsonUnauthorized(errorMessageForGroup(groupName));
}
if (isTemplate(groupName) && !isCurrentUserAdmin()) {
return JsonAction.jsonUnauthorized();
}
GoConfigValidity configValidity = xmlPartialSaver.saveXml(xmlPartial, expectedMd5);
if (configValidity.isValid()) {
return JsonAction.jsonFound(JsonView.getSimpleAjaxResult("result", successMessage));
} else {
Map<String, Object> jsonMap = new LinkedHashMap<>();
jsonMap.put("result", configValidity.errorMessage());
jsonMap.put("originalContent", xmlPartial);
return jsonByValidity(jsonMap, configValidity);
}
}
use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.
the class GoConfigServiceIntegrationTest method shouldNotThrowUpOnConfigSaveWhenIndependentChangesAreMade_ViaMergeFlow.
@Test
public void shouldNotThrowUpOnConfigSaveWhenIndependentChangesAreMade_ViaMergeFlow() throws Exception {
// Priming current configuration to add lines simulating the license section before removal
for (int i = 0; i < 10; i++) {
configHelper.addRole(new RoleConfig(new CaseInsensitiveString("admin_role_" + i), new RoleUser(new CaseInsensitiveString("admin_user_" + i))));
}
// User 1 loads page
CruiseConfig user1SeeingConfig = goConfigDao.loadForEditing();
String user1SeeingMd5 = user1SeeingConfig.getMd5();
// User 2 edits config
configHelper.addPipelineWithGroup("defaultGroup", "user2_pipeline", "user2_stage", "user2_job");
CruiseConfig user2SeeingConfig = configHelper.load();
// User 1 edits old config to make an independent change
new GoConfigMother().addRole(user1SeeingConfig, new RoleConfig(new CaseInsensitiveString("admin_role"), new RoleUser(new CaseInsensitiveString("admin_user"))));
ByteArrayOutputStream os = new ByteArrayOutputStream();
configHelper.getXml(user1SeeingConfig, os);
// User 1 saves edited config
GoConfigService.XmlPartialSaver saver = goConfigService.fileSaver(false);
GoConfigValidity validity = saver.saveXml(os.toString(), user1SeeingMd5);
assertThat(validity.errorMessage(), validity.isValid(), is(true));
}
use of com.thoughtworks.go.config.validation.GoConfigValidity in project gocd by gocd.
the class GoConfigServiceIntegrationTest method shouldThrowUpOnConfigSaveMergeConflict_ViaMergeFlow.
@Test
public void shouldThrowUpOnConfigSaveMergeConflict_ViaMergeFlow() throws Exception {
// User 1 loads page
CruiseConfig user1SeeingConfig = goConfigDao.loadForEditing();
String user1SeeingMd5 = user1SeeingConfig.getMd5();
// User 2 edits config
configHelper.addPipelineWithGroup("defaultGroup", "user2_pipeline", "user2_stage", "user2_job");
CruiseConfig user2SeeingConfig = configHelper.load();
// User 1 edits old config
new GoConfigMother().addPipelineWithGroup(user1SeeingConfig, "defaultGroup", "user1_pipeline", "user1_stage", "user1_job");
ByteArrayOutputStream os = new ByteArrayOutputStream();
configHelper.getXml(user1SeeingConfig, os);
// User 1 saves edited config
GoConfigService.XmlPartialSaver saver = goConfigService.fileSaver(false);
GoConfigValidity validity = saver.saveXml(os.toString(), user1SeeingMd5);
assertThat(validity.isValid(), is(false));
assertThat(validity.isType(GoConfigValidity.VT_MERGE_OPERATION_ERROR), is(true));
assertThat(validity.errorMessage(), is("Configuration file has been modified by someone else."));
}
Aggregations