Search in sources :

Example 1 with REPO_SANDBOX_BRANCH

use of org.craftercms.studio.api.v2.utils.StudioConfiguration.REPO_SANDBOX_BRANCH 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 REPO_SANDBOX_BRANCH

use of org.craftercms.studio.api.v2.utils.StudioConfiguration.REPO_SANDBOX_BRANCH in project studio by craftercms.

the class GitContentRepository method publish.

@RetryingOperation
@Override
public void publish(String site, String sandboxBranch, List<DeploymentItemTO> deploymentItems, String environment, String author, String comment) throws DeploymentException {
    if (CollectionUtils.isEmpty(deploymentItems)) {
        return;
    }
    String commitId = EMPTY;
    String gitLockKey = SITE_PUBLISHED_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repo = helper.getRepository(site, PUBLISHED);
        boolean repoCreated = false;
        if (Objects.isNull(repo)) {
            helper.createPublishedRepository(site, sandboxBranch);
            repo = helper.getRepository(site, PUBLISHED);
            repoCreated = Objects.nonNull(repo);
        }
        String path = EMPTY;
        String sandboxBranchName = sandboxBranch;
        if (StringUtils.isEmpty(sandboxBranchName)) {
            sandboxBranchName = studioConfiguration.getProperty(REPO_SANDBOX_BRANCH);
        }
        synchronized (repo) {
            try (Git git = new Git(repo)) {
                String inProgressBranchName = environment + IN_PROGRESS_BRANCH_NAME_SUFFIX;
                // fetch "origin/master"
                logger.debug("Fetch from sandbox for site " + site);
                FetchCommand fetchCommand = git.fetch();
                retryingRepositoryOperationFacade.call(fetchCommand);
                // checkout master and pull from sandbox
                logger.debug("Checkout published/master branch for site " + site);
                try {
                    // First delete it in case it already exists (ignored if does not exist)
                    String currentBranch = repo.getBranch();
                    if (currentBranch.endsWith(IN_PROGRESS_BRANCH_NAME_SUFFIX)) {
                        ResetCommand resetCommand = git.reset().setMode(HARD);
                        retryingRepositoryOperationFacade.call(resetCommand);
                    }
                    Ref ref = repo.exactRef(R_HEADS + sandboxBranchName);
                    boolean createBranch = (ref == null);
                    CheckoutCommand checkoutCommand = git.checkout().setName(sandboxBranchName).setCreateBranch(createBranch);
                    retryingRepositoryOperationFacade.call(checkoutCommand);
                    logger.debug("Delete in-progress branch, in case it was not cleaned up for site " + site);
                    DeleteBranchCommand deleteBranchCommand = git.branchDelete().setBranchNames(inProgressBranchName).setForce(true);
                    retryingRepositoryOperationFacade.call(deleteBranchCommand);
                    PullCommand pullCommand = git.pull().setRemote(DEFAULT_REMOTE_NAME).setRemoteBranchName(sandboxBranchName).setStrategy(THEIRS);
                    retryingRepositoryOperationFacade.call(pullCommand);
                } catch (RefNotFoundException e) {
                    logger.error("Failed to checkout published master and to pull content from sandbox for site " + site, e);
                    throw new DeploymentException("Failed to checkout published master and to pull content from " + "sandbox for site " + site);
                }
                // checkout environment branch
                logger.debug("Checkout environment branch " + environment + " for site " + site);
                try {
                    CheckoutCommand checkoutCommand = git.checkout().setName(environment);
                    retryingRepositoryOperationFacade.call(checkoutCommand);
                } catch (RefNotFoundException e) {
                    logger.info("Not able to find branch " + environment + " for site " + site + ". Creating new branch");
                    // create new environment branch
                    // it will start as empty orphan branch
                    CheckoutCommand checkoutCommand = git.checkout().setOrphan(true).setForceRefUpdate(true).setStartPoint(sandboxBranchName).setUpstreamMode(TRACK).setName(environment);
                    retryingRepositoryOperationFacade.call(checkoutCommand);
                    // remove any content to create empty branch
                    RmCommand rmcmd = git.rm();
                    File[] toDelete = repo.getWorkTree().listFiles();
                    for (File toDel : toDelete) {
                        if (!repo.getDirectory().equals(toDel) && !StringUtils.equals(toDel.getName(), DOT_GIT_IGNORE)) {
                            rmcmd.addFilepattern(toDel.getName());
                        }
                    }
                    retryingRepositoryOperationFacade.call(rmcmd);
                    CommitCommand commitCommand = git.commit().setMessage(helper.getCommitMessage(REPO_INITIAL_COMMIT_COMMIT_MESSAGE)).setAllowEmpty(true);
                    retryingRepositoryOperationFacade.call(commitCommand);
                }
                // Create in progress branch
                try {
                    // Create in progress branch
                    logger.debug("Create in-progress branch for site " + site);
                    CheckoutCommand checkoutCommand = git.checkout().setCreateBranch(true).setForceRefUpdate(true).setStartPoint(environment).setUpstreamMode(TRACK).setName(inProgressBranchName);
                    retryingRepositoryOperationFacade.call(checkoutCommand);
                } catch (GitAPIException e) {
                    // TODO: DB: Error ?
                    logger.error("Failed to create in-progress published branch for site " + site);
                }
                Set<String> deployedCommits = new HashSet<String>();
                Set<String> deployedPackages = new HashSet<String>();
                logger.debug("Checkout deployed files started.");
                AddCommand addCommand = git.add();
                for (DeploymentItemTO deploymentItem : deploymentItems) {
                    commitId = deploymentItem.getCommitId();
                    path = helper.getGitPath(deploymentItem.getPath());
                    if (Objects.isNull(commitId) || !commitIdExists(site, PUBLISHED, commitId)) {
                        if (contentExists(site, path)) {
                            if (Objects.isNull(commitId)) {
                                logger.warn("Commit ID is NULL for content " + path + ". Was the git repo reset at some point?");
                            } else {
                                logger.warn("Commit ID " + commitId + " does not exist for content " + path + ". Was the git repo reset at some point?");
                            }
                            logger.info("Publishing content from HEAD for " + path);
                            commitId = getRepoLastCommitId(site);
                        } else {
                            logger.warn("Skipping file " + path + " because commit id is null");
                            continue;
                        }
                    }
                    logger.debug("Checking out file " + path + " from commit id " + commitId + " for site " + site);
                    CheckoutCommand checkout = git.checkout();
                    checkout.setStartPoint(commitId).addPath(path);
                    retryingRepositoryOperationFacade.call(checkout);
                    if (deploymentItem.isMove()) {
                        if (!StringUtils.equals(deploymentItem.getPath(), deploymentItem.getOldPath())) {
                            String oldPath = helper.getGitPath(deploymentItem.getOldPath());
                            RmCommand rmCommand = git.rm().addFilepattern(oldPath).setCached(false);
                            retryingRepositoryOperationFacade.call(rmCommand);
                            cleanUpMoveFolders(git, oldPath);
                        }
                    }
                    if (deploymentItem.isDelete()) {
                        String deletePath = helper.getGitPath(deploymentItem.getPath());
                        boolean isPage = deletePath.endsWith(FILE_SEPARATOR + INDEX_FILE);
                        RmCommand rmCommand = git.rm().addFilepattern(deletePath).setCached(false);
                        retryingRepositoryOperationFacade.call(rmCommand);
                        Path parentToDelete = Paths.get(path).getParent();
                        deleteParentFolder(git, parentToDelete, isPage);
                    }
                    deployedCommits.add(commitId);
                    String packageId = deploymentItem.getPackageId();
                    if (StringUtils.isNotEmpty(packageId)) {
                        deployedPackages.add(deploymentItem.getPackageId());
                    }
                    addCommand.addFilepattern(path);
                    objectMetadataManager.updateLastPublishedDate(site, deploymentItem.getPath(), ZonedDateTime.now(UTC));
                }
                logger.debug("Checkout deployed files completed.");
                // commit all deployed files
                String commitMessage = studioConfiguration.getProperty(REPO_PUBLISHED_COMMIT_MESSAGE);
                logger.debug("Get Author Ident started.");
                User user = userServiceInternal.getUserByIdOrUsername(-1, author);
                PersonIdent authorIdent = helper.getAuthorIdent(user);
                logger.debug("Get Author Ident completed.");
                logger.debug("Git add all published items started.");
                retryingRepositoryOperationFacade.call(addCommand);
                logger.debug("Git add all published items completed.");
                commitMessage = commitMessage.replace("{username}", author);
                commitMessage = commitMessage.replace("{datetime}", ZonedDateTime.now(UTC).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HHmmssSSSX")));
                commitMessage = commitMessage.replace("{source}", "UI");
                commitMessage = commitMessage.replace("{message}", comment);
                StringBuilder sb = new StringBuilder();
                for (String c : deployedCommits) {
                    sb.append(c).append(" ");
                }
                StringBuilder sbPackage = new StringBuilder();
                for (String p : deployedPackages) {
                    sbPackage.append(p).append(" ");
                }
                commitMessage = commitMessage.replace("{commit_id}", sb.toString().trim());
                commitMessage = commitMessage.replace("{package_id}", sbPackage.toString().trim());
                logger.debug("Git commit all published items started.");
                String prologue = studioConfiguration.getProperty(REPO_COMMIT_MESSAGE_PROLOGUE);
                String postscript = studioConfiguration.getProperty(REPO_COMMIT_MESSAGE_POSTSCRIPT);
                StringBuilder sbCommitMessage = new StringBuilder();
                if (StringUtils.isNotEmpty(prologue)) {
                    sbCommitMessage.append(prologue).append("\n\n");
                }
                sbCommitMessage.append(commitMessage);
                if (StringUtils.isNotEmpty(postscript)) {
                    sbCommitMessage.append("\n\n").append(postscript);
                }
                CommitCommand commitCommand = git.commit().setMessage(sbCommitMessage.toString()).setAuthor(authorIdent);
                RevCommit revCommit = retryingRepositoryOperationFacade.call(commitCommand);
                logger.debug("Git commit all published items completed.");
                int commitTime = revCommit.getCommitTime();
                // tag
                ZonedDateTime tagDate2 = Instant.ofEpochSecond(commitTime).atZone(UTC);
                ZonedDateTime publishDate = ZonedDateTime.now(UTC);
                String tagName2 = tagDate2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HHmmssSSSX")) + "_published_on_" + publishDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HHmmssSSSX"));
                logger.debug("Get Author Ident started.");
                PersonIdent authorIdent2 = helper.getAuthorIdent(user);
                logger.debug("Get Author Ident completed.");
                logger.debug("Git tag started.");
                TagCommand tagCommand = git.tag().setTagger(authorIdent2).setName(tagName2).setMessage(commitMessage);
                retryingRepositoryOperationFacade.call(tagCommand);
                logger.debug("Git tag completed.");
                // checkout environment
                logger.debug("Checkout environment " + environment + " branch for site " + site);
                CheckoutCommand checkoutCommand = git.checkout().setName(environment);
                retryingRepositoryOperationFacade.call(checkoutCommand);
                Ref branchRef = repo.findRef(inProgressBranchName);
                // merge in-progress branch
                logger.debug("Merge in-progress branch into environment " + environment + " for site " + site);
                MergeCommand mergeCommand = git.merge().setCommit(true).include(branchRef);
                retryingRepositoryOperationFacade.call(mergeCommand);
                // clean up
                logger.debug("Delete in-progress branch (clean up) for site " + site);
                DeleteBranchCommand deleteBranchCommand = git.branchDelete().setBranchNames(inProgressBranchName).setForce(true);
                retryingRepositoryOperationFacade.call(deleteBranchCommand);
                git.close();
                if (repoCreated) {
                    siteService.setPublishedRepoCreated(site);
                }
            }
        }
    } catch (Exception e) {
        logger.error("Error when publishing site " + site + " to environment " + environment, e);
        throw new DeploymentException("Error when publishing site " + site + " to environment " + environment + " [commit ID = " + commitId + "]");
    } finally {
        generalLockService.unlock(gitLockKey);
    }
}
Also used : CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) User(org.craftercms.studio.api.v2.dal.User) MergeCommand(org.eclipse.jgit.api.MergeCommand) TagCommand(org.eclipse.jgit.api.TagCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException) ZonedDateTime(java.time.ZonedDateTime) ResetCommand(org.eclipse.jgit.api.ResetCommand) RmCommand(org.eclipse.jgit.api.RmCommand) CommitCommand(org.eclipse.jgit.api.CommitCommand) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) HashSet(java.util.HashSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Path(java.nio.file.Path) DeleteBranchCommand(org.eclipse.jgit.api.DeleteBranchCommand) PullCommand(org.eclipse.jgit.api.PullCommand) DeploymentItemTO(org.craftercms.studio.api.v1.to.DeploymentItemTO) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) CryptoException(org.craftercms.commons.crypto.CryptoException) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) InvalidRemoteRepositoryException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v2.repository.ContentRepository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) FetchCommand(org.eclipse.jgit.api.FetchCommand) PersonIdent(org.eclipse.jgit.lib.PersonIdent) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) File(java.io.File) AddCommand(org.eclipse.jgit.api.AddCommand) RetryingOperation(org.craftercms.studio.api.v2.annotation.RetryingOperation)

