Search in sources :

Example 36 with StudioConfiguration

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

the class GitContentRepository method getRepoFirstCommitId.

@Override
public String getRepoFirstCommitId(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, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
                if (repo != null) {
                    try (RevWalk rw = new RevWalk(repo)) {
                        ObjectId head = repo.resolve(HEAD);
                        if (head != null) {
                            RevCommit root = rw.parseCommit(head);
                            rw.sort(REVERSE);
                            rw.markStart(root);
                            ObjectId first = rw.next();
                            toReturn = first.getName();
                            logger.debug("getRepoFirstCommitId for site: " + site + " First commit ID: " + toReturn);
                        }
                    }
                }
            }
        }
    } catch (IOException | CryptoException e) {
        logger.error("Error getting first 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) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CryptoException(org.craftercms.commons.crypto.CryptoException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 37 with StudioConfiguration

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

the class GitContentRepository method getContentVersion.

@Override
public InputStream getContentVersion(String site, String path, String version) throws ContentNotFoundException {
    InputStream toReturn = null;
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
        RevTree tree = helper.getTreeForCommit(repo, version);
        if (tree != null) {
            try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
                if (tw != null) {
                    ObjectId id = tw.getObjectId(0);
                    ObjectLoader objectLoader = repo.open(id);
                    toReturn = objectLoader.openStream();
                    tw.close();
                }
            } catch (IOException e) {
                logger.error("Error while getting content for file at site: " + site + " path: " + path + " version: " + version, e);
            }
        }
    } catch (IOException | CryptoException e) {
        logger.error("Failed to create RevTree for site: " + site + " path: " + path + " version: " + version, 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) InputStream(java.io.InputStream) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 38 with StudioConfiguration

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

the class BlueprintsUpgradeOperation method execute.

@Override
public void execute(final String site) throws UpgradeException {
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Path globalConfigPath = helper.buildRepoPath(GitRepositories.GLOBAL);
        Path blueprintsPath = Paths.get(globalConfigPath.toAbsolutePath().toString(), studioConfiguration.getProperty(BLUE_PRINTS_PATH));
        String studioManifestLocation = servletContext.getRealPath(STUDIO_MANIFEST_LOCATION);
        String blueprintsManifestLocation = Paths.get(blueprintsPath.toAbsolutePath().toString(), "BLUEPRINTS.MF").toAbsolutePath().toString();
        boolean blueprintManifestExists = Files.exists(Paths.get(blueprintsManifestLocation));
        InputStream studioManifestStream = FileUtils.openInputStream(new File(studioManifestLocation));
        Manifest studioManifest = new Manifest(studioManifestStream);
        VersionInfo studioVersion = VersionInfo.getVersion(studioManifest);
        InputStream blueprintsManifestStream = null;
        Manifest blueprintsManifest = null;
        VersionInfo blueprintsVersion = null;
        if (blueprintManifestExists) {
            blueprintsManifestStream = FileUtils.openInputStream(new File(blueprintsManifestLocation));
            blueprintsManifest = new Manifest(blueprintsManifestStream);
            blueprintsVersion = VersionInfo.getVersion(blueprintsManifest);
        }
        if (!blueprintManifestExists || !StringUtils.equals(studioVersion.getPackageBuild(), blueprintsVersion.getPackageBuild()) || (StringUtils.equals(studioVersion.getPackageBuild(), blueprintsVersion.getPackageBuild()) && !StringUtils.equals(studioVersion.getPackageBuildDate(), blueprintsVersion.getPackageBuildDate()))) {
            String bootstrapBlueprintsFolderPath = servletContext.getRealPath(FILE_SEPARATOR + BOOTSTRAP_REPO_PATH + FILE_SEPARATOR + BOOTSTRAP_REPO_GLOBAL_PATH + FILE_SEPARATOR + studioConfiguration.getProperty(BLUE_PRINTS_PATH));
            File bootstrapBlueprintsFolder = new File(bootstrapBlueprintsFolderPath);
            File[] blueprintFolders = bootstrapBlueprintsFolder.listFiles(File::isDirectory);
            for (File blueprintFolder : blueprintFolders) {
                String blueprintName = blueprintFolder.getName();
                FileUtils.deleteDirectory(Paths.get(blueprintsPath.toAbsolutePath().toString(), blueprintName).toFile());
                TreeCopier tc = new TreeCopier(Paths.get(blueprintFolder.getAbsolutePath()), Paths.get(blueprintsPath.toAbsolutePath().toString(), blueprintName));
                EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
                Files.walkFileTree(Paths.get(blueprintFolder.getAbsolutePath()), opts, Integer.MAX_VALUE, tc);
            }
            FileUtils.copyFile(Paths.get(studioManifestLocation).toFile(), Paths.get(globalConfigPath.toAbsolutePath().toString(), studioConfiguration.getProperty(BLUE_PRINTS_PATH), "BLUEPRINTS.MF").toFile());
        }
        Repository globalRepo = helper.getRepository(site, GitRepositories.GLOBAL);
        try (Git git = new Git(globalRepo)) {
            StatusCommand statusCommand = git.status();
            Status status = retryingRepositoryOperationFacade.call(statusCommand);
            if (status.hasUncommittedChanges() || !status.isClean()) {
                // Commit everything
                // TODO: Consider what to do with the commitId in the future
                AddCommand addCommand = git.add().addFilepattern(GIT_COMMIT_ALL_ITEMS);
                retryingRepositoryOperationFacade.call(addCommand);
                CommitCommand commitCommand = git.commit().setAll(true).setMessage(studioConfiguration.getProperty(REPO_BLUEPRINTS_UPDATED_COMMIT_MESSAGE));
                retryingRepositoryOperationFacade.call(commitCommand);
            }
        } catch (GitAPIException err) {
            logger.error("error creating initial commit for global configuration", err);
        }
    } catch (Exception e) {
        throw new UpgradeException("Error upgrading blueprints in the global repo", e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
}
Also used : Path(java.nio.file.Path) Status(org.eclipse.jgit.api.Status) InputStream(java.io.InputStream) FileVisitOption(java.nio.file.FileVisitOption) Manifest(java.util.jar.Manifest) StatusCommand(org.eclipse.jgit.api.StatusCommand) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) VersionInfo(org.craftercms.commons.monitoring.VersionInfo) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) TreeCopier(org.craftercms.studio.impl.v1.repository.git.TreeCopier) CommitCommand(org.eclipse.jgit.api.CommitCommand) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) File(java.io.File) AddCommand(org.eclipse.jgit.api.AddCommand)

