Search in sources :

Example 1 with StudioConfiguration

use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.

the class RepositoryManagementServiceInternalImpl method listRemotes.

@Override
public List<RemoteRepositoryInfo> listRemotes(String siteId, String sandboxBranch) throws ServiceLayerException, CryptoException {
    List<RemoteRepositoryInfo> res = new ArrayList<RemoteRepositoryInfo>();
    Map<String, String> unreachableRemotes = new HashMap<String, String>();
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    try (Repository repo = helper.getRepository(siteId, SANDBOX)) {
        try (Git git = new Git(repo)) {
            List<RemoteConfig> resultRemotes = git.remoteList().call();
            if (CollectionUtils.isNotEmpty(resultRemotes)) {
                for (RemoteConfig conf : resultRemotes) {
                    try {
                        fetchRemote(siteId, git, conf);
                    } catch (Exception e) {
                        logger.warn("Failed to fetch from remote repository " + conf.getName());
                        unreachableRemotes.put(conf.getName(), e.getMessage());
                    }
                }
                Map<String, List<String>> remoteBranches = getRemoteBranches(git);
                String sandboxBranchName = sandboxBranch;
                if (StringUtils.isEmpty(sandboxBranchName)) {
                    sandboxBranchName = studioConfiguration.getProperty(REPO_SANDBOX_BRANCH);
                }
                res = getRemoteRepositoryInfo(resultRemotes, remoteBranches, unreachableRemotes, sandboxBranchName);
            }
        } catch (GitAPIException e) {
            logger.error("Error getting remote repositories for site " + siteId, e);
        }
    }
    return res;
}
Also used : RemoteRepositoryInfo(org.craftercms.studio.api.v2.dal.RemoteRepositoryInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) RemoteNotRemovableException(org.craftercms.studio.api.v1.exception.repository.RemoteNotRemovableException) URISyntaxException(java.net.URISyntaxException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) RemoteAlreadyExistsException(org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException) CryptoException(org.craftercms.commons.crypto.CryptoException) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Example 2 with StudioConfiguration

use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.

the class RepositoryManagementServiceInternalImpl method removeRemote.

@RetryingOperation
@Override
public boolean removeRemote(String siteId, String remoteName) throws CryptoException, RemoteNotRemovableException {
    if (!isRemovableRemote(siteId, remoteName)) {
        throw new RemoteNotRemovableException("Remote repository " + remoteName + " is not removable");
    }
    logger.debug("Remove remote " + remoteName + " from the sandbox repo for the site " + siteId);
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(siteId, SANDBOX);
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
    generalLockService.lock(gitLockKey);
    try (Git git = new Git(repo)) {
        RemoteRemoveCommand remoteRemoveCommand = git.remoteRemove();
        remoteRemoveCommand.setRemoteName(remoteName);
        retryingRepositoryOperationFacade.call(remoteRemoveCommand);
        List<Ref> resultRemoteBranches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call();
        List<String> branchesToDelete = new ArrayList<String>();
        for (Ref remoteBranchRef : resultRemoteBranches) {
            if (remoteBranchRef.getName().startsWith(Constants.R_REMOTES + remoteName)) {
                branchesToDelete.add(remoteBranchRef.getName());
            }
        }
        if (CollectionUtils.isNotEmpty(branchesToDelete)) {
            DeleteBranchCommand delBranch = git.branchDelete();
            String[] array = new String[branchesToDelete.size()];
            delBranch.setBranchNames(branchesToDelete.toArray(array));
            delBranch.setForce(true);
            retryingRepositoryOperationFacade.call(delBranch);
        }
    } catch (GitAPIException e) {
        logger.error("Failed to remove remote " + remoteName + " for site " + siteId, e);
        return false;
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    logger.debug("Remove remote record from database for remote " + remoteName + " and site " + siteId);
    Map<String, String> params = new HashMap<String, String>();
    params.put("siteId", siteId);
    params.put("remoteName", remoteName);
    remoteRepositoryDao.deleteRemoteRepository(params);
    return true;
}
Also used : RemoteNotRemovableException(org.craftercms.studio.api.v1.exception.repository.RemoteNotRemovableException) DeleteBranchCommand(org.eclipse.jgit.api.DeleteBranchCommand) HashMap(java.util.HashMap) RemoteRemoveCommand(org.eclipse.jgit.api.RemoteRemoveCommand) ArrayList(java.util.ArrayList) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation)

Example 3 with StudioConfiguration

use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.

the class RepositoryManagementServiceInternalImpl method fetchRemote.

private void fetchRemote(String siteId, Git git, RemoteConfig conf) throws CryptoException, IOException, ServiceLayerException, GitAPIException {
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    RemoteRepository remoteRepository = getRemoteRepository(siteId, conf.getName());
    if (remoteRepository != null) {
        Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
        FetchCommand fetchCommand = git.fetch().setRemote(conf.getName());
        fetchCommand = helper.setAuthenticationForCommand(fetchCommand, remoteRepository.getAuthenticationType(), remoteRepository.getRemoteUsername(), remoteRepository.getRemotePassword(), remoteRepository.getRemoteToken(), remoteRepository.getRemotePrivateKey(), tempKey, true);
        retryingRepositoryOperationFacade.call(fetchCommand);
    }
}
Also used : Path(java.nio.file.Path) FetchCommand(org.eclipse.jgit.api.FetchCommand) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper)

Example 4 with StudioConfiguration

