Search in sources :

Example 1 with GitFile

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

the class GitManagerTest method shouldCreateFolder.

@Test
public void shouldCreateFolder() throws GitClientException {
    final Pipeline pipeline = testingPipeline();
    final PipelineSourceItemVO folder = new PipelineSourceItemVO();
    folder.setPath(DOCS);
    folder.setLastCommitId(pipeline.getCurrentVersion().getCommitId());
    final GitRepositoryEntry bla = new GitRepositoryEntry();
    bla.setName(README_FILE);
    bla.setType(BLOB_TYPE);
    bla.setPath(DOCS + "/" + README_FILE);
    final List<GitRepositoryEntry> tree = singletonList(bla);
    givenThat(get(urlPathEqualTo(api(REPOSITORY_TREE))).withQueryParam(REF_NAME, equalTo(GIT_MASTER_REPOSITORY)).withQueryParam(PATH, equalTo(DOCS)).willReturn(okJson(with(tree))));
    final GitFile file = new GitFile();
    file.setContent(Base64.getEncoder().encodeToString(FILE_CONTENT.getBytes()));
    givenThat(get(urlPathEqualTo(api(REPOSITORY_FILES))).withQueryParam(FILE_PATH, equalTo(DOCS + File.separator + ".gitkeep")).withQueryParam(REF, equalTo(GIT_MASTER_REPOSITORY)).willReturn(okJson(with(file))));
    final GitCommitEntry expectedCommit = new GitCommitEntry();
    givenThat(post(urlPathEqualTo(api(REPOSITORY_COMMITS))).willReturn(okJson(with(expectedCommit))));
    final GitCommitEntry resultingCommit = gitManager.createOrRenameFolder(pipeline.getId(), folder);
    assertThat(resultingCommit, is(expectedCommit));
}
Also used : PipelineSourceItemVO(com.epam.pipeline.controller.vo.PipelineSourceItemVO) GitFile(com.epam.pipeline.entity.git.GitFile) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry) 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 2 with GitFile

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

the class GitManagerTest method shouldNotFailWhenCreateFile.

@Test
public void shouldNotFailWhenCreateFile() throws GitClientException {
    final GitFile gitFile = new GitFile();
    gitFile.setContent(Base64.getEncoder().encodeToString(FILE_CONTENT.getBytes()));
    givenThat(get(urlPathEqualTo(api(REPOSITORY_FILES))).withQueryParam(FILE_PATH, equalTo(DOCS + "/created_file.txt")).withQueryParam(REF, equalTo(GIT_MASTER_REPOSITORY)).willReturn(okJson(with(gitFile))));
    final GitCommitEntry expectedCommit = new GitCommitEntry();
    givenThat(post(urlPathEqualTo(api(REPOSITORY_COMMITS))).willReturn(okJson(with(expectedCommit))));
    final Pipeline pipeline = testingPipeline();
    final GitCommitEntry resultingCommit = gitManager.updateFile(pipeline, DOCS + "/created_file.txt", FILE_CONTENT, "last commit id doesn't matter", "Create file");
    assertThat(resultingCommit, is(expectedCommit));
}
Also used : GitFile(com.epam.pipeline.entity.git.GitFile) GitCommitEntry(com.epam.pipeline.entity.git.GitCommitEntry) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test)

Example 3 with GitFile

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

the class GitManagerTest method shouldFetchPipelineSourceFile.

@Test
public void shouldFetchPipelineSourceFile() throws GitClientException {
    final Pipeline pipeline = testingPipeline();
    final String fileName = "src/test.py";
    final GitFile gitFile = new GitFile();
    gitFile.setContent(Base64.getEncoder().encodeToString(FILE_CONTENT.getBytes()));
    givenThat(get(urlPathEqualTo(api(REPOSITORY_FILES))).withQueryParam(FILE_PATH, equalTo(fileName)).withQueryParam(REF, equalTo(TEST_REVISION)).willReturn(okJson(with(gitFile))));
    final byte[] fileContents = gitManager.getPipelineFileContents(pipeline, TEST_REVISION, fileName);
    final String text = new String(fileContents);
    assertTrue(StringUtils.isNotBlank(text));
}
Also used : GitFile(com.epam.pipeline.entity.git.GitFile) IsEmptyString.isEmptyString(org.hamcrest.text.IsEmptyString.isEmptyString) Matchers.anyString(org.mockito.Matchers.anyString) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test)

Example 4 with GitFile

use of com.epam.pipeline.entity.git.GitFile 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 5 with GitFile

use of com.epam.pipeline.entity.git.GitFile 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)

Aggregations

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