Skip to content

Commit

Permalink
add support for executors report name
Browse files Browse the repository at this point in the history
  • Loading branch information
baev committed Jan 15, 2024
1 parent ea1586f commit 6c4d1a0
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.qameta.allure.CommonJsonAggregator2;
import io.qameta.allure.Constants;
import io.qameta.allure.core.LaunchResults;
import io.qameta.allure.executor.ExecutorPlugin;
import io.qameta.allure.trend.AbstractTrendPlugin;

import java.util.ArrayList;
Expand Down Expand Up @@ -73,7 +74,7 @@ private static List<CategoriesTrendItem> getPreviousTrendData(final LaunchResult

private static CategoriesTrendItem createCurrent(final List<LaunchResults> launchesResults) {
final CategoriesTrendItem item = new CategoriesTrendItem();
extractLatestExecutor(launchesResults).ifPresent(info -> {
ExecutorPlugin.getLatestExecutor(launchesResults).ifPresent(info -> {
item.setBuildOrder(info.getBuildOrder());
item.setReportName(info.getReportName());
item.setReportUrl(info.getReportUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.qameta.allure.CommonJsonAggregator2;
import io.qameta.allure.Constants;
import io.qameta.allure.core.LaunchResults;
import io.qameta.allure.executor.ExecutorPlugin;
import io.qameta.allure.trend.AbstractTrendPlugin;

import java.util.ArrayList;
Expand Down Expand Up @@ -74,7 +75,7 @@ private static List<DurationTrendItem> getPreviousTrendData(final LaunchResults

private static DurationTrendItem createCurrent(final List<LaunchResults> launchesResults) {
final DurationTrendItem item = new DurationTrendItem();
extractLatestExecutor(launchesResults).ifPresent(info -> {
ExecutorPlugin.getLatestExecutor(launchesResults).ifPresent(info -> {
item.setBuildOrder(info.getBuildOrder());
item.setReportName(info.getReportName());
item.setReportUrl(info.getReportUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,27 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.nullsFirst;

/**
* @author charlie (Dmitry Baev).
*/
public class ExecutorPlugin extends CommonJsonAggregator2 implements Reader {

public static final String EXECUTORS_BLOCK_NAME = "executor";

protected static final String JSON_FILE_NAME = "executor.json";

private static final Comparator<ExecutorInfo> COMPARATOR
= comparing(ExecutorInfo::getBuildOrder, nullsFirst(naturalOrder()));

public ExecutorPlugin() {
super(Constants.WIDGETS_DIR, "executors.json");
}
Expand Down Expand Up @@ -70,4 +79,14 @@ protected List<ExecutorInfo> getData(final List<LaunchResults> launches) {
.map(ExecutorInfo.class::cast)
.collect(Collectors.toList());
}

public static Optional<ExecutorInfo> getLatestExecutor(final List<LaunchResults> launches) {
return launches.stream()
.map(launch -> launch.getExtra(EXECUTORS_BLOCK_NAME))
.filter(Optional::isPresent)
.map(Optional::get)
.filter(ExecutorInfo.class::isInstance)
.map(ExecutorInfo.class::cast)
.max(COMPARATOR);
}
}
26 changes: 9 additions & 17 deletions allure-generator/src/main/java/io/qameta/allure/ga/GaPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.qameta.allure.entity.ExecutorInfo;
import io.qameta.allure.entity.Label;
import io.qameta.allure.entity.LabelName;
import io.qameta.allure.executor.ExecutorPlugin;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
Expand Down Expand Up @@ -52,7 +53,6 @@
import java.util.stream.Collectors;

import static io.qameta.allure.Constants.NO_ANALYTICS;
import static io.qameta.allure.executor.ExecutorPlugin.EXECUTORS_BLOCK_NAME;

/**
* @author charlie (Dmitry Baev).
Expand Down Expand Up @@ -133,17 +133,14 @@ protected void sendStats(final String clientId, final GaParameters parameters) {
}

private static String getClientId(final List<LaunchResults> launchesResults) {
final Optional<String> executorHostName = launchesResults.stream()
.map(results -> results.<ExecutorInfo>getExtra(EXECUTORS_BLOCK_NAME))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
return ExecutorPlugin.getLatestExecutor(launchesResults)
.map(ExecutorInfo::getBuildUrl)
.flatMap(GaPlugin::getHostSafe);

return executorHostName.map(DigestUtils::sha256Hex)
.orElse(getLocalHostName().map(DigestUtils::sha256Hex)
.orElse(UUID.randomUUID().toString()));
.flatMap(GaPlugin::getHostSafe)
.map(DigestUtils::sha256Hex)
.orElseGet(() -> getLocalHostName()
.map(DigestUtils::sha256Hex)
.orElse(UUID.randomUUID().toString())
);
}

private static Optional<String> getHostSafe(final String url) {
Expand All @@ -158,12 +155,7 @@ private static Optional<String> getHostSafe(final String url) {

/* package-private */
static String getExecutorType(final List<LaunchResults> launchesResults) {
return launchesResults.stream()
.map(results -> results.<ExecutorInfo>getExtra(EXECUTORS_BLOCK_NAME))
.filter(Optional::isPresent)
.findFirst()
.filter(Optional::isPresent)
.map(Optional::get)
return ExecutorPlugin.getLatestExecutor(launchesResults)
.map(ExecutorInfo::getType)
.map(String::trim)
.map(String::toLowerCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.qameta.allure.core.LaunchResults;
import io.qameta.allure.entity.Statistic;
import io.qameta.allure.entity.TestResult;
import io.qameta.allure.executor.ExecutorPlugin;
import io.qameta.allure.trend.AbstractTrendPlugin;

import java.util.ArrayList;
Expand Down Expand Up @@ -76,7 +77,7 @@ private static HistoryTrendItem createCurrent(final List<LaunchResults> launches
.collect(Statistic::new, Statistic::update, Statistic::merge);
final HistoryTrendItem item = new HistoryTrendItem()
.setStatistic(statistic);
extractLatestExecutor(launchesResults).ifPresent(info -> {
ExecutorPlugin.getLatestExecutor(launchesResults).ifPresent(info -> {
item.setBuildOrder(info.getBuildOrder());
item.setReportName(info.getReportName());
item.setReportUrl(info.getReportUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.qameta.allure.CommonJsonAggregator2;
import io.qameta.allure.Constants;
import io.qameta.allure.core.LaunchResults;
import io.qameta.allure.executor.ExecutorPlugin;
import io.qameta.allure.trend.AbstractTrendPlugin;

import java.util.ArrayList;
Expand Down Expand Up @@ -74,7 +75,7 @@ private static List<RetryTrendItem> getPreviousTrendData(final LaunchResults res

private static RetryTrendItem createCurrent(final List<LaunchResults> launchesResults) {
final RetryTrendItem item = new RetryTrendItem();
extractLatestExecutor(launchesResults).ifPresent(info -> {
ExecutorPlugin.getLatestExecutor(launchesResults).ifPresent(info -> {
item.setBuildOrder(info.getBuildOrder());
item.setReportName(info.getReportName());
item.setReportUrl(info.getReportUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
import io.qameta.allure.ReportStorage;
import io.qameta.allure.core.Configuration;
import io.qameta.allure.core.LaunchResults;
import io.qameta.allure.entity.ExecutorInfo;
import io.qameta.allure.entity.GroupTime;
import io.qameta.allure.entity.Statistic;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static io.qameta.allure.executor.ExecutorPlugin.getLatestExecutor;

/**
* Plugins generates Summary widget.
Expand All @@ -45,15 +49,30 @@ public void aggregate(final Configuration configuration,
final SummaryData data1 = new SummaryData()
.setStatistic(new Statistic())
.setTime(new GroupTime())
.setReportName(configuration.getReportName());
.setReportName(getReportName(configuration, launchesResults));

launchesResults.stream()
.map(LaunchResults::getResults)
.flatMap(Collection::stream)
.forEach(result -> {
data1.getStatistic().update(result);
data1.getTime().update(result);
});

storage.addDataJson(String.format("%s/%s", Constants.WIDGETS_DIR, JSON_FILE_NAME), data1);
}

private static String getReportName(final Configuration configuration,
final List<LaunchResults> launchesResults) {
final String reportName = configuration.getReportName();
if (Objects.nonNull(reportName)) {
return reportName;
}

return getLatestExecutor(launchesResults)
.map(ExecutorInfo::getReportName)
.map(String::trim)
.orElse(Constants.DEFAULT_REPORT_NAME);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.qameta.allure.core.LaunchResults;
import io.qameta.allure.core.ResultsVisitor;
import io.qameta.allure.entity.ExecutorInfo;
import io.qameta.allure.executor.ExecutorPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,17 +36,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Spliterator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.qameta.allure.executor.ExecutorPlugin.EXECUTORS_BLOCK_NAME;
import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.nullsFirst;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.StreamSupport.stream;

Expand Down Expand Up @@ -114,14 +110,15 @@ private Optional<T> parseItem(final Path historyFile, final ObjectMapper mapper,

protected abstract Optional<T> parseItem(ObjectMapper mapper, JsonNode child) throws JsonProcessingException;

/**
* Deprecated, use {@link ExecutorPlugin#getLatestExecutor(List)} instead.
*
* @param launches the launch results.
* @return the latest executor
* @deprecated use {@link ExecutorPlugin#getLatestExecutor(List)} instead.
*/
@Deprecated
protected static Optional<ExecutorInfo> extractLatestExecutor(final List<LaunchResults> launches) {
final Comparator<ExecutorInfo> comparator = comparing(ExecutorInfo::getBuildOrder, nullsFirst(naturalOrder()));
return launches.stream()
.map(launch -> launch.getExtra(EXECUTORS_BLOCK_NAME))
.filter(Optional::isPresent)
.map(Optional::get)
.filter(ExecutorInfo.class::isInstance)
.map(ExecutorInfo.class::cast)
.max(comparator);
return ExecutorPlugin.getLatestExecutor(launches);
}
}
Loading

0 comments on commit 6c4d1a0

Please sign in to comment.