Example 3 with REPO_SANDBOX_BRANCH

use of org.craftercms.studio.api.v2.utils.StudioConfiguration.REPO_SANDBOX_BRANCH in project studio by craftercms.

the class GitContentRepository method listRemote.

@Override
public List<RemoteRepositoryInfoTO> listRemote(String siteId, String sandboxBranch) throws ServiceLayerException, CryptoException {
    List<RemoteRepositoryInfoTO> res = new ArrayList<RemoteRepositoryInfoTO>();
    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) {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("siteId", siteId);
                    params.put("remoteName", conf.getName());
                    RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
                    if (remoteRepository != null) {
                        switch(remoteRepository.getAuthenticationType()) {
                            case NONE:
                                logger.debug("No authentication");
                                git.fetch().setRemote(conf.getName()).call();
                                break;
                            case BASIC:
                                logger.debug("Basic authentication");
                                String hashedPassword = remoteRepository.getRemotePassword();
                                String password = encryptor.decrypt(hashedPassword);
                                git.fetch().setRemote(conf.getName()).setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteRepository.getRemoteUsername(), password)).call();
                                break;
                            case TOKEN:
                                logger.debug("Token based authentication");
                                String hashedToken = remoteRepository.getRemoteToken();
                                String remoteToken = encryptor.decrypt(hashedToken);
                                git.fetch().setRemote(conf.getName()).setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteToken, EMPTY)).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();
                                git.fetch().setRemote(conf.getName()).setTransportConfigCallback(new TransportConfigCallback() {

                                    @Override
                                    public void configure(Transport transport) {
                                        SshTransport sshTransport = (SshTransport) transport;
                                        sshTransport.setSshSessionFactory(getSshSessionFactory(privateKey, tempKey));
                                    }
                                }).call();
                                Files.delete(tempKey);
                                break;
                            default:
                                throw new ServiceLayerException("Unsupported authentication type " + remoteRepository.getAuthenticationType());
                        }
                    }
                }
                List<Ref> resultRemoteBranches = git.branchList().setListMode(REMOTE).call();
                Map<String, List<String>> remoteBranches = new HashMap<String, List<String>>();
                for (Ref remoteBranchRef : resultRemoteBranches) {
                    String branchFullName = remoteBranchRef.getName().replace(R_REMOTES, "");
                    String remotePart = EMPTY;
                    String branchNamePart = EMPTY;
                    int slashIndex = branchFullName.indexOf("/");
                    if (slashIndex > 0) {
                        remotePart = branchFullName.substring(0, slashIndex);
                        branchNamePart = branchFullName.substring(slashIndex + 1);
                    }
                    if (!remoteBranches.containsKey(remotePart)) {
                        remoteBranches.put(remotePart, new ArrayList<String>());
                    }
                    remoteBranches.get(remotePart).add(branchNamePart);
                }
                String sandboxBranchName = sandboxBranch;
                if (StringUtils.isEmpty(sandboxBranchName)) {
                    sandboxBranchName = studioConfiguration.getProperty(REPO_SANDBOX_BRANCH);
                }
                for (RemoteConfig conf : resultRemotes) {
                    RemoteRepositoryInfoTO rri = new RemoteRepositoryInfoTO();
                    rri.setName(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.setPush_url(sbPushUrl.toString());
                    res.add(rri);
                }
            }
        } catch (GitAPIException | CryptoException | IOException e) {
            logger.error("Error getting remote repositories for site " + siteId, e);
        }
    }
    return res;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) ArrayList(java.util.ArrayList) List(java.util.List) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) Path(java.nio.file.Path) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) RemoteRepositoryInfoTO(org.craftercms.studio.api.v1.to.RemoteRepositoryInfoTO) 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 4 with REPO_SANDBOX_BRANCH