Example 39 with StudioConfiguration

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

the class GitContentRepository method contentExists.

@Override
public boolean contentExists(String site, String path) {
    boolean toReturn = false;
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
        if (repo != null) {
            RevTree tree = helper.getTreeForLastCommit(repo);
            try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
                // pick the first item in the list
                if (tw != null && tw.getObjectId(0) != null) {
                    toReturn = true;
                    tw.close();
                } else if (tw == null) {
                    String gitPath = helper.getGitPath(path);
                    if (StringUtils.isEmpty(gitPath) || gitPath.equals(".")) {
                        toReturn = true;
                    }
                }
            } catch (IOException e) {
                logger.info("Content not found for site: " + site + " path: " + path, e);
            }
        }
    } catch (Exception e) {
        logger.error("Failed to create RevTree for site: " + site + " path: " + path, 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) IOException(java.io.IOException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) 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)

Example 40 with StudioConfiguration

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

the class GitContentRepository method writeContent.

@Override
public String writeContent(String site, String path, InputStream content) {
    // Write content to git and commit it
    String commitId = null;
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
            Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
            if (repo != null) {
                if (helper.writeFile(repo, site, path, content)) {
                    PersonIdent user = helper.getCurrentUserIdent();
                    String username = securityService.getCurrentUser();
                    String comment = helper.getCommitMessage(REPO_SANDBOX_WRITE_COMMIT_MESSAGE).replace(REPO_COMMIT_MESSAGE_USERNAME_VAR, username).replace(REPO_COMMIT_MESSAGE_PATH_VAR, path);
                    commitId = helper.commitFile(repo, site, path, comment, user);
                } else {
                    logger.error("Failed to write content site: " + site + " path: " + path);
                }
            } else {
                logger.error("Missing repository during write for site: " + site + " path: " + path);
            }
        }
    } catch (ServiceLayerException | UserNotFoundException | CryptoException e) {
        logger.error("Unknown service error during write for site: " + site + " path: " + path, e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
    return commitId;
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) PersonIdent(org.eclipse.jgit.lib.PersonIdent) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) CryptoException(org.craftercms.commons.crypto.CryptoException)

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)34 ArrayList (java.util.ArrayList)22 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)21 Path (java.nio.file.Path)20 ObjectId (org.eclipse.jgit.lib.ObjectId)19 RevCommit (org.eclipse.jgit.revwalk.RevCommit)17 RevTree (org.eclipse.jgit.revwalk.RevTree)17 ContentRepository (org.craftercms.studio.api.v2.repository.ContentRepository)15 File (java.io.File)12 HashMap (java.util.HashMap)12 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)11 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)10