Search in sources :

Example 16 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException in project studio by craftercms.

the class StudioClusterGlobalRepoSyncTask method addRemoteRepository.

protected void addRemoteRepository(ClusterMember member, String remoteUrl) throws IOException, InvalidRemoteUrlException, ServiceLayerException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repo = builder.setGitDir(Paths.get(studioConfiguration.getProperty(StudioConfiguration.REPO_BASE_PATH), studioConfiguration.getProperty(StudioConfiguration.GLOBAL_REPO_PATH)).resolve(GIT_ROOT).toFile()).readEnvironment().findGitDir().build();
    try (Git git = new Git(repo)) {
        Config storedConfig = repo.getConfig();
        Set<String> remotes = storedConfig.getSubsections(CONFIG_SECTION_REMOTE);
        if (remotes.contains(member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""))) {
            try {
                studioClusterUtils.removeRemote(git, member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""));
            } catch (GitAPIException e) {
                logger.debug("Error while cleaning remote repositories for global repo", e);
            }
        }
        if (remotes.contains(member.getGitRemoteName())) {
            logger.debug("Remote " + member.getGitRemoteName() + " already exists for global repo");
            String storedRemoteUrl = storedConfig.getString(CONFIG_SECTION_REMOTE, member.getGitRemoteName(), CONFIG_PARAMETER_URL);
            if (!StringUtils.equals(storedRemoteUrl, remoteUrl)) {
                RemoteSetUrlCommand remoteSetUrlCommand = git.remoteSetUrl();
                remoteSetUrlCommand.setName(member.getGitRemoteName());
                remoteSetUrlCommand.setUri(new URIish(remoteUrl));
                remoteSetUrlCommand.call();
            }
        } else {
            logger.debug("Add " + member.getLocalAddress() + " as remote to sandbox");
            RemoteAddCommand remoteAddCommand = git.remoteAdd();
            remoteAddCommand.setName(member.getGitRemoteName());
            remoteAddCommand.setUri(new URIish(remoteUrl));
            remoteAddCommand.call();
        }
    } catch (URISyntaxException e) {
        logger.error("Remote URL is invalid " + remoteUrl, e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException e) {
        logger.error("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for global repo", e);
        throw new ServiceLayerException("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for global repo", e);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Config(org.eclipse.jgit.lib.Config) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) URISyntaxException(java.net.URISyntaxException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand)

Example 17 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException in project studio by craftercms.

the class StudioClusterPublishedRepoSyncTask method addRemoteRepository.

protected void addRemoteRepository(String siteId, ClusterMember member, String remoteUrl) 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)) {
        Config storedConfig = repo.getConfig();
        Set<String> remotes = storedConfig.getSubsections(CONFIG_SECTION_REMOTE);
        if (remotes.contains(member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""))) {
            try {
                removeRemote(git, member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""));
            } catch (GitAPIException e) {
                logger.debug("Error while cleaning up remote repository for site " + siteId, e);
            }
        }
        if (remotes.contains(member.getGitRemoteName())) {
            logger.debug("Remote " + member.getGitRemoteName() + " already exists for PUBLISHED repo for site " + siteId);
            String storedRemoteUrl = storedConfig.getString(CONFIG_SECTION_REMOTE, member.getGitRemoteName(), CONFIG_PARAMETER_URL);
            if (!StringUtils.equals(storedRemoteUrl, remoteUrl)) {
                RemoteSetUrlCommand remoteSetUrlCommand = git.remoteSetUrl();
                remoteSetUrlCommand.setName(member.getGitRemoteName());
                remoteSetUrlCommand.setUri(new URIish(remoteUrl));
                remoteSetUrlCommand.call();
            }
        } else {
            logger.debug("Add " + member.getLocalAddress() + " as remote to PUBLISHED");
            RemoteAddCommand remoteAddCommand = git.remoteAdd();
            remoteAddCommand.setName(member.getGitRemoteName());
            remoteAddCommand.setUri(new URIish(remoteUrl));
            remoteAddCommand.call();
        }
    } catch (URISyntaxException e) {
        logger.error("Remote URL is invalid " + remoteUrl, e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException e) {
        logger.error("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
        throw new ServiceLayerException("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Config(org.eclipse.jgit.lib.Config) ServicesConfig(org.craftercms.studio.api.v1.service.configuration.ServicesConfig) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) URISyntaxException(java.net.URISyntaxException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand)

Example 18 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException in project studio by craftercms.

the class StudioClusterSandboxRepoSyncTask method addRemoteRepository.

private void addRemoteRepository(String siteId, ClusterMember member, String remoteUrl) 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)) {
        Config storedConfig = repo.getConfig();
        Set<String> remotes = storedConfig.getSubsections(CONFIG_SECTION_REMOTE);
        if (remotes.contains(member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""))) {
            try {
                removeRemote(git, member.getGitRemoteName().replaceFirst(CLUSTER_NODE_REMOTE_NAME_PREFIX, ""));
            } catch (GitAPIException e) {
                logger.debug("Error while cleaning up remote repository for site " + siteId, e);
            }
        }
        if (remotes.contains(member.getGitRemoteName())) {
            logger.debug("Remote " + member.getGitRemoteName() + " already exists for SANDBOX repo for site " + siteId);
            String storedRemoteUrl = storedConfig.getString(CONFIG_SECTION_REMOTE, member.getGitRemoteName(), CONFIG_PARAMETER_URL);
            if (!StringUtils.equals(storedRemoteUrl, remoteUrl)) {
                RemoteSetUrlCommand remoteSetUrlCommand = git.remoteSetUrl();
                remoteSetUrlCommand.setName(member.getGitRemoteName());
                remoteSetUrlCommand.setUri(new URIish(remoteUrl));
                remoteSetUrlCommand.call();
            }
        } else {
            logger.debug("Add " + member.getLocalAddress() + " as remote to SANDBOX");
            RemoteAddCommand remoteAddCommand = git.remoteAdd();
            remoteAddCommand.setName(member.getGitRemoteName());
            remoteAddCommand.setUri(new URIish(remoteUrl));
            remoteAddCommand.call();
        }
    } catch (URISyntaxException e) {
        logger.error("Remote URL is invalid " + remoteUrl, e);
        throw new InvalidRemoteUrlException();
    } catch (GitAPIException e) {
        logger.error("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
        throw new ServiceLayerException("Error while adding remote " + member.getGitRemoteName() + " (url: " + remoteUrl + ") for site " + siteId, e);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Config(org.eclipse.jgit.lib.Config) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) URISyntaxException(java.net.URISyntaxException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteSetUrlCommand(org.eclipse.jgit.api.RemoteSetUrlCommand)

Example 19 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException 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);
    }
}
Also used : URIish(org.eclipse.jgit.transport.URIish) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) URISyntaxException(java.net.URISyntaxException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)

