Search in sources :

Example 1 with RemoteAlreadyExistsException

use of org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException in project studio by craftercms.

the class GitContentRepository method addRemote.

@Override
public boolean addRemote(String siteId, String remoteName, String remoteUrl, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey) throws InvalidRemoteUrlException, ServiceLayerException {
    boolean isValid = false;
    try {
        logger.debug("Add remote " + remoteName + " to the sandbox repo for the site " + siteId);
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repo = helper.getRepository(siteId, SANDBOX);
        try (Git git = new Git(repo)) {
            Config storedConfig = repo.getConfig();
            Set<String> remotes = storedConfig.getSubsections("remote");
            if (remotes.contains(remoteName)) {
                throw new RemoteAlreadyExistsException(remoteName);
            }
            RemoteAddCommand remoteAddCommand = git.remoteAdd();
            remoteAddCommand.setName(remoteName);
            remoteAddCommand.setUri(new URIish(remoteUrl));
            retryingRepositoryOperationFacade.call(remoteAddCommand);
            try {
                isValid = isRemoteValid(git, remoteName, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey);
            } finally {
                if (!isValid) {
                    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 (URISyntaxException | ClassCastException e) {
            logger.error("Remote URL is invalid " + remoteUrl, e);
            throw new InvalidRemoteUrlException("Remote URL is invalid " + remoteUrl, e);
        } catch (GitAPIException | IOException e) {
            logger.error("Error while adding remote " + remoteName + " (url: " + remoteUrl + ") for site " + siteId, e);
            throw new ServiceLayerException("Error while adding remote " + remoteName + " (url: " + remoteUrl + ") for site " + siteId, e);
        }
        if (isValid) {
            insertRemoteToDb(siteId, remoteName, remoteUrl, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey);
        }
    } catch (CryptoException e) {
        throw new ServiceLayerException(e);
    }
    return isValid;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) Config(org.eclipse.jgit.lib.Config) OpenSshConfig(org.eclipse.jgit.transport.OpenSshConfig) ServicesConfig(org.craftercms.studio.api.v1.service.configuration.ServicesConfig) RemoteRemoveCommand(org.eclipse.jgit.api.RemoteRemoveCommand) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) DeleteBranchCommand(org.eclipse.jgit.api.DeleteBranchCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) RemoteAlreadyExistsException(org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException) 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) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 2 with RemoteAlreadyExistsException

use of org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException in project studio by craftercms.

the class RepositoryManagementServiceInternalImpl method addRemote.

@Override
public boolean addRemote(String siteId, RemoteRepository remoteRepository) throws ServiceLayerException, InvalidRemoteUrlException {
    boolean isValid = false;
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
    generalLockService.lock(gitLockKey);
    try {
        logger.debug("Add remote " + remoteRepository.getRemoteName() + " to the sandbox repo for the site " + siteId);
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repo = helper.getRepository(siteId, SANDBOX);
        try (Git git = new Git(repo)) {
            Config storedConfig = repo.getConfig();
            Set<String> remotes = storedConfig.getSubsections("remote");
            if (remotes.contains(remoteRepository.getRemoteName())) {
                throw new RemoteAlreadyExistsException(remoteRepository.getRemoteName());
            }
            RemoteAddCommand remoteAddCommand = git.remoteAdd();
            remoteAddCommand.setName(remoteRepository.getRemoteName());
            remoteAddCommand.setUri(new URIish(remoteRepository.getRemoteUrl()));
            retryingRepositoryOperationFacade.call(remoteAddCommand);
            try {
                isValid = helper.isRemoteValid(git, remoteRepository.getRemoteName(), remoteRepository.getAuthenticationType(), remoteRepository.getRemoteUsername(), remoteRepository.getRemotePassword(), remoteRepository.getRemoteToken(), remoteRepository.getRemotePrivateKey());
            } finally {
                if (!isValid) {
                    RemoteRemoveCommand remoteRemoveCommand = git.remoteRemove();
                    remoteRemoveCommand.setRemoteName(remoteRepository.getRemoteName());
                    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 + remoteRepository.getRemoteName())) {
                            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 (URISyntaxException e) {
            logger.error("Remote URL is invalid " + remoteRepository.getRemoteUrl(), e);
            throw new InvalidRemoteUrlException();
        } catch (GitAPIException | IOException e) {
            logger.error("Error while adding remote " + remoteRepository.getRemoteName() + " (url: " + remoteRepository.getRemoteUrl() + ") " + "for site " + siteId, e);
            throw new ServiceLayerException("Error while adding remote " + remoteRepository.getRemoteName() + " (url: " + remoteRepository.getRemoteUrl() + ") for site " + siteId, e);
        }
        if (isValid) {
            insertRemoteToDb(siteId, remoteRepository);
        }
    } catch (CryptoException e) {
        throw new ServiceLayerException(e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return isValid;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) DeleteBranchCommand(org.eclipse.jgit.api.DeleteBranchCommand) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) Config(org.eclipse.jgit.lib.Config) RemoteRemoveCommand(org.eclipse.jgit.api.RemoteRemoveCommand) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ArrayList(java.util.ArrayList) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) RemoteAlreadyExistsException(org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException) 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) CryptoException(org.craftercms.commons.crypto.CryptoException)

Aggregations

IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 CryptoException (org.craftercms.commons.crypto.CryptoException)2 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)2 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)2 RemoteAlreadyExistsException (org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException)2 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)2 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)2 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)2 DeleteBranchCommand (org.eclipse.jgit.api.DeleteBranchCommand)2 Git (org.eclipse.jgit.api.Git)2 RemoteAddCommand (org.eclipse.jgit.api.RemoteAddCommand)2 RemoteRemoveCommand (org.eclipse.jgit.api.RemoteRemoveCommand)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 Config (org.eclipse.jgit.lib.Config)2 Ref (org.eclipse.jgit.lib.Ref)2 Repository (org.eclipse.jgit.lib.Repository)2 RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)2 URIish (org.eclipse.jgit.transport.URIish)2