Search in sources :

Example 1 with PublishingHistoryItem

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

the class DashboardServiceImpl method preparePublishingResult.

private List<PublishingDashboardItem> preparePublishingResult(List<PublishingHistoryItem> publishingHistory) {
    List<PublishingDashboardItem> publishingDashboardItems = new ArrayList<PublishingDashboardItem>();
    for (PublishingHistoryItem historyItem : publishingHistory) {
        PublishingDashboardItem dashboardItem = new PublishingDashboardItem();
        ItemMetadata itemMetadata = objectMetadataManager.getProperties(historyItem.getSiteId(), historyItem.getPath());
        dashboardItem.setSiteId(historyItem.getSiteId());
        dashboardItem.setPath(historyItem.getPath());
        dashboardItem.setLabel(itemMetadata.getName());
        dashboardItem.setEnvironment(historyItem.getEnvironment());
        dashboardItem.setPublishedDate(historyItem.getPublishedDate());
        dashboardItem.setPublisher(historyItem.getPublisher());
        publishingDashboardItems.add(dashboardItem);
    }
    return publishingDashboardItems;
}
Also used : PublishingDashboardItem(org.craftercms.studio.model.rest.dashboard.PublishingDashboardItem) ArrayList(java.util.ArrayList) PublishingHistoryItem(org.craftercms.studio.api.v2.dal.PublishingHistoryItem) ItemMetadata(org.craftercms.studio.api.v1.dal.ItemMetadata)

Example 2 with PublishingHistoryItem

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

the class PublishServiceImpl method preparePublishingResult.

private List<PublishingDashboardItem> preparePublishingResult(List<PublishingHistoryItem> publishingHistory) {
    List<PublishingDashboardItem> publishingDashboardItems = new ArrayList<PublishingDashboardItem>();
    for (PublishingHistoryItem historyItem : publishingHistory) {
        PublishingDashboardItem dashboardItem = new PublishingDashboardItem();
        ItemMetadata itemMetadata = objectMetadataManager.getProperties(historyItem.getSiteId(), historyItem.getPath());
        dashboardItem.setSiteId(historyItem.getSiteId());
        dashboardItem.setPath(historyItem.getPath());
        dashboardItem.setLabel(itemMetadata.getName());
        dashboardItem.setEnvironment(historyItem.getEnvironment());
        dashboardItem.setPublishedDate(historyItem.getPublishedDate());
        dashboardItem.setPublisher(historyItem.getPublisher());
        publishingDashboardItems.add(dashboardItem);
    }
    return publishingDashboardItems;
}
Also used : PublishingDashboardItem(org.craftercms.studio.model.rest.dashboard.PublishingDashboardItem) ArrayList(java.util.ArrayList) PublishingHistoryItem(org.craftercms.studio.api.v2.dal.PublishingHistoryItem) ItemMetadata(org.craftercms.studio.api.v1.dal.ItemMetadata)

Example 3 with PublishingHistoryItem

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

the class GitContentRepository method getPublishingHistory.