Example 20 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException in project studio by craftercms.

the class ContentServiceImpl method pushToRemote.

@Override
public boolean pushToRemote(String siteId, String remoteName, String remoteBranch) throws ServiceLayerException, InvalidRemoteUrlException, AuthenticationException, CryptoException {
    if (!siteService.exists(siteId)) {
        throw new SiteNotFoundException();
    }
    boolean toRet = _contentRepository.pushToRemote(siteId, remoteName, remoteBranch);
    SiteFeed siteFeed = siteService.getSite(siteId);
    AuditLog auditLog = auditServiceInternal.createAuditLogEntry();
    auditLog.setOperation(OPERATION_PUSH_TO_REMOTE);
    auditLog.setSiteId(siteFeed.getId());
    auditLog.setActorId(userService.getCurrentUser().getUsername());
    auditLog.setPrimaryTargetId(remoteName + "/" + remoteBranch);
    auditLog.setPrimaryTargetType(TARGET_TYPE_REMOTE_REPOSITORY);
    auditLog.setPrimaryTargetValue(remoteName + "/" + remoteBranch);
    auditServiceInternal.insertAuditLog(auditLog);
    return toRet;
}
Also used : SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) AuditLog(org.craftercms.studio.api.v2.dal.AuditLog)

Aggregations

ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)15 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)15 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)12 Git (org.eclipse.jgit.api.Git)12 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)12 Repository (org.eclipse.jgit.lib.Repository)12 IOException (java.io.IOException)11 CryptoException (org.craftercms.commons.crypto.CryptoException)10 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)10 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)9 URISyntaxException (java.net.URISyntaxException)8 RemoteAddCommand (org.eclipse.jgit.api.RemoteAddCommand)8 URIish (org.eclipse.jgit.transport.URIish)8 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)7 Config (org.eclipse.jgit.lib.Config)7 Path (java.nio.file.Path)6 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)6 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)5 HashMap (java.util.HashMap)4 PullCommand (org.eclipse.jgit.api.PullCommand)4