use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.

the class RepositoryManagementServiceInternalImpl method pullFromRemote.

@Override
public boolean pullFromRemote(String siteId, String remoteName, String remoteBranch, String mergeStrategy) throws InvalidRemoteUrlException, ServiceLayerException, CryptoException {
    logger.debug("Get remote data from database for remote " + remoteName + " and site " + siteId);
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
    RemoteRepository remoteRepository = getRemoteRepository(siteId, remoteName);
    logger.debug("Prepare pull command");
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(siteId, SANDBOX);
    generalLockService.lock(gitLockKey);
    try (Git git = new Git(repo)) {
        PullResult pullResult = null;
        PullCommand pullCommand = git.pull();
        logger.debug("Set remote " + remoteName);
        pullCommand.setRemote(remoteRepository.getRemoteName());
        logger.debug("Set branch to be " + remoteBranch);
        pullCommand.setRemoteBranchName(remoteBranch);
        Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
        pullCommand = helper.setAuthenticationForCommand(pullCommand, remoteRepository.getAuthenticationType(), remoteRepository.getRemoteUsername(), remoteRepository.getRemotePassword(), remoteRepository.getRemoteToken(), remoteRepository.getRemotePrivateKey(), tempKey, true);
        switch(mergeStrategy) {
            case THEIRS:
                pullCommand.setStrategy(MergeStrategy.THEIRS);
                break;
            case OURS:
                pullCommand.setStrategy(MergeStrategy.OURS);
                break;
            default:
                break;
        }
        pullCommand.setFastForward(MergeCommand.FastForwardMode.NO_FF);
        pullResult = retryingRepositoryOperationFacade.call(pullCommand);
        String pullResultMessage = pullResult.toString();
        if (StringUtils.isNotEmpty(pullResultMessage)) {
            logger.info(pullResultMessage);
        }
        Files.delete(tempKey);
        if (!pullResult.isSuccessful() && conflictNotificationEnabled()) {
            List<String> conflictFiles = new ArrayList<String>();
            if (pullResult.getMergeResult() != null) {
                pullResult.getMergeResult().getConflicts().forEach((m, v) -> {
                    conflictFiles.add(m);
                });
            }
            notificationService.notifyRepositoryMergeConflict(siteId, conflictFiles, Locale.ENGLISH);
        }
        if (pullResult.isSuccessful()) {
            String lastCommitId = contentRepository.getRepoLastCommitId(siteId);
            contentRepositoryV2.upsertGitLogList(siteId, Arrays.asList(lastCommitId), false, false);
            List<String> newMergedCommits = extractCommitIdsFromPullResult(siteId, repo, pullResult);
            List<String> commitIds = new ArrayList<String>();
            if (Objects.nonNull(newMergedCommits) && newMergedCommits.size() > 0) {
                logger.debug("Really pulled commits:");
                int cnt = 0;
                for (int i = 0; i < newMergedCommits.size(); i++) {
                    String commitId = newMergedCommits.get(i);
                    logger.debug(commitId);
                    if (!StringUtils.equals(lastCommitId, commitId)) {
                        commitIds.add(commitId);
                        if (cnt++ >= batchSizeGitLog) {
                            contentRepositoryV2.upsertGitLogList(siteId, commitIds, true, true);
                            cnt = 0;
                            commitIds.clear();
                        }
                    }
                }
                if (Objects.nonNull(commitIds) && commitIds.size() > 0) {
                    contentRepositoryV2.upsertGitLogList(siteId, commitIds, true, true);
                }
            }
            siteService.updateLastCommitId(siteId, lastCommitId);
        }
        return pullResult != null && pullResult.isSuccessful();
    } catch (InvalidRemoteException e) {
        logger.error("Remote is invalid " + remoteName, e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException e) {
        logger.error("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
        throw new ServiceLayerException("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
    } catch (CryptoException | IOException e) {
        throw new ServiceLayerException(e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
}
Also used : Path(java.nio.file.Path) PullCommand(org.eclipse.jgit.api.PullCommand) ArrayList(java.util.ArrayList) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) IOException(java.io.IOException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) PullResult(org.eclipse.jgit.api.PullResult) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 5 with StudioConfiguration

use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.

the class RepositoryManagementServiceInternalImpl method unlockRepository.

@Override
public boolean unlockRepository(String siteId, GitRepositories repositoryType) throws CryptoException {
    boolean toRet = false;
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(siteId, repositoryType);
    if (Objects.nonNull(repo)) {
        toRet = FileUtils.deleteQuietly(Paths.get(repo.getDirectory().getAbsolutePath(), LOCK_FILE).toFile());
    }
    return toRet;
}
Also used : RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper)

Aggregations

GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)65 Repository (org.eclipse.jgit.lib.Repository)59 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)58 CryptoException (org.craftercms.commons.crypto.CryptoException)53 IOException (java.io.IOException)47 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)43 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)42 Git (org.eclipse.jgit.api.Git)41 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)34 ArrayList (java.util.ArrayList)22 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)21 Path (java.nio.file.Path)20 ObjectId (org.eclipse.jgit.lib.ObjectId)19 RevCommit (org.eclipse.jgit.revwalk.RevCommit)17 RevTree (org.eclipse.jgit.revwalk.RevTree)17 ContentRepository (org.craftercms.studio.api.v2.repository.ContentRepository)15 File (java.io.File)12 HashMap (java.util.HashMap)12 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)11 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)10