use of com.thoughtworks.go.apiv9.admin.shared.representers.stages.ConfigHelperOptions in project gocd by gocd.
the class PipelinesAsCodeInternalControllerV1 method configFiles.
String configFiles(Request req, Response res) {
ConfigRepoPlugin repoPlugin = pluginFromRequest(req);
File folder = null;
try {
JsonReader jsonReader = GsonTransformer.getInstance().jsonReaderFrom(req.body());
ConfigHelperOptions options = new ConfigHelperOptions(goConfigService.getCurrentConfig(), passwordDeserializer);
MaterialConfig materialConfig = MaterialsRepresenter.fromJSON(jsonReader, options);
if (!(materialConfig instanceof ScmMaterialConfig)) {
res.status(HttpStatus.UNPROCESSABLE_ENTITY.value());
return MessageJson.create(format("This material check requires an SCM repository; instead, supplied material was of type: %s", materialConfig.getType()));
}
validateMaterial(materialConfig);
if (materialConfig.errors().present()) {
res.status(HttpStatus.UNPROCESSABLE_ENTITY.value());
return MessageJson.create(format("Please fix the following SCM configuration errors: %s", materialConfig.errors().asString()));
}
if (configRepoService.hasConfigRepoByFingerprint(materialConfig.getFingerprint())) {
res.status(HttpStatus.CONFLICT.value());
return MessageJson.create("Material is already being used as a config repository");
}
folder = FileUtil.createTempFolder();
checkoutFromMaterialConfig(materialConfig, folder);
final Map<String, ConfigFileList> pacPluginFiles = Collections.singletonMap(repoPlugin.id(), repoPlugin.getConfigFiles(folder, new ArrayList<>()));
return jsonizeAsTopLevelObject(req, w -> ConfigFileListsRepresenter.toJSON(w, pacPluginFiles));
} catch (TimeoutException e) {
res.status(HttpStatus.PAYLOAD_TOO_LARGE.value());
return MessageJson.create("Aborted check because cloning the SCM repository took too long");
} catch (ExecutionException e) {
res.status(HttpStatus.INTERNAL_SERVER_ERROR.value());
// unwrap these exceptions thrown by the future
return MessageJson.create(e.getCause().getMessage());
} catch (Exception e) {
res.status(HttpStatus.INTERNAL_SERVER_ERROR.value());
return MessageJson.create(e.getMessage());
} finally {
if (null != folder) {
FileUtils.deleteQuietly(folder);
}
}
}
use of com.thoughtworks.go.apiv9.admin.shared.representers.stages.ConfigHelperOptions in project gocd by gocd.
the class MaterialsRepresenter method fromJSON.
public static MaterialConfig fromJSON(JsonReader jsonReader, ConfigHelperOptions options) {
String type = jsonReader.getString("type");
JsonReader attributes = jsonReader.readJsonObject("attributes");
return stream(Materials.values()).filter(material -> equalsIgnoreCase(type, material.name())).findFirst().map(material -> material.representer.fromJSON(attributes, options)).orElseThrow(() -> new UnprocessableEntityException(String.format("Invalid material type %s. It has to be one of 'git, svn, hg, p4, tfs, dependency, package, plugin'.", type)));
}
use of com.thoughtworks.go.apiv9.admin.shared.representers.stages.ConfigHelperOptions in project gocd by gocd.
the class PipelinesAsCodeInternalControllerV1 method preview.
String preview(Request req, Response res) {
ConfigRepoPlugin repoPlugin = pluginFromRequest(req);
String pluginId = repoPlugin.id();
String groupName = requiredQueryParam(req, "group");
if (!pluginService.supportsPipelineExport(pluginId)) {
throw haltBecauseOfReason("Plugin `%s` does not support pipeline config export.", pluginId);
}
ConfigHelperOptions options = new ConfigHelperOptions(goConfigService.getCurrentConfig(), passwordDeserializer);
JsonReader jsonReader = GsonTransformer.getInstance().jsonReaderFrom(req.body());
validateNamePresent(jsonReader);
PipelineConfig pipeline = PipelineConfigRepresenter.fromJSON(jsonReader, options);
if (requiresFullValidation(req) && !isValidPipelineConfig(pipeline, groupName)) {
res.status(HttpStatus.UNPROCESSABLE_ENTITY.value());
return MessageJson.create(format("Please fix the validation errors for pipeline %s.", pipeline.name()), jsonWriter(pipeline));
}
String etag = entityHashingService.hashForEntity(pipeline, groupName, pluginId);
if (fresh(req, etag)) {
return notModified(res);
} else {
setEtagHeader(res, etag);
ExportedConfig export = repoPlugin.pipelineExport(pipeline, groupName);
res.header("Content-Type", export.getContentType());
res.header("Content-Disposition", format("attachment; filename=\"%s\"", export.getFilename()));
return export.getContent();
}
}
use of com.thoughtworks.go.apiv9.admin.shared.representers.stages.ConfigHelperOptions in project gocd by gocd.
the class PipelineConfigControllerV11 method buildEntityFromRequestBody.
@Override
public PipelineConfig buildEntityFromRequestBody(Request req) {
ConfigHelperOptions options = new ConfigHelperOptions(goConfigService.getCurrentConfig(), passwordDeserializer);
JsonReader jsonReader = GsonTransformer.getInstance().jsonReaderFrom(req.body());
if ("PUT".equalsIgnoreCase(req.requestMethod())) {
return PipelineConfigRepresenter.fromJSON(jsonReader, options);
}
return PipelineConfigRepresenter.fromJSON(jsonReader.readJsonObject("pipeline"), options);
}
use of com.thoughtworks.go.apiv9.admin.shared.representers.stages.ConfigHelperOptions in project gocd by gocd.
the class MaterialsRepresenter method fromJSON.
public static MaterialConfig fromJSON(JsonReader jsonReader, ConfigHelperOptions options) {
String type = jsonReader.getString("type");
JsonReader attributes = jsonReader.readJsonObject("attributes");
return stream(Materials.values()).filter(material -> equalsIgnoreCase(type, material.name())).findFirst().map(material -> material.representer.fromJSON(attributes, options)).orElseThrow(() -> new UnprocessableEntityException(String.format("Invalid material type %s. It has to be one of 'git, svn, hg, p4, tfs, dependency, package, plugin'.", type)));
}
Aggregations