Search in sources :

Example 1 with GitPushCommitEntry

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

the class GitManager method removeFolder.

public GitCommitEntry removeFolder(Pipeline pipeline, String folder, String lastCommitId, String commitMessage) throws GitClientException {
    if (commitMessage == null) {
        commitMessage = String.format("Removing update %s", folder);
    }
    Assert.isTrue(lastCommitId.equals(pipeline.getCurrentVersion().getCommitId()), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_FILE_WAS_UPDATED, folder));
    Assert.isTrue(!StringUtils.isNullOrEmpty(folder), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_ROOT_FOLDER_CANNOT_BE_REMOVED));
    Assert.isTrue(!folder.equalsIgnoreCase(srcDirectory), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_FOLDER_CANNOT_BE_REMOVED, folder));
    GitlabClient gitlabClient = this.getGitlabClientForPipeline(pipeline);
    List<GitRepositoryEntry> allFiles = gitlabClient.getRepositoryContents(folder, GIT_MASTER_REPOSITORY, true);
    GitPushCommitEntry gitPushCommitEntry = new GitPushCommitEntry();
    gitPushCommitEntry.setCommitMessage(commitMessage);
    for (GitRepositoryEntry file : allFiles) {
        if (file.getType().equalsIgnoreCase("tree")) {
            continue;
        }
        GitPushCommitActionEntry gitPushCommitActionEntry = new GitPushCommitActionEntry();
        gitPushCommitActionEntry.setAction("delete");
        gitPushCommitActionEntry.setFilePath(file.getPath());
        gitPushCommitEntry.getActions().add(gitPushCommitActionEntry);
    }
    return gitlabClient.commit(gitPushCommitEntry);
}
Also used : GitPushCommitActionEntry(com.epam.pipeline.entity.git.GitPushCommitActionEntry) GitPushCommitEntry(com.epam.pipeline.entity.git.GitPushCommitEntry) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry)

Example 2 with GitPushCommitEntry

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

the class GitManager method uploadFiles.

public GitCommitEntry uploadFiles(Pipeline pipeline, String folder, List<UploadFileMetadata> files, String lastCommitId, String commitMessage) throws GitClientException {
    Assert.isTrue(lastCommitId.equals(pipeline.getCurrentVersion().getCommitId()), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_WAS_UPDATED));
    GitlabClient gitlabClient = this.getGitlabClientForPipeline(pipeline);
    GitPushCommitEntry gitPushCommitEntry = new GitPushCommitEntry();
    for (UploadFileMetadata file : files) {
        String filePath = folder + PATH_DELIMITER + file.getFileName();
        Arrays.stream(filePath.split(PATH_DELIMITER)).forEach(pathPart -> Assert.isTrue(GitUtils.checkGitNaming(pathPart), messageHelper.getMessage(MessageConstants.ERROR_INVALID_PIPELINE_FILE_NAME, filePath)));
        boolean fileExists = false;
        try {
            fileExists = gitlabClient.getFileContents(filePath, GIT_MASTER_REPOSITORY) != null;
        } catch (HttpClientErrorException exception) {
            LOGGER.debug(exception.getMessage(), exception);
        }
        GitPushCommitActionEntry gitPushCommitActionEntry = new GitPushCommitActionEntry();
        commitMessage = getCommitMessage(commitMessage, filePath, fileExists, gitPushCommitActionEntry);
        gitPushCommitActionEntry.setFilePath(filePath);
        gitPushCommitActionEntry.setContent(Base64.getEncoder().encodeToString(file.getBytes()));
        gitPushCommitActionEntry.setEncoding(BASE64_ENCODING);
        gitPushCommitEntry.getActions().add(gitPushCommitActionEntry);
    }
    gitPushCommitEntry.setCommitMessage(commitMessage);
    return gitlabClient.commit(gitPushCommitEntry);
}
Also used : GitPushCommitActionEntry(com.epam.pipeline.entity.git.GitPushCommitActionEntry) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) UploadFileMetadata(com.epam.pipeline.controller.vo.UploadFileMetadata) GitPushCommitEntry(com.epam.pipeline.entity.git.GitPushCommitEntry)

Example 3 with GitPushCommitEntry

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

the class GitManager method renameFile.

private GitCommitEntry renameFile(Pipeline pipeline, String filePath, String filePreviousPath, String lastCommitId, String commitMessage) throws GitClientException {
    Assert.isTrue(lastCommitId.equals(pipeline.getCurrentVersion().getCommitId()), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_FILE_WAS_UPDATED, filePath));
    final GitlabClient gitlabClient = getGitlabClientForPipeline(pipeline);
    if (StringUtils.isNullOrEmpty(commitMessage)) {
        commitMessage = String.format("Renaming '%s' to '%s", filePreviousPath, filePath);
    }
    final GitPushCommitEntry gitPushCommitEntry = new GitPushCommitEntry();
    gitPushCommitEntry.setCommitMessage(commitMessage);
    prepareFileForRenaming(filePath, filePreviousPath, gitPushCommitEntry, gitlabClient);
    return gitlabClient.commit(gitPushCommitEntry);
}
Also used : GitPushCommitEntry(com.epam.pipeline.entity.git.GitPushCommitEntry)

Example 4 with GitPushCommitEntry

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

