Search in sources :

Example 6 with GitCommitEntry

use of com.epam.pipeline.entity.git.GitCommitEntry in project cloud-pipeline by epam.

the class GitManagerTest method shouldModifyFile.

@Test
@Ignore
public void shouldModifyFile() throws GitClientException {
    final GitCommitEntry expectedCommit = new GitCommitEntry();
    givenThat(post(urlPathEqualTo(api(REPOSITORY_COMMITS))).willReturn(okJson(with(expectedCommit))));
    final Pipeline pipeline = testingPipeline();
    final String lastCommit = pipeline.getCurrentVersion().getCommitId();
    final PipelineSourceItemVO file = new PipelineSourceItemVO();
    file.setLastCommitId(lastCommit);
    file.setContents(FILE_CONTENT);
    file.setComment("Update some file");
    file.setPath(DOCS + "/" + README_FILE);
    file.setPreviousPath(DOCS + "/" + README_FILE);
    final GitCommitEntry resultingCommit = gitManager.modifyFile(pipeline, file);
    assertThat(resultingCommit, is(expectedCommit));
}
Also used : PipelineSourceItemVO(com.epam.pipeline.controller.vo.PipelineSourceItemVO) IsEmptyString.isEmptyString(org.hamcrest.text.IsEmptyString.isEmptyString) Matchers.anyString(org.mockito.Matchers.anyString) GitCommitEntry(com.epam.pipeline.entity.git.GitCommitEntry) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) Ignore(org.junit.Ignore) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test)

Example 7 with GitCommitEntry

use of com.epam.pipeline.entity.git.GitCommitEntry in project cloud-pipeline by epam.

the class GitManagerTest method shouldRenameFile.

@Test
public void shouldRenameFile() throws GitClientException, UnsupportedEncodingException {
    final Pipeline pipeline = testingPipeline();
    final UploadFileMetadata file = new UploadFileMetadata();
    final byte[] readmeContent = "Some inconsiderable content".getBytes("UTF-8");
    file.setFileName(README_FILE);
    file.setFileSize(readmeContent.length / 1024 + " Kb");
    file.setFileType("text/markdown; charset=UTF-8");
    file.setBytes(readmeContent);
    final GitCommitEntry expectedCommit = new GitCommitEntry();
    expectedCommit.setMessage("Rename the file");
    expectedCommit.setCreatedAt("2017-07-25T13:13:11Z");
    givenThat(post(urlPathEqualTo(api(REPOSITORY_COMMITS))).willReturn(okJson(with(expectedCommit))));
    final GitFile gitFile = new GitFile();
    gitFile.setContent(Base64.getEncoder().encodeToString(FILE_CONTENT.getBytes()));
    givenThat(get(urlPathEqualTo(api(REPOSITORY_FILES))).withQueryParam(FILE_PATH, equalTo(DOCS + "/" + README_FILE)).withQueryParam(REF, equalTo(GIT_MASTER_REPOSITORY)).willReturn(okJson(with(gitFile))));
    final GitCommitEntry resultingCommit = gitManager.uploadFiles(pipeline, DOCS, singletonList(file), pipeline.getCurrentVersion().getCommitId(), "Rename the file");
    assertThat(resultingCommit, is(expectedCommit));
}
Also used : UploadFileMetadata(com.epam.pipeline.controller.vo.UploadFileMetadata) GitFile(com.epam.pipeline.entity.git.GitFile) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) GitCommitEntry(com.epam.pipeline.entity.git.GitCommitEntry) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test)

Example 8 with GitCommitEntry

use of com.epam.pipeline.entity.git.GitCommitEntry in project cloud-pipeline by epam.

the class GitlabClient method commit.

