Search in sources :

Example 1 with ConfigHelperOptions

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);
        }
    }
}
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)

Example 2 with ConfigHelperOptions

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)));
}
Also used : OutputListWriter(com.thoughtworks.go.api.base.OutputListWriter) TfsMaterialConfig(com.thoughtworks.go.config.materials.tfs.TfsMaterialConfig) PluggableSCMMaterialConfig(com.thoughtworks.go.config.materials.PluggableSCMMaterialConfig) GitMaterialConfig(com.thoughtworks.go.config.materials.git.GitMaterialConfig) DependencyMaterialConfig(com.thoughtworks.go.config.materials.dependency.DependencyMaterialConfig) ErrorGetter(com.thoughtworks.go.api.representers.ErrorGetter) HashMap(java.util.HashMap) SvnMaterialConfig(com.thoughtworks.go.config.materials.svn.SvnMaterialConfig) MaterialConfig(com.thoughtworks.go.domain.materials.MaterialConfig) PackageMaterialConfig(com.thoughtworks.go.config.materials.PackageMaterialConfig) OutputWriter(com.thoughtworks.go.api.base.OutputWriter) JsonReader(com.thoughtworks.go.api.representers.JsonReader) ConfigHelperOptions(com.thoughtworks.go.apiv9.admin.shared.representers.stages.ConfigHelperOptions) HgMaterialConfig(com.thoughtworks.go.config.materials.mercurial.HgMaterialConfig) MaterialConfigs(com.thoughtworks.go.config.materials.MaterialConfigs) P4MaterialConfig(com.thoughtworks.go.config.materials.perforce.P4MaterialConfig) StringUtils.equalsIgnoreCase(org.apache.commons.lang3.StringUtils.equalsIgnoreCase) Arrays.stream(java.util.Arrays.stream) UnprocessableEntityException(com.thoughtworks.go.config.exceptions.UnprocessableEntityException) UnprocessableEntityException(com.thoughtworks.go.config.exceptions.UnprocessableEntityException) JsonReader(com.thoughtworks.go.api.representers.JsonReader)

Example 3 with ConfigHelperOptions

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();
    }
}
Also used : ExportedConfig(com.thoughtworks.go.plugin.access.configrepo.ExportedConfig) JsonReader(com.thoughtworks.go.api.representers.JsonReader) ConfigHelperOptions(com.thoughtworks.go.apiv10.admin.shared.representers.stages.ConfigHelperOptions)

Example 4 with ConfigHelperOptions

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);
}
Also used : JsonReader(com.thoughtworks.go.api.representers.JsonReader) ConfigHelperOptions(com.thoughtworks.go.apiv11.admin.shared.representers.stages.ConfigHelperOptions)

Example 5 with ConfigHelperOptions

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)));
}
Also used : OutputListWriter(com.thoughtworks.go.api.base.OutputListWriter) TfsMaterialConfig(com.thoughtworks.go.config.materials.tfs.TfsMaterialConfig) PluggableSCMMaterialConfig(com.thoughtworks.go.config.materials.PluggableSCMMaterialConfig) GitMaterialConfig(com.thoughtworks.go.config.materials.git.GitMaterialConfig) DependencyMaterialConfig(com.thoughtworks.go.config.materials.dependency.DependencyMaterialConfig) ErrorGetter(com.thoughtworks.go.api.representers.ErrorGetter) HashMap(java.util.HashMap) SvnMaterialConfig(com.thoughtworks.go.config.materials.svn.SvnMaterialConfig) MaterialConfig(com.thoughtworks.go.domain.materials.MaterialConfig) PackageMaterialConfig(com.thoughtworks.go.config.materials.PackageMaterialConfig) OutputWriter(com.thoughtworks.go.api.base.OutputWriter) JsonReader(com.thoughtworks.go.api.representers.JsonReader) ConfigHelperOptions(com.thoughtworks.go.apiv10.admin.shared.representers.stages.ConfigHelperOptions) HgMaterialConfig(com.thoughtworks.go.config.materials.mercurial.HgMaterialConfig) MaterialConfigs(com.thoughtworks.go.config.materials.MaterialConfigs) P4MaterialConfig(com.thoughtworks.go.config.materials.perforce.P4MaterialConfig) StringUtils.equalsIgnoreCase(org.apache.commons.lang3.StringUtils.equalsIgnoreCase) Arrays.stream(java.util.Arrays.stream) UnprocessableEntityException(com.thoughtworks.go.config.exceptions.UnprocessableEntityException) UnprocessableEntityException(com.thoughtworks.go.config.exceptions.UnprocessableEntityException) JsonReader(com.thoughtworks.go.api.representers.JsonReader)

Aggregations

JsonReader (com.thoughtworks.go.api.representers.JsonReader)7 ConfigHelperOptions (com.thoughtworks.go.apiv10.admin.shared.representers.stages.ConfigHelperOptions)4 MaterialConfig (com.thoughtworks.go.domain.materials.MaterialConfig)4 OutputListWriter (com.thoughtworks.go.api.base.OutputListWriter)3 OutputWriter (com.thoughtworks.go.api.base.OutputWriter)3 ErrorGetter (com.thoughtworks.go.api.representers.ErrorGetter)3 UnprocessableEntityException (com.thoughtworks.go.config.exceptions.UnprocessableEntityException)3 MaterialConfigs (com.thoughtworks.go.config.materials.MaterialConfigs)3 PackageMaterialConfig (com.thoughtworks.go.config.materials.PackageMaterialConfig)3 PluggableSCMMaterialConfig (com.thoughtworks.go.config.materials.PluggableSCMMaterialConfig)3 DependencyMaterialConfig (com.thoughtworks.go.config.materials.dependency.DependencyMaterialConfig)3 GitMaterialConfig (com.thoughtworks.go.config.materials.git.GitMaterialConfig)3 HgMaterialConfig (com.thoughtworks.go.config.materials.mercurial.HgMaterialConfig)3 P4MaterialConfig (com.thoughtworks.go.config.materials.perforce.P4MaterialConfig)3 SvnMaterialConfig (com.thoughtworks.go.config.materials.svn.SvnMaterialConfig)3 TfsMaterialConfig (com.thoughtworks.go.config.materials.tfs.TfsMaterialConfig)3 Arrays.stream (java.util.Arrays.stream)3 HashMap (java.util.HashMap)3 StringUtils.equalsIgnoreCase (org.apache.commons.lang3.StringUtils.equalsIgnoreCase)3 ConfigHelperOptions (com.thoughtworks.go.apiv11.admin.shared.representers.stages.ConfigHelperOptions)2