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;
}
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;
}
Aggregations