Search in sources :

Example 11 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.

the class RepositoryManagementServiceInternalImpl method pushToRemote.

@Override
public boolean pushToRemote(String siteId, String remoteName, String remoteBranch, boolean force) throws CryptoException, ServiceLayerException, InvalidRemoteUrlException {
    logger.debug("Get remote data from database for remote " + remoteName + " and site " + siteId);
    RemoteRepository remoteRepository = getRemoteRepository(siteId, remoteName);
    logger.debug("Prepare push command.");
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(siteId, SANDBOX);
    try (Git git = new Git(repo)) {
        Iterable<PushResult> pushResultIterable = null;
        PushCommand pushCommand = git.push();
        logger.debug("Set remote " + remoteName);
        pushCommand.setRemote(remoteRepository.getRemoteName());
        logger.debug("Set branch to be " + remoteBranch);
        RefSpec r = new RefSpec();
        r = r.setSourceDestination(Constants.R_HEADS + repo.getBranch(), Constants.R_HEADS + remoteBranch);
        pushCommand.setRefSpecs(r);
        Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
        pushCommand = helper.setAuthenticationForCommand(pushCommand, remoteRepository.getAuthenticationType(), remoteRepository.getRemoteUsername(), remoteRepository.getRemotePassword(), remoteRepository.getRemoteToken(), remoteRepository.getRemotePrivateKey(), tempKey, true);
        pushCommand.setForce(force);
        pushResultIterable = retryingRepositoryOperationFacade.call(pushCommand);
        Files.delete(tempKey);
        boolean toRet = true;
        for (PushResult pushResult : pushResultIterable) {
            String pushResultMessage = pushResult.getMessages();
            if (StringUtils.isNotEmpty(pushResultMessage)) {
                logger.info(pushResultMessage);
            }
            Collection<RemoteRefUpdate> updates = pushResult.getRemoteUpdates();
            for (RemoteRefUpdate remoteRefUpdate : updates) {
                switch(remoteRefUpdate.getStatus()) {
                    case REJECTED_NODELETE:
                        toRet = false;
                        logger.error("Remote ref " + remoteRefUpdate.getSrcRef() + " update was rejected, " + "because remote side doesn't support/allow deleting refs.\n" + remoteRefUpdate.getMessage());
                    case REJECTED_NONFASTFORWARD:
                        toRet = false;
                        logger.error("Remote ref " + remoteRefUpdate.getSrcRef() + " update was rejected, as it " + "would cause non fast-forward update.\n" + remoteRefUpdate.getMessage());
                    case REJECTED_REMOTE_CHANGED:
                        toRet = false;
                        logger.error("Remote ref " + remoteRefUpdate.getSrcRef() + " update was rejected, because" + " old object id on remote repository " + remoteRefUpdate.getRemoteName() + " wasn't the same as defined expected old object. \n" + remoteRefUpdate.getMessage());
                    case REJECTED_OTHER_REASON:
                        toRet = false;
                        logger.error("Remote ref " + remoteRefUpdate.getSrcRef() + " update was rejected for " + "other reason.\n" + remoteRefUpdate.getMessage());
                    default:
                        break;
                }
            }
        }
        return toRet;
    } catch (InvalidRemoteException e) {
        logger.error("Remote is invalid " + remoteName, e);
        throw new InvalidRemoteUrlException();
    } catch (IOException | JGitInternalException | GitAPIException | CryptoException e) {
        logger.error("Error while pushing to remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
        throw new ServiceLayerException("Error while pushing to remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
    }
}
Also used : Path(java.nio.file.Path) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) PushResult(org.eclipse.jgit.transport.PushResult) IOException(java.io.IOException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) PushCommand(org.eclipse.jgit.api.PushCommand) 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) RefSpec(org.eclipse.jgit.transport.RefSpec) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 12 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository 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)

Example 13 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.

the class StudioClusterSandboxRepoSyncTask method addRemoteRepository.

