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