Search in sources :

Example 11 with InvalidRemoteUrlException

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

the class RepositoryManagementController method addRemote.

@ResponseStatus(HttpStatus.CREATED)
@PostMapping(ADD_REMOTE)
public ResponseBody addRemote(HttpServletResponse response, @RequestBody RemoteRepository remoteRepository) throws ServiceLayerException, InvalidRemoteUrlException {
    if (!siteService.exists(remoteRepository.getSiteId())) {
        throw new SiteNotFoundException(remoteRepository.getSiteId());
    }
    boolean res = repositoryManagementService.addRemote(remoteRepository.getSiteId(), remoteRepository);
    ResponseBody responseBody = new ResponseBody();
    Result result = new Result();
    if (res) {
        result.setResponse(CREATED);
    } else {
        result.setResponse(ADD_REMOTE_INVALID);
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    }
    responseBody.setResult(result);
    return responseBody;
}
Also used : SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) Result(org.craftercms.studio.model.rest.Result) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus)

Example 12 with InvalidRemoteUrlException

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

the class RepositoryManagementController method pullFromRemote.

@PostMapping(PULL_FROM_REMOTE)
public ResponseBody pullFromRemote(@RequestBody PullFromRemoteRequest pullFromRemoteRequest) throws InvalidRemoteUrlException, ServiceLayerException, CryptoException {
    if (!siteService.exists(pullFromRemoteRequest.getSiteId())) {
        throw new SiteNotFoundException(pullFromRemoteRequest.getSiteId());
    }
    boolean res = repositoryManagementService.pullFromRemote(pullFromRemoteRequest.getSiteId(), pullFromRemoteRequest.getRemoteName(), pullFromRemoteRequest.getRemoteBranch(), pullFromRemoteRequest.getMergeStrategy());
    if (!res) {
        throw new PullFromRemoteConflictException("Pull from remote result is merge conflict.");
    }
    ResponseBody responseBody = new ResponseBody();
    Result result = new Result();
    result.setResponse(OK);
    responseBody.setResult(result);
    return responseBody;
}
Also used : SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) PullFromRemoteConflictException(org.craftercms.studio.api.v2.exception.PullFromRemoteConflictException) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) Result(org.craftercms.studio.model.rest.Result) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 13 with InvalidRemoteUrlException

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

the class RepositoryManagementController method pushToRemote.

@PostMapping(PUSH_TO_REMOTE)
public ResponseBody pushToRemote(HttpServletResponse response, @RequestBody PushToRemoteRequest pushToRemoteRequest) throws InvalidRemoteUrlException, CryptoException, ServiceLayerException {
    if (!siteService.exists(pushToRemoteRequest.getSiteId())) {
        throw new SiteNotFoundException(pushToRemoteRequest.getSiteId());
    }
    boolean res = repositoryManagementService.pushToRemote(pushToRemoteRequest.getSiteId(), pushToRemoteRequest.getRemoteName(), pushToRemoteRequest.getRemoteBranch(), pushToRemoteRequest.isForce());
    ResponseBody responseBody = new ResponseBody();
    Result result = new Result();
    if (res) {
        result.setResponse(OK);
    } else {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setResponse(PUSH_TO_REMOTE_FAILED);
    }
    responseBody.setResult(result);
    return responseBody;
}
Also used : SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) Result(org.craftercms.studio.model.rest.Result) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 14 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException 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 15 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException 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

ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)15 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)15 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)12 Git (org.eclipse.jgit.api.Git)12 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)12 Repository (org.eclipse.jgit.lib.Repository)12 IOException (java.io.IOException)11 CryptoException (org.craftercms.commons.crypto.CryptoException)10 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)10 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)9 URISyntaxException (java.net.URISyntaxException)8 RemoteAddCommand (org.eclipse.jgit.api.RemoteAddCommand)8 URIish (org.eclipse.jgit.transport.URIish)8 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)7 Config (org.eclipse.jgit.lib.Config)7 Path (java.nio.file.Path)6 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)6 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)5 HashMap (java.util.HashMap)4 PullCommand (org.eclipse.jgit.api.PullCommand)4