Search in sources :

Example 1 with ConfigFileList

use of com.thoughtworks.go.plugin.access.configrepo.ConfigFileList 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);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ConfigFileList(com.thoughtworks.go.plugin.access.configrepo.ConfigFileList) ConfigHelperOptions(com.thoughtworks.go.apiv10.admin.shared.representers.stages.ConfigHelperOptions) HaltException(spark.HaltException) MaterialConfig(com.thoughtworks.go.domain.materials.MaterialConfig) ScmMaterialConfig(com.thoughtworks.go.config.materials.ScmMaterialConfig) JsonReader(com.thoughtworks.go.api.representers.JsonReader) File(java.io.File) ScmMaterialConfig(com.thoughtworks.go.config.materials.ScmMaterialConfig)

Aggregations

JsonReader (com.thoughtworks.go.api.representers.JsonReader)1 ConfigHelperOptions (com.thoughtworks.go.apiv10.admin.shared.representers.stages.ConfigHelperOptions)1 ScmMaterialConfig (com.thoughtworks.go.config.materials.ScmMaterialConfig)1 MaterialConfig (com.thoughtworks.go.domain.materials.MaterialConfig)1 ConfigFileList (com.thoughtworks.go.plugin.access.configrepo.ConfigFileList)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HaltException (spark.HaltException)1