the class GitManager method renameFolder.

private GitCommitEntry renameFolder(Pipeline pipeline, String folder, String newFolderName, String lastCommitId, String commitMessage) throws GitClientException {
    if (commitMessage == null) {
        commitMessage = String.format("Renaming update %s to %s", folder, newFolderName);
    }
    Assert.isTrue(lastCommitId.equals(pipeline.getCurrentVersion().getCommitId()), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_FILE_WAS_UPDATED, folder));
    Assert.isTrue(folderExists(pipeline, folder), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_FOLDER_NOT_FOUND, folder));
    Assert.isTrue(!folderExists(pipeline, newFolderName), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_FOLDER_ALREADY_EXISTS, newFolderName));
    final GitlabClient gitlabClient = getGitlabClientForPipeline(pipeline);
    final List<GitRepositoryEntry> allFiles = gitlabClient.getRepositoryContents(folder, GIT_MASTER_REPOSITORY, true);
    final GitPushCommitEntry gitPushCommitEntry = new GitPushCommitEntry();
    gitPushCommitEntry.setCommitMessage(commitMessage);
    for (GitRepositoryEntry file : allFiles) {
        if (file.getType().equalsIgnoreCase("tree")) {
            continue;
        }
        prepareFileForRenaming(file.getPath().replaceFirst(folder, newFolderName), file.getPath(), gitPushCommitEntry, gitlabClient);
    }
    return gitlabClient.commit(gitPushCommitEntry);
}
Also used : GitPushCommitEntry(com.epam.pipeline.entity.git.GitPushCommitEntry) GitRepositoryEntry(com.epam.pipeline.entity.git.GitRepositoryEntry)

Example 5 with GitPushCommitEntry

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

the class GitManager method updateFiles.

public GitCommitEntry updateFiles(Pipeline pipeline, PipelineSourceItemsVO sourceItemVOList) throws GitClientException {
    if (CollectionUtils.isEmpty(sourceItemVOList.getItems())) {
        return null;
    }
    String lastCommitId = sourceItemVOList.getLastCommitId();
    Assert.isTrue(lastCommitId.equals(pipeline.getCurrentVersion().getCommitId()), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_WAS_UPDATED));
    GitlabClient gitlabClient = getGitlabClientForPipeline(pipeline);
    GitPushCommitEntry gitPushCommitEntry = new GitPushCommitEntry();
    if (StringUtils.isNullOrEmpty(sourceItemVOList.getComment())) {
        gitPushCommitEntry.setCommitMessage(String.format("Updating files %s", StringUtils.join(", ", String.valueOf(sourceItemVOList.getItems().stream().map(PipelineSourceItemVO::getPath)))));
    } else {
        gitPushCommitEntry.setCommitMessage(sourceItemVOList.getComment());
    }
    for (PipelineSourceItemVO sourceItemVO : sourceItemVOList.getItems()) {
        String sourcePath = sourceItemVO.getPath();
        Arrays.stream(sourcePath.split(PATH_DELIMITER)).forEach(pathPart -> Assert.isTrue(GitUtils.checkGitNaming(pathPart), messageHelper.getMessage(MessageConstants.ERROR_INVALID_PIPELINE_FILE_NAME, sourcePath)));
        String action;
        if (!StringUtils.isNullOrEmpty(sourceItemVO.getPreviousPath())) {
            action = ACTION_MOVE;
        } else {
            boolean fileExists = false;
            try {
                fileExists = gitlabClient.getFileContents(sourcePath, GIT_MASTER_REPOSITORY) != null;
            } catch (HttpClientErrorException exception) {
                LOGGER.debug(exception.getMessage(), exception);
            }
            if (fileExists) {
                action = "update";
            } else {
                action = "create";
            }
        }
        GitPushCommitActionEntry gitPushCommitActionEntry = new GitPushCommitActionEntry();
        gitPushCommitActionEntry.setFilePath(sourcePath);
        gitPushCommitActionEntry.setAction(action);
        if (StringUtils.isNullOrEmpty(sourceItemVO.getPreviousPath())) {
            gitPushCommitActionEntry.setContent(sourceItemVO.getContents());
        } else {
            gitPushCommitActionEntry.setPreviousPath(sourceItemVO.getPreviousPath());
        }
        gitPushCommitEntry.getActions().add(gitPushCommitActionEntry);
    }
    return gitlabClient.commit(gitPushCommitEntry);
}
Also used : GitPushCommitActionEntry(com.epam.pipeline.entity.git.GitPushCommitActionEntry) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) PipelineSourceItemVO(com.epam.pipeline.controller.vo.PipelineSourceItemVO) GitPushCommitEntry(com.epam.pipeline.entity.git.GitPushCommitEntry)

Aggregations

GitPushCommitEntry (com.epam.pipeline.entity.git.GitPushCommitEntry)8 GitPushCommitActionEntry (com.epam.pipeline.entity.git.GitPushCommitActionEntry)6 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)3 GitRepositoryEntry (com.epam.pipeline.entity.git.GitRepositoryEntry)2 PipelineSourceItemVO (com.epam.pipeline.controller.vo.PipelineSourceItemVO)1 UploadFileMetadata (com.epam.pipeline.controller.vo.UploadFileMetadata)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1