Search in sources :

Example 1 with DmFilterWrapper

use of org.craftercms.studio.api.v1.util.filter.DmFilterWrapper in project studio by craftercms.

the class DeploymentServiceImpl method getDeploymentHistory.

@Override
@ValidateParams
public List<DmDeploymentTaskTO> getDeploymentHistory(@ValidateStringParam(name = "site") String site, @ValidateIntegerParam(name = "daysFromToday") int daysFromToday, @ValidateIntegerParam(name = "numberOfItems") int numberOfItems, @ValidateStringParam(name = "sort") String sort, boolean ascending, @ValidateStringParam(name = "filterType") String filterType) throws SiteNotFoundException {
    // get the filtered list of attempts in a specific date range
    ZonedDateTime toDate = ZonedDateTime.now(ZoneOffset.UTC);
    ZonedDateTime fromDate = toDate.minusDays(daysFromToday);
    List<DeploymentSyncHistory> deployReports = deploymentHistoryProvider.getDeploymentHistory(site, getEnvironmentNames(site), fromDate, toDate, dmFilterWrapper, filterType, numberOfItems);
    List<DmDeploymentTaskTO> tasks = new ArrayList<DmDeploymentTaskTO>();
    if (deployReports != null) {
        int count = 0;
        String timezone = servicesConfig.getDefaultTimezone(site);
        Map<String, Set<String>> processedItems = new HashMap<String, Set<String>>();
        for (int index = 0; index < deployReports.size() && count < numberOfItems; index++) {
            DeploymentSyncHistory entry = deployReports.get(index);
            String env = entry.getEnvironment();
            if (!processedItems.containsKey(env)) {
                processedItems.put(env, new HashSet<String>());
            }
            if (!processedItems.get(env).contains(entry.getPath())) {
                ContentItemTO deployedItem = getDeployedItem(entry.getSite(), entry.getPath());
                if (deployedItem != null) {
                    deployedItem.eventDate = entry.getSyncDate();
                    deployedItem.endpoint = entry.getTarget();
                    deployedItem.setUser(entry.getUser());
                    deployedItem.setEndpoint(entry.getEnvironment());
                    String deployedLabel = entry.getSyncDate().withZoneSameInstant(ZoneId.of(timezone)).format(ISO_OFFSET_DATE);
                    if (tasks.size() > 0) {
                        DmDeploymentTaskTO lastTask = tasks.get(tasks.size() - 1);
                        String lastDeployedLabel = lastTask.getInternalName();
                        if (lastDeployedLabel.equals(deployedLabel)) {
                            // add to the last task if it is deployed on the same day
                            lastTask.setNumOfChildren(lastTask.getNumOfChildren() + 1);
                            lastTask.getChildren().add(deployedItem);
                        } else {
                            tasks.add(createDeploymentTask(deployedLabel, deployedItem));
                        }
                    } else {
                        tasks.add(createDeploymentTask(deployedLabel, deployedItem));
                    }
                    processedItems.get(env).add(entry.getPath());
                }
            }
        }
    }
    return tasks;
}
Also used : DmDeploymentTaskTO(org.craftercms.studio.api.v1.to.DmDeploymentTaskTO) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) DeploymentSyncHistory(org.craftercms.studio.api.v1.dal.DeploymentSyncHistory) ArrayList(java.util.ArrayList) FastArrayList(org.apache.commons.collections.FastArrayList) ContentItemTO(org.craftercms.studio.api.v1.to.ContentItemTO) ZonedDateTime(java.time.ZonedDateTime) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 2 with DmFilterWrapper

use of org.craftercms.studio.api.v1.util.filter.DmFilterWrapper in project studio by craftercms.

the class GitContentRepository method getDeploymentHistory.

@Override
public List<DeploymentSyncHistory> getDeploymentHistory(String site, List<String> environmentNames, ZonedDateTime fromDate, ZonedDateTime toDate, DmFilterWrapper dmFilterWrapper, String filterType, int numberOfItems) {
    List<DeploymentSyncHistory> toRet = new ArrayList<DeploymentSyncHistory>();
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository publishedRepo = helper.getRepository(site, PUBLISHED);
        if (Objects.nonNull(publishedRepo)) {
            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 < numberOfItems; i++) {
                    Ref env = environments.get(i);
                    String environment = env.getName();
                    environment = environment.replace(R_HEADS, "");
                    if (environmentNames.contains(environment)) {
                        List<RevFilter> filters = new ArrayList<RevFilter>();
                        filters.add(CommitTimeRevFilter.after(fromDate.toInstant().toEpochMilli()));
                        filters.add(CommitTimeRevFilter.before(toDate.toInstant().toEpochMilli()));
                        filters.add(NotRevFilter.create(MessageRevFilter.create("Initial commit.")));
                        Iterable<RevCommit> branchLog = git.log().add(env.getObjectId()).setRevFilter(AndRevFilter.create(filters)).call();
                        Iterator<RevCommit> iterator = branchLog.iterator();
                        while (iterator.hasNext() && counter < numberOfItems) {
                            RevCommit revCommit = iterator.next();
                            List<String> files = helper.getFilesInCommit(publishedRepo, revCommit);
                            for (int j = 0; j < files.size() && counter < numberOfItems; j++) {
                                String file = files.get(j);
                                Path path = Paths.get(file);
                                String fileName = path.getFileName().toString();
                                if (!ArrayUtils.contains(IGNORE_FILES, fileName)) {
                                    if (dmFilterWrapper.accept(site, file, filterType)) {
                                        DeploymentSyncHistory dsh = new DeploymentSyncHistory();
                                        dsh.setSite(site);
                                        dsh.setPath(file);
                                        dsh.setSyncDate(Instant.ofEpochSecond(revCommit.getCommitTime()).atZone(UTC));
                                        dsh.setUser(revCommit.getAuthorIdent().getName());
                                        dsh.setEnvironment(environment.replace(R_HEADS, ""));
                                        toRet.add(dsh);
                                        counter++;
                                    }
                                }
                            }
                        }
                    }
                }
                git.close();
                toRet.sort((o1, o2) -> o2.getSyncDate().compareTo(o1.getSyncDate()));
            }
        }
    } catch (CryptoException | IOException | GitAPIException e) {
        logger.error("Error while getting deployment history for site " + site, e);
    }
    return toRet;
}
Also used : Path(java.nio.file.Path) DeploymentSyncHistory(org.craftercms.studio.api.v1.dal.DeploymentSyncHistory) ArrayList(java.util.ArrayList) IOException(java.io.IOException) 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.v2.repository.ContentRepository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) 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) CryptoException(org.craftercms.commons.crypto.CryptoException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ArrayList (java.util.ArrayList)2 DeploymentSyncHistory (org.craftercms.studio.api.v1.dal.DeploymentSyncHistory)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ZonedDateTime (java.time.ZonedDateTime)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 FastArrayList (org.apache.commons.collections.FastArrayList)1 CryptoException (org.craftercms.commons.crypto.CryptoException)1 ValidateParams (org.craftercms.commons.validation.annotations.param.ValidateParams)1 ContentItemTO (org.craftercms.studio.api.v1.to.ContentItemTO)1 DmDeploymentTaskTO (org.craftercms.studio.api.v1.to.DmDeploymentTaskTO)1 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)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