use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.
the class RepositoryManagementServiceInternalImpl method listRemotes.
@Override
public List<RemoteRepositoryInfo> listRemotes(String siteId, String sandboxBranch) throws ServiceLayerException, CryptoException {
List<RemoteRepositoryInfo> res = new ArrayList<RemoteRepositoryInfo>();
Map<String, String> unreachableRemotes = new HashMap<String, String>();
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
try (Repository repo = helper.getRepository(siteId, SANDBOX)) {
try (Git git = new Git(repo)) {
List<RemoteConfig> resultRemotes = git.remoteList().call();
if (CollectionUtils.isNotEmpty(resultRemotes)) {
for (RemoteConfig conf : resultRemotes) {
try {
fetchRemote(siteId, git, conf);
} catch (Exception e) {
logger.warn("Failed to fetch from remote repository " + conf.getName());
unreachableRemotes.put(conf.getName(), e.getMessage());
}
}
Map<String, List<String>> remoteBranches = getRemoteBranches(git);
String sandboxBranchName = sandboxBranch;
if (StringUtils.isEmpty(sandboxBranchName)) {
sandboxBranchName = studioConfiguration.getProperty(REPO_SANDBOX_BRANCH);
}
res = getRemoteRepositoryInfo(resultRemotes, remoteBranches, unreachableRemotes, sandboxBranchName);
}
} catch (GitAPIException e) {
logger.error("Error getting remote repositories for site " + siteId, e);
}
}
return res;
}
use of org.craftercms.studio.api.v2.utils.StudioConfiguration 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.v2.utils.StudioConfiguration in project studio by craftercms.
the class RepositoryManagementServiceInternalImpl method fetchRemote.
private void fetchRemote(String siteId, Git git, RemoteConfig conf) throws CryptoException, IOException, ServiceLayerException, GitAPIException {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
RemoteRepository remoteRepository = getRemoteRepository(siteId, conf.getName());
if (remoteRepository != null) {
Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
FetchCommand fetchCommand = git.fetch().setRemote(conf.getName());
fetchCommand = helper.setAuthenticationForCommand(fetchCommand, remoteRepository.getAuthenticationType(), remoteRepository.getRemoteUsername(), remoteRepository.getRemotePassword(), remoteRepository.getRemoteToken(), remoteRepository.getRemotePrivateKey(), tempKey, true);
retryingRepositoryOperationFacade.call(fetchCommand);
}
}
use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.
the class RepositoryManagementServiceInternalImpl method pullFromRemote.
@Override
public boolean pullFromRemote(String siteId, String remoteName, String remoteBranch, String mergeStrategy) throws InvalidRemoteUrlException, ServiceLayerException, CryptoException {
logger.debug("Get remote data from database for remote " + remoteName + " and site " + siteId);
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, siteId);
RemoteRepository remoteRepository = getRemoteRepository(siteId, remoteName);
logger.debug("Prepare pull command");
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(siteId, SANDBOX);
generalLockService.lock(gitLockKey);
try (Git git = new Git(repo)) {
PullResult pullResult = null;
PullCommand pullCommand = git.pull();
logger.debug("Set remote " + remoteName);
pullCommand.setRemote(remoteRepository.getRemoteName());
logger.debug("Set branch to be " + remoteBranch);
pullCommand.setRemoteBranchName(remoteBranch);
Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
pullCommand = helper.setAuthenticationForCommand(pullCommand, remoteRepository.getAuthenticationType(), remoteRepository.getRemoteUsername(), remoteRepository.getRemotePassword(), remoteRepository.getRemoteToken(), remoteRepository.getRemotePrivateKey(), tempKey, true);
switch(mergeStrategy) {
case THEIRS:
pullCommand.setStrategy(MergeStrategy.THEIRS);
break;
case OURS:
pullCommand.setStrategy(MergeStrategy.OURS);
break;
default:
break;
}
pullCommand.setFastForward(MergeCommand.FastForwardMode.NO_FF);
pullResult = retryingRepositoryOperationFacade.call(pullCommand);
String pullResultMessage = pullResult.toString();
if (StringUtils.isNotEmpty(pullResultMessage)) {
logger.info(pullResultMessage);
}
Files.delete(tempKey);
if (!pullResult.isSuccessful() && conflictNotificationEnabled()) {
List<String> conflictFiles = new ArrayList<String>();
if (pullResult.getMergeResult() != null) {
pullResult.getMergeResult().getConflicts().forEach((m, v) -> {
conflictFiles.add(m);
});
}
notificationService.notifyRepositoryMergeConflict(siteId, conflictFiles, Locale.ENGLISH);
}
if (pullResult.isSuccessful()) {
String lastCommitId = contentRepository.getRepoLastCommitId(siteId);
contentRepositoryV2.upsertGitLogList(siteId, Arrays.asList(lastCommitId), false, false);
List<String> newMergedCommits = extractCommitIdsFromPullResult(siteId, repo, pullResult);
List<String> commitIds = new ArrayList<String>();
if (Objects.nonNull(newMergedCommits) && newMergedCommits.size() > 0) {
logger.debug("Really pulled commits:");
int cnt = 0;
for (int i = 0; i < newMergedCommits.size(); i++) {
String commitId = newMergedCommits.get(i);
logger.debug(commitId);
if (!StringUtils.equals(lastCommitId, commitId)) {
commitIds.add(commitId);
if (cnt++ >= batchSizeGitLog) {
contentRepositoryV2.upsertGitLogList(siteId, commitIds, true, true);
cnt = 0;
commitIds.clear();
}
}
}
if (Objects.nonNull(commitIds) && commitIds.size() > 0) {
contentRepositoryV2.upsertGitLogList(siteId, commitIds, true, true);
}
}
siteService.updateLastCommitId(siteId, lastCommitId);
}
return pullResult != null && pullResult.isSuccessful();
} catch (InvalidRemoteException e) {
logger.error("Remote is invalid " + remoteName, e);
throw new InvalidRemoteUrlException();
} catch (GitAPIException e) {
logger.error("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
throw new ServiceLayerException("Error while pulling from remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
} catch (CryptoException | IOException e) {
throw new ServiceLayerException(e);
} finally {
generalLockService.unlock(gitLockKey);
}
}
use of org.craftercms.studio.api.v2.utils.StudioConfiguration in project studio by craftercms.
the class RepositoryManagementServiceInternalImpl method unlockRepository.
@Override
public boolean unlockRepository(String siteId, GitRepositories repositoryType) throws CryptoException {
boolean toRet = false;
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(siteId, repositoryType);
if (Objects.nonNull(repo)) {
toRet = FileUtils.deleteQuietly(Paths.get(repo.getDirectory().getAbsolutePath(), LOCK_FILE).toFile());
}
return toRet;
}
Aggregations