use of org.craftercms.studio.api.v2.dal.RemoteRepository 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.v2.dal.RemoteRepository 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;
}
use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.
the class StudioClusterSandboxRepoSyncTask method addRemoteRepository.
protected void addRemoteRepository(String siteId, RemoteRepository remoteRepository) throws IOException, InvalidRemoteUrlException, ServiceLayerException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repo = builder.setGitDir(buildRepoPath(siteId).resolve(GIT_ROOT).toFile()).readEnvironment().findGitDir().build();
try (Git git = new Git(repo)) {
logger.debug("Add " + remoteRepository.getRemoteName() + " as remote to SANDBOX");
RemoteAddCommand remoteAddCommand = git.remoteAdd();
remoteAddCommand.setName(remoteRepository.getRemoteName());
remoteAddCommand.setUri(new URIish(remoteRepository.getRemoteUrl()));
remoteAddCommand.call();
} catch (URISyntaxException e) {
logger.error("Remote URL is invalid " + remoteRepository.getRemoteUrl(), e);
throw new InvalidRemoteUrlException();
} catch (GitAPIException 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);
}
}
use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.
the class GitContentRepository method insertRemoteToDb.
private void insertRemoteToDb(String siteId, String remoteName, String remoteUrl, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey) throws CryptoException {
logger.debug("Inserting remote " + remoteName + " for site " + siteId + " into database.");
Map<String, String> params = new HashMap<String, String>();
params.put("siteId", siteId);
params.put("remoteName", remoteName);
params.put("remoteUrl", remoteUrl);
params.put("authenticationType", authenticationType);
params.put("remoteUsername", remoteUsername);
if (StringUtils.isNotEmpty(remotePassword)) {
logger.debug("Encrypt password before inserting to database");
String hashedPassword = encryptor.encrypt(remotePassword);
params.put("remotePassword", hashedPassword);
} else {
params.put("remotePassword", remotePassword);
}
if (StringUtils.isNotEmpty(remoteToken)) {
logger.debug("Encrypt token before inserting to database");
String hashedToken = encryptor.encrypt(remoteToken);
params.put("remoteToken", hashedToken);
} else {
params.put("remoteToken", remoteToken);
}
if (StringUtils.isNotEmpty(remotePrivateKey)) {
logger.debug("Encrypt private key before inserting to database");
String hashedPrivateKey = encryptor.encrypt(remotePrivateKey);
params.put("remotePrivateKey", hashedPrivateKey);
} else {
params.put("remotePrivateKey", remotePrivateKey);
}
logger.debug("Insert site remote record into database");
remoteRepositoryDAO.insertRemoteRepository(params);
params = new HashMap<String, String>();
params.put("siteId", siteId);
params.put("remoteName", remoteName);
RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
if (remoteRepository != null) {
insertClusterRemoteRepository(remoteRepository);
}
}
use of org.craftercms.studio.api.v2.dal.RemoteRepository in project studio by craftercms.
the class GitContentRepository method insertClusterRemoteRepository.
@RetryingOperation
public void insertClusterRemoteRepository(RemoteRepository remoteRepository) {
HierarchicalConfiguration<ImmutableNode> registrationData = studioConfiguration.getSubConfig(CLUSTERING_NODE_REGISTRATION);
if (registrationData != null && !registrationData.isEmpty()) {
String localAddress = registrationData.getString(CLUSTER_MEMBER_LOCAL_ADDRESS);
ClusterMember member = clusterDao.getMemberByLocalAddress(localAddress);
if (member != null) {
clusterDao.addClusterRemoteRepository(member.getId(), remoteRepository.getId());
}
}
}
Aggregations