Skip to content

Commit

Permalink
parse labels from tags using naming convention (via #2326)
Browse files Browse the repository at this point in the history
  • Loading branch information
baev authored Jan 29, 2024
1 parent d945c5a commit dbc3f54
Show file tree
Hide file tree
Showing 4 changed files with 370 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public ConfigurationBuilder useDefault() {
new FreemarkerContext(),
new RandomUidContext(),
new MarkdownDescriptionsPlugin(),
new TagsPlugin(),
new RetryPlugin(),
new RetryTrendPlugin(),
new TagsPlugin(),
new SeverityPlugin(),
new OwnerPlugin(),
new IdeaLinksPlugin(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public final class DummyReportGenerator {
new FreemarkerContext(),
new RandomUidContext(),
new MarkdownDescriptionsPlugin(),
new TagsPlugin(),
new RetryPlugin(),
new RetryTrendPlugin(),
new TagsPlugin(),
new SeverityPlugin(),
new OwnerPlugin(),
new IdeaLinksPlugin(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,27 @@
import io.qameta.allure.ReportStorage;
import io.qameta.allure.core.Configuration;
import io.qameta.allure.core.LaunchResults;
import io.qameta.allure.entity.Label;
import io.qameta.allure.entity.LabelName;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* @author charlie (Dmitry Baev).
*/
public class TagsPlugin implements Aggregator2 {

private static final Pattern LABEL_TAG = Pattern.compile("^@?allure\\.label\\.(?<name>.+)[:=](?<value>.+)$");

public static final String TAGS_BLOCK_NAME = "tags";

@Override
Expand All @@ -41,8 +50,43 @@ public void aggregate(final Configuration configuration,
.map(LaunchResults::getAllResults)
.flatMap(Collection::stream)
.forEach(result -> {
final Set<String> tags = new HashSet<>(result.findAllLabels(LabelName.TAG));
result.addExtraBlock(TAGS_BLOCK_NAME, tags);

final Set<String> tags = result.findAllLabels(LabelName.TAG, Collectors.toSet());

final List<Label> extraLabels = tags.stream()
.filter(Objects::nonNull)
.filter(tag -> LABEL_TAG.matcher(tag).matches())
.map(TagsPlugin::createFromTag)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());

if (!extraLabels.isEmpty()) {
final List<Label> labels = new ArrayList<>(result.getLabels());
labels.addAll(extraLabels);
result.setLabels(labels);
}

final Set<String> filteredTags = tags.stream()
.filter(Objects::nonNull)
.filter(tag -> !LABEL_TAG.matcher(tag).matches())
.map(String::trim)
.filter(s -> !"".equals(s))
.collect(Collectors.toSet());

result.addExtraBlock(TAGS_BLOCK_NAME, new HashSet<>(filteredTags));
});
}

public static Optional<Label> createFromTag(final String tag) {
final Matcher label = LABEL_TAG.matcher(tag);
if (label.matches()) {
final String name = label.group("name");
final String value = label.group("value");
return Optional.of(new Label()
.setName(name)
.setValue(value.replace("_", " ")));
}
return Optional.empty();
}
}
Loading

0 comments on commit dbc3f54

Please sign in to comment.