Search in sources :

Example 21 with GitRepositoryHelper

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) GitLog(org.craftercms.studio.api.v2.dal.GitLog) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 22 with GitRepositoryHelper

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);
    }
}
Also used : GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 23 with GitRepositoryHelper

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);
    }
}
Also used : HashMap(java.util.HashMap) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RefSpec(org.eclipse.jgit.transport.RefSpec) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) Path(java.nio.file.Path) Status(org.eclipse.jgit.api.Status) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PushResult(org.eclipse.jgit.transport.PushResult) IOException(java.io.IOException) PushCommand(org.eclipse.jgit.api.PushCommand) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) SshTransport(org.eclipse.jgit.transport.SshTransport) Transport(org.eclipse.jgit.transport.Transport) CryptoException(org.craftercms.commons.crypto.CryptoException) SshTransport(org.eclipse.jgit.transport.SshTransport)

Example 24 with GitRepositoryHelper

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;
}
Also used : RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 25 with GitRepositoryHelper

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);
    }
}
Also used : RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) LockFile(org.eclipse.jgit.internal.storage.file.LockFile) IOException(java.io.IOException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) File(java.io.File) LockFile(org.eclipse.jgit.internal.storage.file.LockFile) RevTree(org.eclipse.jgit.revwalk.RevTree)

Aggregations

GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)65 Repository (org.eclipse.jgit.lib.Repository)59 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)58 CryptoException (org.craftercms.commons.crypto.CryptoException)53 IOException (java.io.IOException)47 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)43 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)42 Git (org.eclipse.jgit.api.Git)41 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)30 Path (java.nio.file.Path)20 ArrayList (java.util.ArrayList)19 ObjectId (org.eclipse.jgit.lib.ObjectId)19 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)17 RevCommit (org.eclipse.jgit.revwalk.RevCommit)17 RevTree (org.eclipse.jgit.revwalk.RevTree)17 ContentRepository (org.craftercms.studio.api.v2.repository.ContentRepository)15 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)13 File (java.io.File)12 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)11 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)10