use of org.craftercms.studio.api.v2.utils.StudioConfiguration.REPO_SANDBOX_BRANCH in project studio by craftercms.

the class GitContentRepository method initialPublish.

@Override
public void initialPublish(String site, String sandboxBranch, String environment, String author, String comment) throws DeploymentException, CryptoException {
    String gitLockKey = SITE_PUBLISHED_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(site, PUBLISHED);
    String commitId = EMPTY;
    String sandboxBranchName = sandboxBranch;
    if (StringUtils.isEmpty(sandboxBranchName)) {
        sandboxBranchName = studioConfiguration.getProperty(REPO_SANDBOX_BRANCH);
    }
    generalLockService.lock(gitLockKey);
    synchronized (repo) {
        try (Git git = new Git(repo)) {
            // fetch "origin/master"
            logger.debug("Fetch from sandbox for site " + site);
            git.fetch().call();
            // checkout master and pull from sandbox
            logger.debug("Checkout published/master branch for site " + site);
            try {
                CheckoutCommand checkoutCommand = git.checkout().setName(sandboxBranchName);
                retryingRepositoryOperationFacade.call(checkoutCommand);
                PullCommand pullCommand = git.pull().setRemote(DEFAULT_REMOTE_NAME).setRemoteBranchName(sandboxBranchName).setStrategy(THEIRS);
                retryingRepositoryOperationFacade.call(pullCommand);
            } catch (RefNotFoundException e) {
                logger.error("Failed to checkout published master and to pull content from sandbox for site " + site, e);
                throw new DeploymentException("Failed to checkout published master and to pull content from " + "sandbox for site " + site);
            }
            // checkout environment branch
            logger.debug("Checkout environment branch " + environment + " for site " + site);
            try {
                CheckoutCommand checkoutCommand = git.checkout().setCreateBranch(true).setForce(true).setStartPoint(sandboxBranchName).setUpstreamMode(TRACK).setName(environment);
                retryingRepositoryOperationFacade.call(checkoutCommand);
            } catch (RefNotFoundException e) {
                logger.info("Not able to find branch " + environment + " for site " + site + ". Creating new branch");
            }
            // tag
            PersonIdent authorIdent = helper.getAuthorIdent(author);
            ZonedDateTime publishDate = ZonedDateTime.now(UTC);
            String tagName = publishDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HHmmssSSSX")) + "_published_on_" + publishDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HHmmssSSSX"));
            TagCommand tagCommand = git.tag().setTagger(authorIdent).setName(tagName).setMessage(comment);
            retryingRepositoryOperationFacade.call(tagCommand);
        } catch (Exception e) {
            logger.error("Error when publishing site " + site + " to environment " + environment, e);
            throw new DeploymentException("Error when publishing site " + site + " to environment " + environment + " [commit ID = " + commitId + "]");
        } finally {
            generalLockService.unlock(gitLockKey);
        }
    }
}
Also used : RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) PullCommand(org.eclipse.jgit.api.PullCommand) Git(org.eclipse.jgit.api.Git) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException) PersonIdent(org.eclipse.jgit.lib.PersonIdent) ZonedDateTime(java.time.ZonedDateTime) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) JSchException(com.jcraft.jsch.JSchException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) RefNotFoundException(org.eclipse.jgit.api.errors.RefNotFoundException) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) URISyntaxException(java.net.URISyntaxException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) ContentNotFoundException(org.craftercms.studio.api.v1.exception.ContentNotFoundException) RemoteAlreadyExistsException(org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException) DeploymentException(org.craftercms.studio.api.v1.service.deployment.DeploymentException) CryptoException(org.craftercms.commons.crypto.CryptoException) TransportException(org.eclipse.jgit.api.errors.TransportException) RemoteRepositoryNotBareException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotBareException) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) InvalidRemoteRepositoryException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException) TagCommand(org.eclipse.jgit.api.TagCommand)

