Search in sources :

Example 26 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class GitRepositoryHelper method performInitialCommit.

/**
 * Perform an initial commit after large changes to a site. Will not work against the global config repo.
 * @param site
 * @param message
 * @return true if successful, false otherwise
 */
public boolean performInitialCommit(String site, String message, String sandboxBranch) {
    boolean toReturn = true;
    Repository repo = getRepository(site, GitRepositories.SANDBOX, sandboxBranch);
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try (Git git = new Git(repo)) {
        Status status = git.status().call();
        if (status.hasUncommittedChanges() || !status.isClean()) {
            DirCache dirCache = git.add().addFilepattern(GIT_COMMIT_ALL_ITEMS).call();
            CommitCommand commitCommand = git.commit().setMessage(message);
            String username = securityService.getCurrentUser();
            User user = userServiceInternal.getUserByIdOrUsername(-1, username);
            if (Objects.nonNull(user)) {
                commitCommand = commitCommand.setAuthor(getAuthorIdent(user));
            }
            RevCommit commit = commitCommand.call();
        // TODO: SJ: Do we need the commit id?
        // commitId = commit.getName();
        }
        checkoutSandboxBranch(site, repo, sandboxBranch);
        git.close();
    } catch (GitAPIException | UserNotFoundException | ServiceLayerException err) {
        logger.error("error creating initial commit for site:  " + site, err);
        toReturn = false;
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return toReturn;
}
Also used : Status(org.eclipse.jgit.api.Status) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) User(org.craftercms.studio.api.v2.dal.User) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) DirCache(org.eclipse.jgit.dircache.DirCache) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) CommitCommand(org.eclipse.jgit.api.CommitCommand) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 27 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class AbstractUpgradeOperation method commitAllChanges.

protected void commitAllChanges(String site) throws UpgradeException {
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        Path repositoryPath = getRepositoryPath(site);
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repo = builder.setGitDir(repositoryPath.toFile()).readEnvironment().findGitDir().build();
        try (Git git = new Git(repo)) {
            git.add().addFilepattern(".").call();
            Status status = git.status().call();
            if (status.hasUncommittedChanges() || !status.isClean()) {
                git.commit().setAll(true).setMessage(getCommitMessage()).call();
            }
        }
    } catch (IOException | GitAPIException e) {
        throw new UpgradeException("Error committing changes for site " + site, e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
}
Also used : Path(java.nio.file.Path) Status(org.eclipse.jgit.api.Status) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) IOException(java.io.IOException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Example 28 with PATTERN_SITE

use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.

the class SiteRepositoryUpgradePipelineImpl method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(final String site) throws UpgradeException {
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        clusterSandboxRepoSyncTask.execute(site);
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repository = helper.getRepository(site, GitRepositories.SANDBOX);
        String sandboxBranch = siteSandboxBranch;
        if (repository != null) {
            Git git = new Git(repository);
            try {
                if (!isEmpty()) {
                    SiteFeed siteFeed = siteService.getSite(site);
                    if (!StringUtils.isEmpty(siteFeed.getSandboxBranch())) {
                        sandboxBranch = siteFeed.getSandboxBranch();
                    }
                    createTemporaryBranch(site, git);
                    checkoutBranch(siteUpgradeBranch, git);
                    super.execute(site);
                    checkoutBranch(sandboxBranch, git);
                    mergeTemporaryBranch(repository, git);
                    deleteTemporaryBranch(git);
                }
            } catch (GitAPIException | IOException | SiteNotFoundException e) {
                throw new UpgradeException("Error branching or merging upgrade branch for site " + site, e);
            } finally {
                if (!isEmpty()) {
                    try {
                        checkoutBranch(sandboxBranch, git);
                    } catch (GitAPIException e) {
                        logger.error("Error cleaning up repo for site " + site, e);
                    }
                }
                git.close();
            }
        }
    } catch (CryptoException e) {
        throw new UpgradeException("Unexpected error upgrading site " + site, e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
}
Also used : GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) IOException(java.io.IOException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException)

Aggregations

Repository (org.eclipse.jgit.lib.Repository)25 Git (org.eclipse.jgit.api.Git)23 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)23 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)21 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)21 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)20 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)20 IOException (java.io.IOException)17 CryptoException (org.craftercms.commons.crypto.CryptoException)16 Path (java.nio.file.Path)14 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)12 File (java.io.File)7 CommitCommand (org.eclipse.jgit.api.CommitCommand)6 Status (org.eclipse.jgit.api.Status)6 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)5 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)5 InvalidRemoteRepositoryCredentialsException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException)4 InvalidRemoteRepositoryException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException)4 RemoteRepositoryNotFoundException (org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException)4 AddCommand (org.eclipse.jgit.api.AddCommand)4