Search in sources :

Example 11 with UnexpectedResponseStatusException

use of com.epam.pipeline.exception.git.UnexpectedResponseStatusException in project cloud-pipeline by epam.

the class DockerClient method getRawImageDescription.

private RawImageDescription getRawImageDescription(DockerRegistry registry, String imageName, String tag, HttpEntity headers) {
    String url = String.format(IMAGE_DESCRIPTION_URL, registry.getPath(), imageName, tag);
    try {
        URI uri = new URI(url);
        ResponseEntity<RawImageDescription> response = getRestTemplate().exchange(uri, HttpMethod.GET, headers, new ParameterizedTypeReference<RawImageDescription>() {
        });
        if (response.getStatusCode() == HttpStatus.OK) {
            return response.getBody();
        } else {
            throw new UnexpectedResponseStatusException(response.getStatusCode());
        }
    } catch (URISyntaxException | UnexpectedResponseStatusException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DockerConnectionException(url, e.getMessage());
    }
}
Also used : RawImageDescription(com.epam.pipeline.entity.docker.RawImageDescription) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DockerConnectionException(com.epam.pipeline.exception.docker.DockerConnectionException)

Example 12 with UnexpectedResponseStatusException

use of com.epam.pipeline.exception.git.UnexpectedResponseStatusException in project cloud-pipeline by epam.

the class DockerClient method getManifest.

/**
 * Gets a V2 Manifest for a specified image and tag
 * @param registry a registry, where image is located
 * @param imageName a name of an image (repository)
 * @param tag tag name
 * @return image's manifest
 */
public Optional<ManifestV2> getManifest(DockerRegistry registry, String imageName, String tag) {
    String url = String.format(IMAGE_DESCRIPTION_URL, registry.getPath(), imageName, tag);
    try {
        URI uri = new URI(url);
        ResponseEntity<ManifestV2> response = getRestTemplate().exchange(uri, HttpMethod.GET, getV2AuthHeaders(), new ParameterizedTypeReference<ManifestV2>() {
        });
        if (response.getStatusCode() == HttpStatus.OK) {
            List<String> digest = response.getHeaders().get("docker-content-digest");
            ManifestV2 manifest = response.getBody();
            manifest.setDigest(digest.get(0));
            return Optional.of(manifest);
        } else {
            throw new UnexpectedResponseStatusException(response.getStatusCode());
        }
    } catch (URISyntaxException | UnexpectedResponseStatusException e) {
        LOGGER.error(e.getMessage(), e);
        throw new DockerConnectionException(url, e.getMessage());
    } catch (HttpClientErrorException e) {
        LOGGER.error(e.getMessage(), e);
        return Optional.empty();
    }
}
Also used : ManifestV2(com.epam.pipeline.entity.docker.ManifestV2) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DockerConnectionException(com.epam.pipeline.exception.docker.DockerConnectionException)

Example 13 with UnexpectedResponseStatusException

use of com.epam.pipeline.exception.git.UnexpectedResponseStatusException in project cloud-pipeline by epam.

the class GitlabClient method createRepositoryRevision.

