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;
}
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;
}
Aggregations