Search in sources :

Example 1 with RemoteNotRemovableException

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

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

the class RepositoryManagementController method removeRemote.

@PostMapping(REMOVE_REMOTE)
public ResponseBody removeRemote(HttpServletResponse response, @RequestBody RemoveRemoteRequest removeRemoteRequest) throws CryptoException, SiteNotFoundException, RemoteNotRemovableException {
    if (!siteService.exists(removeRemoteRequest.getSiteId())) {
        throw new SiteNotFoundException(removeRemoteRequest.getSiteId());
    }
    boolean res = repositoryManagementService.removeRemote(removeRemoteRequest.getSiteId(), removeRemoteRequest.getRemoteName());
    ResponseBody responseBody = new ResponseBody();
    Result result = new Result();
    if (res) {
        result.setResponse(OK);
    } else {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setResponse(REMOVE_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)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)1 RemoteNotRemovableException (org.craftercms.studio.api.v1.exception.repository.RemoteNotRemovableException)1 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)1 RetryingOperation (org.craftercms.studio.api.v2.annotation.RetryingOperation)1 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)1 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)1 ResponseBody (org.craftercms.studio.model.rest.ResponseBody)1 Result (org.craftercms.studio.model.rest.Result)1 DeleteBranchCommand (org.eclipse.jgit.api.DeleteBranchCommand)1 Git (org.eclipse.jgit.api.Git)1 RemoteRemoveCommand (org.eclipse.jgit.api.RemoteRemoveCommand)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 Ref (org.eclipse.jgit.lib.Ref)1 Repository (org.eclipse.jgit.lib.Repository)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1