public GitTagEntry createRepositoryRevision(String name, String ref, String message, String releaseDescription) throws GitClientException {
    if (name == null) {
        throw new GitClientException("Tag name is required");
    }
    if (ref == null) {
        throw new GitClientException("Ref (commit SHA, another tag name, or branch name) is required");
    }
    try {
        String projectId = makeProjectId(namespace, projectName);
        Map<String, Object> params = new HashMap<>();
        params.put("tag_name", name);
        params.put("ref", ref);
        if (message != null) {
            params.put("message", message);
        }
        if (releaseDescription != null) {
            params.put("release_description", releaseDescription);
        }
        String url = addUrlParameters(String.format(GIT_REVISIONS, gitHost, projectId), params);
        URI uri = new URI(url);
        LOGGER.trace("Creating new tag using URL: {}", uri);
        RestTemplate template = new RestTemplate();
        ResponseEntity<GitTagEntry> sourcesResponse = template.exchange(uri, HttpMethod.POST, getAuthHeaders(), new ParameterizedTypeReference<GitTagEntry>() {
        });
        if (sourcesResponse.getStatusCode() == HttpStatus.CREATED) {
            return sourcesResponse.getBody();
        } else {
            throw new UnexpectedResponseStatusException(sourcesResponse.getStatusCode());
        }
    } catch (UnsupportedEncodingException | URISyntaxException e) {
        throw new GitClientException(e);
    }
}
Also used : HashMap(java.util.HashMap) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) GitClientException(com.epam.pipeline.exception.git.GitClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RestTemplate(org.springframework.web.client.RestTemplate) GitTagEntry(com.epam.pipeline.entity.git.GitTagEntry)

Example 14 with UnexpectedResponseStatusException

use of com.epam.pipeline.exception.git.UnexpectedResponseStatusException in project cloud-pipeline by epam.

the class GitlabClient method getRepositoryContents.

/**
 * Retrieves repository contents - a list of files in a repository
 * @param path a path in a repository to browse
 * @return a list of {@link GitRepositoryEntry}, representing files and folders
 * @throws GitClientException if something goes wrong
 */
public List<GitRepositoryEntry> getRepositoryContents(String path, String revision, boolean recursive) throws GitClientException {
    try {
        String projectId = makeProjectId(namespace, projectName);
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(path)) {
            params.put("path", path);
        }
        if (StringUtils.isNotBlank(revision)) {
            params.put("ref_name", revision);
        }
        params.put("recursive", recursive);
        String url = addUrlParameters(String.format(GIT_GET_SOURCES_ROOT_URL, gitHost, projectId), params);
        URI uri = new URI(url);
        LOGGER.trace("Getting repository contents on path {}, URL: {}", path, uri);
        RestTemplate template = new RestTemplate();
        ResponseEntity<List<GitRepositoryEntry>> sourcesResponse = template.exchange(uri, HttpMethod.GET, getAuthHeaders(), new ParameterizedTypeReference<List<GitRepositoryEntry>>() {
        });
        if (sourcesResponse.getStatusCode() == HttpStatus.OK) {
            return sourcesResponse.getBody();
        } else {
            throw new UnexpectedResponseStatusException(sourcesResponse.getStatusCode());
        }
    } catch (UnsupportedEncodingException | URISyntaxException e) {
        throw new GitClientException(e);
    }
}
Also used : HashMap(java.util.HashMap) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) GitClientException(com.epam.pipeline.exception.git.GitClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RestTemplate(org.springframework.web.client.RestTemplate) List(java.util.List)

Example 15 with UnexpectedResponseStatusException

use of com.epam.pipeline.exception.git.UnexpectedResponseStatusException in project cloud-pipeline by epam.

the class GitlabClient method createFile.

private void createFile(GitProject project, String path, String content) throws URISyntaxException, UnexpectedResponseStatusException, UnsupportedEncodingException {
    HttpHeaders headers = new HttpHeaders();
    headers.add(TOKEN_HEADER, token);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("file_path", path);
    parameters.put("branch_name", DEFAULT_BRANCH);
    parameters.put("commit_message", "New pipeline initial commit");
    parameters.put("content", content);
    String url = addUrlParameters(String.format(GIT_POST_FILE_URL, gitHost, project.getId().toString(), URLEncoder.encode(path, Charset.defaultCharset().displayName())), parameters);
    URI uri = new URI(url);
    HttpEntity entity = new HttpEntity(headers);
    ResponseEntity<GitRepositoryEntry> response = new RestTemplate().exchange(uri, HttpMethod.POST, entity, new ParameterizedTypeReference<GitRepositoryEntry>() {
    });
    if (response.getStatusCode() != HttpStatus.CREATED) {
        throw new UnexpectedResponseStatusException(response.getStatusCode());
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) RestTemplate(org.springframework.web.client.RestTemplate) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry) URI(java.net.URI)

Aggregations

UnexpectedResponseStatusException (com.epam.pipeline.exception.git.UnexpectedResponseStatusException)19 URI (java.net.URI)19 URISyntaxException (java.net.URISyntaxException)15 RestTemplate (org.springframework.web.client.RestTemplate)14 GitClientException (com.epam.pipeline.exception.git.GitClientException)10 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 HashMap (java.util.HashMap)8 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)8 HttpEntity (org.springframework.http.HttpEntity)6 DockerConnectionException (com.epam.pipeline.exception.docker.DockerConnectionException)4 HttpHeaders (org.springframework.http.HttpHeaders)4 List (java.util.List)3 GitCommitEntry (com.epam.pipeline.entity.git.GitCommitEntry)2 GitProject (com.epam.pipeline.entity.git.GitProject)2 GitRepositoryEntry (com.epam.pipeline.entity.git.GitRepositoryEntry)2 GitTagEntry (com.epam.pipeline.entity.git.GitTagEntry)2 PipelineSourceItemErrorVO (com.epam.pipeline.controller.vo.PipelineSourceItemErrorVO)1 ManifestV2 (com.epam.pipeline.entity.docker.ManifestV2)1 RawImageDescription (com.epam.pipeline.entity.docker.RawImageDescription)1 RegistryListing (com.epam.pipeline.entity.docker.RegistryListing)1