@Override
public List<PublishingHistoryItem> getPublishingHistory(String siteId, String environment, String pathRegex, String publisher, ZonedDateTime fromDate, ZonedDateTime toDate, int limit) {
    List<PublishingHistoryItem> toRet = new ArrayList<PublishingHistoryItem>();
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository publishedRepo = helper.getRepository(siteId, PUBLISHED);
        if (publishedRepo != null) {
            int counter = 0;
            try (Git git = new Git(publishedRepo)) {
                // List all environments
                List<Ref> environments = git.branchList().call();
                for (int i = 0; i < environments.size() && counter < limit; i++) {
                    Ref env = environments.get(i);
                    String environmentGit = env.getName();
                    environmentGit = environmentGit.replace(R_HEADS, "");
                    if ((StringUtils.isBlank(environment) && !StringUtils.equals(MASTER, environmentGit)) || StringUtils.equals(environment, environmentGit)) {
                        List<RevFilter> filters = new ArrayList<RevFilter>();
                        if (fromDate != null) {
                            filters.add(CommitTimeRevFilter.after(fromDate.toInstant().toEpochMilli()));
                        }
                        if (toDate != null) {
                            filters.add(CommitTimeRevFilter.before(toDate.toInstant().toEpochMilli()));
                        } else {
                            filters.add(CommitTimeRevFilter.before(ZonedDateTime.now().toInstant().toEpochMilli()));
                        }
                        filters.add(NotRevFilter.create(MessageRevFilter.create("Initial commit.")));
                        if (StringUtils.isNotEmpty(publisher)) {
                            User user = userServiceInternal.getUserByIdOrUsername(-1, publisher);
                            filters.add(AuthorRevFilter.create(helper.getAuthorIdent(user).getName()));
                        }
                        Iterable<RevCommit> branchLog = git.log().add(env.getObjectId()).setRevFilter(AndRevFilter.create(filters)).call();
                        Iterator<RevCommit> iterator = branchLog.iterator();
                        while (iterator.hasNext() && counter < limit) {
                            RevCommit revCommit = iterator.next();
                            List<String> files = helper.getFilesInCommit(publishedRepo, revCommit);
                            for (int j = 0; j < files.size() && counter < limit; j++) {
                                String file = files.get(j);
                                Path path = Paths.get(file);
                                String fileName = path.getFileName().toString();
                                if (!ArrayUtils.contains(IGNORE_FILES, fileName)) {
                                    boolean addFile = false;
                                    if (StringUtils.isNotEmpty(pathRegex)) {
                                        Pattern pattern = Pattern.compile(pathRegex);
                                        Matcher matcher = pattern.matcher(file);
                                        addFile = matcher.matches();
                                    } else {
                                        addFile = true;
                                    }
                                    if (addFile) {
                                        PublishingHistoryItem phi = new PublishingHistoryItem();
                                        phi.setSiteId(siteId);
                                        phi.setPath(file);
                                        phi.setPublishedDate(Instant.ofEpochSecond(revCommit.getCommitTime()).atZone(UTC));
                                        phi.setPublisher(revCommit.getAuthorIdent().getName());
                                        phi.setEnvironment(environmentGit.replace(R_HEADS, ""));
                                        toRet.add(phi);
                                        counter++;
                                    }
                                }
                            }
                        }
                    }
                }
                git.close();
                toRet.sort((o1, o2) -> o2.getPublishedDate().compareTo(o1.getPublishedDate()));
            } catch (IOException | GitAPIException | UserNotFoundException | ServiceLayerException e1) {
                logger.error("Error while getting deployment history for site " + siteId, e1);
            }
        }
    } catch (CryptoException e) {
        e.printStackTrace();
    }
    return toRet;
}
Also used : UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) User(org.craftercms.studio.api.v2.dal.User) Matcher(java.util.regex.Matcher) PublishingHistoryItem(org.craftercms.studio.api.v2.dal.PublishingHistoryItem) ArrayList(java.util.ArrayList) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) CommitTimeRevFilter(org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter) MessageRevFilter(org.eclipse.jgit.revwalk.filter.MessageRevFilter) RevFilter(org.eclipse.jgit.revwalk.filter.RevFilter) AndRevFilter(org.eclipse.jgit.revwalk.filter.AndRevFilter) AuthorRevFilter(org.eclipse.jgit.revwalk.filter.AuthorRevFilter) NotRevFilter(org.eclipse.jgit.revwalk.filter.NotRevFilter) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) 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.v2.repository.ContentRepository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) CryptoException(org.craftercms.commons.crypto.CryptoException)

Aggregations

ArrayList (java.util.ArrayList)3 PublishingHistoryItem (org.craftercms.studio.api.v2.dal.PublishingHistoryItem)3 ItemMetadata (org.craftercms.studio.api.v1.dal.ItemMetadata)2 PublishingDashboardItem (org.craftercms.studio.model.rest.dashboard.PublishingDashboardItem)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 CryptoException (org.craftercms.commons.crypto.CryptoException)1 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)1 UserNotFoundException (org.craftercms.studio.api.v1.exception.security.UserNotFoundException)1 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)1 User (org.craftercms.studio.api.v2.dal.User)1 ContentRepository (org.craftercms.studio.api.v2.repository.ContentRepository)1 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)1 Git (org.eclipse.jgit.api.Git)1 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)1 Ref (org.eclipse.jgit.lib.Ref)1 Repository (org.eclipse.jgit.lib.Repository)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1