Search in sources :

Example 16 with UnexpectedResponseStatusException

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

the class GitlabClient method getFileContents.

public byte[] getFileContents(String projectId, String path, String revision) throws GitClientException {
    Assert.isTrue(StringUtils.isNotBlank(path), "File path can't be null");
    Assert.isTrue(StringUtils.isNotBlank(revision), "Revision can't be null");
    try {
        if (StringUtils.isBlank(projectId)) {
            projectId = makeProjectId(namespace, projectName);
        }
        Map<String, Object> params = new HashMap<>();
        params.put("file_path", path);
        params.put("ref", revision);
        String url = addUrlParameters(String.format(GIT_GET_SOURCE_FILE_URL, gitHost, projectId), params);
        URI uri = new URI(url);
        LOGGER.trace("Getting file contents on path {}, URL: {}", path, uri);
        RestTemplate template = new RestTemplate();
        ResponseEntity<GitFile> sourcesResponse = template.exchange(uri, HttpMethod.GET, getAuthHeaders(), GitFile.class);
        if (sourcesResponse.getStatusCode() == HttpStatus.OK) {
            return Base64.getDecoder().decode(sourcesResponse.getBody().getContent());
        } else {
            throw new UnexpectedResponseStatusException(sourcesResponse.getStatusCode());
        }
    } catch (UnsupportedEncodingException | URISyntaxException | UnexpectedResponseStatusException e) {
        throw new GitClientException(e);
    }
}
Also used : HashMap(java.util.HashMap) GitFile(com.epam.pipeline.entity.git.GitFile) 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)

Example 17 with UnexpectedResponseStatusException

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

the class GitlabClient method deleteRepository.

public void deleteRepository() throws UnexpectedResponseStatusException, URISyntaxException, UnsupportedEncodingException {
    String projectId = makeProjectId(namespace, projectName);
    String url = addUrlParameters(String.format(GIT_PROJECT_URL, gitHost, projectId), null);
    URI uri = new URI(url);
    ResponseEntity<Void> response = new RestTemplate().exchange(uri, HttpMethod.DELETE, getAuthHeaders(), new ParameterizedTypeReference<Void>() {
    });
    if (response.getStatusCode() != HttpStatus.OK) {
        throw new UnexpectedResponseStatusException(response.getStatusCode());
    }
}
Also used : UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) RestTemplate(org.springframework.web.client.RestTemplate) URI(java.net.URI)

Example 18 with UnexpectedResponseStatusException

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

the class GitlabClient method projectExists.

public boolean projectExists(String name) throws GitClientException {
    String project = convertPipeNameToProject(name);
    try {
        URI uri = new URI(String.format(GIT_PROJECT_URL, gitHost, makeProjectId(adminName, project)));
        ResponseEntity<GitProject> response = new RestTemplate().exchange(uri, HttpMethod.GET, getAuthHeaders(), new ParameterizedTypeReference<GitProject>() {
        });
        return response.getStatusCode() == HttpStatus.OK;
    } catch (HttpClientErrorException e) {
        if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
            return false;
        }
        throw new UnexpectedResponseStatusException(e.getStatusCode());
    } catch (URISyntaxException | UnsupportedEncodingException e) {
        throw new GitClientException(e.getMessage(), e);
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) GitProject(com.epam.pipeline.entity.git.GitProject) RestTemplate(org.springframework.web.client.RestTemplate) GitClientException(com.epam.pipeline.exception.git.GitClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 19 with UnexpectedResponseStatusException

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

the class GitlabClient method addProjectHook.

private GitRepositoryEntry addProjectHook(String projectId, String hookUrl) throws UnsupportedEncodingException, UnexpectedResponseStatusException, URISyntaxException {
    HttpHeaders headers = new HttpHeaders();
    headers.add(TOKEN_HEADER, token);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("url", hookUrl);
    parameters.put("push_events", true);
    parameters.put("push_events_branch_filter", DEFAULT_BRANCH);
    parameters.put("tag_push_events", true);
    parameters.put("enable_ssl_verification", false);
    String url = addUrlParameters(String.format(GITLAB_PROJECT_HOOKS, gitHost, projectId), 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) {
        return response.getBody();
    } else {
        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