protected void addRemoteRepository(String siteId, RemoteRepository remoteRepository) throws IOException, InvalidRemoteUrlException, ServiceLayerException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(buildRepoPath(siteId).resolve(GIT_ROOT).toFile()).readEnvironment().findGitDir().build();
    try (Git git = new Git(repo)) {
        logger.debug("Add " + remoteRepository.getRemoteName() + " as remote to SANDBOX");
        RemoteAddCommand remoteAddCommand = git.remoteAdd();
        remoteAddCommand.setName(remoteRepository.getRemoteName());
        remoteAddCommand.setUri(new URIish(remoteRepository.getRemoteUrl()));
        remoteAddCommand.call();
    } catch (URISyntaxException e) {
        logger.error("Remote URL is invalid " + remoteRepository.getRemoteUrl(), e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException 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);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) URISyntaxException(java.net.URISyntaxException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)

Example 14 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.

the class GitContentRepository method insertRemoteToDb.

private void insertRemoteToDb(String siteId, String remoteName, String remoteUrl, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey) throws CryptoException {
    logger.debug("Inserting remote " + remoteName + " for site " + siteId + " into database.");
    Map<String, String> params = new HashMap<String, String>();
    params.put("siteId", siteId);
    params.put("remoteName", remoteName);
    params.put("remoteUrl", remoteUrl);
    params.put("authenticationType", authenticationType);
    params.put("remoteUsername", remoteUsername);
    if (StringUtils.isNotEmpty(remotePassword)) {
        logger.debug("Encrypt password before inserting to database");
        String hashedPassword = encryptor.encrypt(remotePassword);
        params.put("remotePassword", hashedPassword);
    } else {
        params.put("remotePassword", remotePassword);
    }
    if (StringUtils.isNotEmpty(remoteToken)) {
        logger.debug("Encrypt token before inserting to database");
        String hashedToken = encryptor.encrypt(remoteToken);
        params.put("remoteToken", hashedToken);
    } else {
        params.put("remoteToken", remoteToken);
    }
    if (StringUtils.isNotEmpty(remotePrivateKey)) {
        logger.debug("Encrypt private key before inserting to database");
        String hashedPrivateKey = encryptor.encrypt(remotePrivateKey);
        params.put("remotePrivateKey", hashedPrivateKey);
    } else {
        params.put("remotePrivateKey", remotePrivateKey);
    }
    logger.debug("Insert site remote record into database");
    remoteRepositoryDAO.insertRemoteRepository(params);
    params = new HashMap<String, String>();
    params.put("siteId", siteId);
    params.put("remoteName", remoteName);
    RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
    if (remoteRepository != null) {
        insertClusterRemoteRepository(remoteRepository);
    }
}
Also used : HashMap(java.util.HashMap) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository)

Example 15 with RemoteRepository

use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.

the class GitContentRepository method insertClusterRemoteRepository.

@RetryingOperation
public void insertClusterRemoteRepository(RemoteRepository remoteRepository) {
    HierarchicalConfiguration<ImmutableNode> registrationData = studioConfiguration.getSubConfig(CLUSTERING_NODE_REGISTRATION);
    if (registrationData != null && !registrationData.isEmpty()) {
        String localAddress = registrationData.getString(CLUSTER_MEMBER_LOCAL_ADDRESS);
        ClusterMember member = clusterDao.getMemberByLocalAddress(localAddress);
        if (member != null) {
            clusterDao.addClusterRemoteRepository(member.getId(), remoteRepository.getId());
        }
    }
}
Also used : ClusterMember(org.craftercms.studio.api.v2.dal.ClusterMember) ImmutableNode(org.apache.commons.configuration2.tree.ImmutableNode) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation)

Aggregations

RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)13 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)8 IOException (java.io.IOException)7 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)7 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)7 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)7 Git (org.eclipse.jgit.api.Git)7 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)7 Repository (org.eclipse.jgit.lib.Repository)7 Path (java.nio.file.Path)6 CryptoException (org.craftercms.commons.crypto.CryptoException)6 HashMap (java.util.HashMap)5 ClusterMember (org.craftercms.studio.api.v2.dal.ClusterMember)4 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)4 ArrayList (java.util.ArrayList)3 TransportConfigCallback (org.eclipse.jgit.api.TransportConfigCallback)3 SshTransport (org.eclipse.jgit.transport.SshTransport)3 Transport (org.eclipse.jgit.transport.Transport)3 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)3 URISyntaxException (java.net.URISyntaxException)2