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