Search in sources :

Example 1 with RemoteRepositoryInfo

use of org.craftercms.studio.api.v2.dal.RemoteRepositoryInfo 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;
}
Also used : RemoteRepositoryInfo(org.craftercms.studio.api.v2.dal.RemoteRepositoryInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) RemoteNotRemovableException(org.craftercms.studio.api.v1.exception.repository.RemoteNotRemovableException) URISyntaxException(java.net.URISyntaxException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) RemoteAlreadyExistsException(org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException) CryptoException(org.craftercms.commons.crypto.CryptoException) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) 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) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Example 2 with RemoteRepositoryInfo

use of org.craftercms.studio.api.v2.dal.RemoteRepositoryInfo in project studio by craftercms.

the class RepositoryManagementController method listRemotes.

@GetMapping(value = LIST_REMOTES, produces = APPLICATION_JSON_VALUE)
public ResponseBody listRemotes(@RequestParam(name = "siteId", required = true) String siteId) throws ServiceLayerException, CryptoException {
    if (!siteService.exists(siteId)) {
        throw new SiteNotFoundException(siteId);
    }
    List<RemoteRepositoryInfo> remotes = repositoryManagementService.listRemotes(siteId);
    ResponseBody responseBody = new ResponseBody();
    ResultList<RemoteRepositoryInfo> result = new ResultList<RemoteRepositoryInfo>();
    result.setEntities(RESULT_KEY_REMOTES, remotes);
    result.setResponse(OK);
    responseBody.setResult(result);
    return responseBody;
}
Also used : ResultList(org.craftercms.studio.model.rest.ResultList) RemoteRepositoryInfo(org.craftercms.studio.api.v2.dal.RemoteRepositoryInfo) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) ResponseBody(org.craftercms.studio.model.rest.ResponseBody) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 3 with RemoteRepositoryInfo

use of org.craftercms.studio.api.v2.dal.RemoteRepositoryInfo in project studio by craftercms.

the class RepositoryManagementServiceInternalImpl method getRemoteRepositoryInfo.

private List<RemoteRepositoryInfo> getRemoteRepositoryInfo(List<RemoteConfig> resultRemotes, Map<String, List<String>> remoteBranches, Map<String, String> unreachableRemotes, String sandboxBranchName) {
    List<RemoteRepositoryInfo> res = new ArrayList<RemoteRepositoryInfo>();
    for (RemoteConfig conf : resultRemotes) {
        RemoteRepositoryInfo rri = new RemoteRepositoryInfo();
        rri.setName(conf.getName());
        if (MapUtils.isNotEmpty(unreachableRemotes) && unreachableRemotes.containsKey(conf.getName())) {
            rri.setReachable(false);
            rri.setUnreachableReason(unreachableRemotes.get(conf.getName()));
        }
        List<String> branches = remoteBranches.get(rri.getName());
        if (CollectionUtils.isEmpty(branches)) {
            branches = new ArrayList<String>();
            branches.add(sandboxBranchName);
        }
        rri.setBranches(branches);
        StringBuilder sbUrl = new StringBuilder();
        if (CollectionUtils.isNotEmpty(conf.getURIs())) {
            for (int i = 0; i < conf.getURIs().size(); i++) {
                sbUrl.append(conf.getURIs().get(i).toString());
                if (i < conf.getURIs().size() - 1) {
                    sbUrl.append(":");
                }
            }
        }
        rri.setUrl(sbUrl.toString());
        StringBuilder sbFetch = new StringBuilder();
        if (CollectionUtils.isNotEmpty(conf.getFetchRefSpecs())) {
            for (int i = 0; i < conf.getFetchRefSpecs().size(); i++) {
                sbFetch.append(conf.getFetchRefSpecs().get(i).toString());
                if (i < conf.getFetchRefSpecs().size() - 1) {
                    sbFetch.append(":");
                }
            }
        }
        rri.setFetch(sbFetch.toString());
        StringBuilder sbPushUrl = new StringBuilder();
        if (CollectionUtils.isNotEmpty(conf.getPushURIs())) {
            for (int i = 0; i < conf.getPushURIs().size(); i++) {
                sbPushUrl.append(conf.getPushURIs().get(i).toString());
                if (i < conf.getPushURIs().size() - 1) {
                    sbPushUrl.append(":");
                }
            }
        } else {
            sbPushUrl.append(rri.getUrl());
        }
        rri.setPushUrl(sbPushUrl.toString());
        res.add(rri);
    }
    return res;
}
Also used : RemoteRepositoryInfo(org.craftercms.studio.api.v2.dal.RemoteRepositoryInfo) ArrayList(java.util.ArrayList) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig)

Aggregations

RemoteRepositoryInfo (org.craftercms.studio.api.v2.dal.RemoteRepositoryInfo)3 ArrayList (java.util.ArrayList)2 RemoteConfig (org.eclipse.jgit.transport.RemoteConfig)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 CryptoException (org.craftercms.commons.crypto.CryptoException)1 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)1 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)1 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)1 RemoteAlreadyExistsException (org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException)1 RemoteNotRemovableException (org.craftercms.studio.api.v1.exception.repository.RemoteNotRemovableException)1 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)1 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)1 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)1 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)1 ResponseBody (org.craftercms.studio.model.rest.ResponseBody)1 ResultList (org.craftercms.studio.model.rest.ResultList)1