Example 5 with REPO_SANDBOX_BRANCH

use of org.craftercms.studio.api.v2.utils.StudioConfiguration.REPO_SANDBOX_BRANCH in project studio by craftercms.

the class GitRepositoryHelper method makeRepoOrphan.

private void makeRepoOrphan(Repository repository, String site) throws IOException, GitAPIException, ServiceLayerException, UserNotFoundException {
    logger.debug("Make repository orphan fir site " + site);
    String sandboxBranchName = repository.getBranch();
    if (StringUtils.isEmpty(sandboxBranchName)) {
        sandboxBranchName = studioConfiguration.getProperty(REPO_SANDBOX_BRANCH);
    }
    String sandboxBranchOrphanName = sandboxBranchName + "_orphan";
    logger.debug("Shallow clone is not implemented in JGit. Instead we are creating new orphan branch after " + "cloning and renaming it to sandbox branch to replace fully cloned branch");
    try (Git git = new Git(repository)) {
        logger.debug("Create temporary orphan branch " + sandboxBranchOrphanName);
        git.checkout().setName(sandboxBranchOrphanName).setStartPoint(sandboxBranchName).setOrphan(true).call();
        // Reset everything to simulate first commit as created empty repo
        logger.debug("Soft reset to commit empty repo");
        git.reset().call();
        logger.debug("Commit empty repo, because we need to have HEAD to delete old and rename new branch");
        CommitCommand commitCommand = git.commit().setMessage(getCommitMessage(REPO_CREATE_AS_ORPHAN_COMMIT_MESSAGE));
        String username = securityService.getCurrentUser();
        User user = userServiceInternal.getUserByIdOrUsername(-1, username);
        if (Objects.nonNull(user)) {
            commitCommand = commitCommand.setAuthor(getAuthorIdent(user));
        }
        commitCommand.call();
        logger.debug("Delete cloned branch " + sandboxBranchName);
        git.branchDelete().setBranchNames(sandboxBranchName).setForce(true).call();
        logger.debug("Rename temporary orphan branch to sandbox branch");
        git.branchRename().setNewName(sandboxBranchName).setOldName(sandboxBranchOrphanName).call();
    }
}
Also used : Git(org.eclipse.jgit.api.Git) User(org.craftercms.studio.api.v2.dal.User) CommitCommand(org.eclipse.jgit.api.CommitCommand)

Aggregations

Git (org.eclipse.jgit.api.Git)5 IOException (java.io.IOException)4 CryptoException (org.craftercms.commons.crypto.CryptoException)4 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)4 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)4 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 Repository (org.eclipse.jgit.lib.Repository)4 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)3 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)3 URISyntaxException (java.net.URISyntaxException)2 Path (java.nio.file.Path)2 ZonedDateTime (java.time.ZonedDateTime)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 InvalidRemoteRepositoryCredentialsException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException)2 InvalidRemoteRepositoryException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException)2 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)2 RemoteAlreadyExistsException (org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException)2