use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method insertFullGitLog.
@Override
public void insertFullGitLog(String siteId, int processed) {
List<GitLog> gitLogs = new ArrayList<>();
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
synchronized (helper.getRepository(siteId, SANDBOX)) {
Repository repo = helper.getRepository(siteId, SANDBOX);
try (Git git = new Git(repo)) {
Iterable<RevCommit> logs = git.log().call();
for (RevCommit rev : logs) {
GitLog gitLog = new GitLog();
gitLog.setCommitId(rev.getId().getName());
gitLog.setProcessed(processed);
gitLog.setSiteId(siteId);
gitLogs.add(gitLog);
}
}
}
} catch (GitAPIException | CryptoException e) {
logger.error("Error getting full git log for site " + siteId, e);
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("siteId", siteId);
params.put("gitLogs", gitLogs);
params.put("processed", 1);
gitLogDao.insertGitLogList(params);
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method reloadRepository.
@Override
public void reloadRepository(String siteId) {
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
helper.removeSandbox(siteId);
helper.getRepository(siteId, SANDBOX);
} catch (CryptoException e) {
logger.error("Unexpected error reloading repository for site " + siteId);
}
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method pushToRemote.
@Override
public boolean pushToRemote(String siteId, String remoteName, String remoteBranch) throws ServiceLayerException, InvalidRemoteUrlException, CryptoException {
logger.debug("Get remote data from database for remote " + remoteName + " and site " + siteId);
Map<String, String> params = new HashMap<String, String>();
params.put("siteId", siteId);
params.put("remoteName", remoteName);
RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
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);
switch(remoteRepository.getAuthenticationType()) {
case NONE:
logger.debug("No authentication");
pushResultIterable = pushCommand.call();
break;
case BASIC:
logger.debug("Basic authentication");
String hashedPassword = remoteRepository.getRemotePassword();
String password = encryptor.decrypt(hashedPassword);
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteRepository.getRemoteUsername(), password));
pushResultIterable = pushCommand.call();
break;
case TOKEN:
logger.debug("Token based authentication");
String hashedToken = remoteRepository.getRemoteToken();
String remoteToken = encryptor.decrypt(hashedToken);
pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteToken, EMPTY));
pushResultIterable = pushCommand.call();
break;
case PRIVATE_KEY:
logger.debug("Private key authentication");
final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
String hashedPrivateKey = remoteRepository.getRemotePrivateKey();
String privateKey = encryptor.decrypt(hashedPrivateKey);
tempKey.toFile().deleteOnExit();
pushCommand.setTransportConfigCallback(new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(getSshSessionFactory(privateKey, tempKey));
}
});
pushResultIterable = retryingRepositoryOperationFacade.call(pushCommand);
Files.delete(tempKey);
break;
default:
throw new ServiceLayerException("Unsupported authentication type " + remoteRepository.getAuthenticationType());
}
boolean toRet = true;
List<RemoteRefUpdate.Status> failure = Arrays.asList(REJECTED_NODELETE, REJECTED_NONFASTFORWARD, REJECTED_REMOTE_CHANGED, REJECTED_OTHER_REASON);
for (PushResult pushResult : pushResultIterable) {
Collection<RemoteRefUpdate> updates = pushResult.getRemoteUpdates();
for (RemoteRefUpdate remoteRefUpdate : updates) {
toRet = toRet && !failure.contains(remoteRefUpdate.getStatus());
if (!toRet)
break;
}
if (!toRet)
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.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method getRepoLastCommitId.
@Override
public String getRepoLastCommitId(final String site) {
String toReturn = EMPTY;
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repository = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repository != null) {
synchronized (repository) {
Repository repo = helper.getRepository(site, SANDBOX);
ObjectId commitId = repo.resolve(HEAD);
if (commitId != null) {
toReturn = commitId.getName();
}
}
}
} catch (IOException | CryptoException e) {
logger.error("Error getting last commit ID for site " + site, e);
}
return toReturn;
}
use of org.craftercms.studio.api.v2.utils.GitRepositoryHelper in project studio by craftercms.
the class GitContentRepository method lockItemForPublishing.
@Override
public void lockItemForPublishing(String site, String path) {
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repo = helper.getRepository(site, PUBLISHED);
synchronized (repo) {
try (TreeWalk tw = new TreeWalk(repo)) {
RevTree tree = helper.getTreeForLastCommit(repo);
// tree ‘0’
tw.addTree(tree);
tw.setRecursive(false);
tw.setFilter(PathFilter.create(path));
if (!tw.next()) {
return;
}
File repoRoot = repo.getWorkTree();
Paths.get(repoRoot.getPath(), tw.getPathString());
File file = new File(tw.getPathString());
LockFile lock = new LockFile(file);
lock.lock();
}
}
} catch (IOException | CryptoException e) {
logger.error("Error while locking file for site: " + site + " path: " + path, e);
}
}
Aggregations