public GitCommitEntry commit(GitPushCommitEntry commitEntry) throws GitClientException {
    try {
        String projectId = makeProjectId(namespace, projectName);
        String url = addUrlParameters(String.format(GIT_COMMITS, gitHost, projectId), null);
        URI uri = new URI(url);
        LOGGER.trace("Performing commit; URL: {}", uri);
        HttpHeaders headers = new HttpHeaders();
        headers.add(TOKEN_HEADER, token);
        HttpEntity entity = new HttpEntity<>(commitEntry, headers);
        ResponseEntity<GitCommitEntry> response = new RestTemplate().exchange(uri, HttpMethod.POST, entity, new ParameterizedTypeReference<GitCommitEntry>() {
        });
        if (response.getStatusCode() == HttpStatus.OK || response.getStatusCode() == HttpStatus.CREATED) {
            return response.getBody();
        } else {
            throw new UnexpectedResponseStatusException(response.getStatusCode());
        }
    } catch (HttpClientErrorException e) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            PipelineSourceItemErrorVO error = mapper.readValue(e.getResponseBodyAsByteArray(), PipelineSourceItemErrorVO.class);
            throw new GitClientException(error.getMessage());
        } catch (IOException e1) {
            throw new GitClientException(e.getMessage(), e);
        }
    } catch (UnsupportedEncodingException | URISyntaxException e) {
        throw new GitClientException(e.getMessage(), e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) UnexpectedResponseStatusException(com.epam.pipeline.exception.git.UnexpectedResponseStatusException) GitClientException(com.epam.pipeline.exception.git.GitClientException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) RestTemplate(org.springframework.web.client.RestTemplate) PipelineSourceItemErrorVO(com.epam.pipeline.controller.vo.PipelineSourceItemErrorVO) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GitCommitEntry(com.epam.pipeline.entity.git.GitCommitEntry)

Example 9 with GitCommitEntry

use of com.epam.pipeline.entity.git.GitCommitEntry in project cloud-pipeline by epam.

the class GitlabClient method getRepositoryCommit.

public GitCommitEntry getRepositoryCommit(String commitId) throws GitClientException {
    try {
        String projectId = makeProjectId(namespace, projectName);
        String url = addUrlParameters(String.format(GIT_GET_COMMIT, gitHost, projectId, commitId), new HashMap<>());
        URI uri = new URI(url);
        LOGGER.trace("Getting repository commit {} from URL: {}", commitId, uri);
        RestTemplate template = new RestTemplate();
        ResponseEntity<GitCommitEntry> sourcesResponse = template.exchange(uri, HttpMethod.GET, getAuthHeaders(), new ParameterizedTypeReference<GitCommitEntry>() {
        });
        if (sourcesResponse.getStatusCode() == HttpStatus.OK) {
            return sourcesResponse.getBody();
        } else {
            throw new UnexpectedResponseStatusException(sourcesResponse.getStatusCode());
        }
    } catch (UnsupportedEncodingException | URISyntaxException | HttpClientErrorException e) {
        throw new GitClientException(e.getMessage(), e);
    }
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) 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) GitCommitEntry(com.epam.pipeline.entity.git.GitCommitEntry)

Example 10 with GitCommitEntry

use of com.epam.pipeline.entity.git.GitCommitEntry in project cloud-pipeline by epam.

the class GitManagerTest method shouldFetchConfigFileContent.

@Test
public void shouldFetchConfigFileContent() throws GitClientException {
    final GitCommitEntry gitCommitEntry = new GitCommitEntry();
    givenThat(post(urlPathEqualTo(api(REPOSITORY_COMMITS))).willReturn(okJson(with(gitCommitEntry))));
    final GitTagEntry tag = new GitTagEntry();
    tag.setName(TEST_REVISION);
    givenThat(get(urlPathEqualTo(api(REPOSITORY_TAGS + "/" + tag.getName()))).willReturn(okJson(with(tag))));
    final GitFile gitFile = new GitFile();
    gitFile.setContent(Base64.getEncoder().encodeToString(FILE_CONTENT.getBytes()));
    givenThat(get(urlPathEqualTo(api(REPOSITORY_FILES))).withQueryParam(FILE_PATH, equalTo("config.json")).withQueryParam(REF, equalTo(tag.getName())).willReturn(okJson(with(gitFile))));
    final Pipeline pipeline = testingPipeline();
    final String fileContent = gitManager.getConfigFileContent(pipeline, pipeline.getCurrentVersion().getName());
    assertThat(fileContent, not(isEmptyString()));
}
Also used : GitFile(com.epam.pipeline.entity.git.GitFile) GitTagEntry(com.epam.pipeline.entity.git.GitTagEntry) IsEmptyString.isEmptyString(org.hamcrest.text.IsEmptyString.isEmptyString) Matchers.anyString(org.mockito.Matchers.anyString) GitCommitEntry(com.epam.pipeline.entity.git.GitCommitEntry) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test)

Aggregations

GitCommitEntry (com.epam.pipeline.entity.git.GitCommitEntry)15 Pipeline (com.epam.pipeline.entity.pipeline.Pipeline)13 AbstractManagerTest (com.epam.pipeline.manager.AbstractManagerTest)13 Test (org.junit.Test)13 GitFile (com.epam.pipeline.entity.git.GitFile)6 IsEmptyString.isEmptyString (org.hamcrest.text.IsEmptyString.isEmptyString)5 Matchers.anyString (org.mockito.Matchers.anyString)5 PipelineSourceItemVO (com.epam.pipeline.controller.vo.PipelineSourceItemVO)4 GitTagEntry (com.epam.pipeline.entity.git.GitTagEntry)4 Revision (com.epam.pipeline.entity.pipeline.Revision)4 GitRepositoryEntry (com.epam.pipeline.entity.git.GitRepositoryEntry)3 GitClientException (com.epam.pipeline.exception.git.GitClientException)2 UnexpectedResponseStatusException (com.epam.pipeline.exception.git.UnexpectedResponseStatusException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Date (java.util.Date)2 Ignore (org.junit.Ignore)2 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)2 RestTemplate (org.springframework.web.client.RestTemplate)2