Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse labels from tags using naming convention #2326

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading