From 3510ccdc3753ab6ecca794b68d5d7192d28921cf Mon Sep 17 00:00:00 2001 From: Artem Eroshenko Date: Mon, 21 Mar 2022 14:21:23 +0300 Subject: [PATCH] add possibility to change status color (via #23) --- build.gradle.kts | 3 ++ .../allure/AllurePDFGenerator.java | 7 ++-- .../eroshenkoam/allure/MainCommand.java | 15 ++++++-- .../eroshenkoam/allure/StatusColors.java | 35 +++++++++++++++++++ .../allure/option/StatusColorOptions.java | 24 +++++++++++++ .../eroshenkoam/allure/util/ColorUtils.java | 31 ---------------- 6 files changed, 79 insertions(+), 36 deletions(-) create mode 100644 src/main/java/io/github/eroshenkoam/allure/StatusColors.java create mode 100644 src/main/java/io/github/eroshenkoam/allure/option/StatusColorOptions.java delete mode 100644 src/main/java/io/github/eroshenkoam/allure/util/ColorUtils.java diff --git a/build.gradle.kts b/build.gradle.kts index 06e432e..4df9994 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -24,6 +24,9 @@ repositories { dependencies { annotationProcessor("info.picocli:picocli-codegen:4.1.4") + annotationProcessor("org.projectlombok:lombok:1.18.12") + + compileOnly("org.projectlombok:lombok:1.18.12") implementation("com.fasterxml.jackson.core:jackson-databind:2.10.2") implementation("org.apache.commons:commons-collections4:4.4") diff --git a/src/main/java/io/github/eroshenkoam/allure/AllurePDFGenerator.java b/src/main/java/io/github/eroshenkoam/allure/AllurePDFGenerator.java index cb6c07b..b6e9841 100644 --- a/src/main/java/io/github/eroshenkoam/allure/AllurePDFGenerator.java +++ b/src/main/java/io/github/eroshenkoam/allure/AllurePDFGenerator.java @@ -35,7 +35,6 @@ import java.util.stream.Collectors; import static io.github.eroshenkoam.allure.FontHolder.loadArialFont; -import static io.github.eroshenkoam.allure.util.ColorUtils.statusTextColor; import static io.github.eroshenkoam.allure.util.PdfUtil.addEmptyLine; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.joining; @@ -48,11 +47,13 @@ public class AllurePDFGenerator { private final String reportName; private final Path reportPath; private final Map filter; + private final StatusColors statusColors; - public AllurePDFGenerator(final String reportName, final Path reportPath) { + public AllurePDFGenerator(final String reportName, final Path reportPath, final StatusColors statusColors) { this.filter = new HashMap<>(); this.reportName = reportName; this.reportPath = reportPath; + this.statusColors = statusColors; } public void filter(final Map tags) { @@ -171,7 +172,7 @@ private void addSteps(final TestResult testResult, final FontHolder fontHolder, private com.lowagie.text.List createStepsList(final List steps, final FontHolder fontHolder) { final com.lowagie.text.List stepList = new com.lowagie.text.List(true); steps.forEach(step -> { - final Font font = fontHolder.normal(statusTextColor(step.getStatus())); + final Font font = fontHolder.normal(statusColors.getStatusColor(step.getStatus())); final ListItem stepItem = new ListItem(String.format("%s", step.getName()), font); final StatusDetails statusDetails = step.getStatusDetails(); if (Objects.nonNull(statusDetails) && Objects.nonNull(statusDetails.getMessage())) { diff --git a/src/main/java/io/github/eroshenkoam/allure/MainCommand.java b/src/main/java/io/github/eroshenkoam/allure/MainCommand.java index 4f8f90c..007c205 100644 --- a/src/main/java/io/github/eroshenkoam/allure/MainCommand.java +++ b/src/main/java/io/github/eroshenkoam/allure/MainCommand.java @@ -1,5 +1,7 @@ package io.github.eroshenkoam.allure; +import io.github.eroshenkoam.allure.option.StatusColorOptions; +import io.qameta.allure.model.Status; import picocli.CommandLine; import java.io.IOException; @@ -32,12 +34,21 @@ public class MainCommand implements Runnable { protected String reportName; @CommandLine.Option(names = {"-f", "--filter"}) - Map filter; + protected Map filter; + + @CommandLine.ArgGroup + protected StatusColorOptions statusColorOptions = new StatusColorOptions(); @Override public void run() { try { - final AllurePDFGenerator generator = new AllurePDFGenerator(reportName, reportPath); + final StatusColors statusColors = new StatusColors(); + statusColors.setStatusColors(Status.PASSED, statusColorOptions.getPassed()); + statusColors.setStatusColors(Status.FAILED, statusColorOptions.getFailed()); + statusColors.setStatusColors(Status.BROKEN, statusColorOptions.getBroken()); + statusColors.setStatusColors(Status.SKIPPED, statusColorOptions.getSkipped()); + + final AllurePDFGenerator generator = new AllurePDFGenerator(reportName, reportPath, statusColors); generator.filter(filter); generator.generate(outputPath); ; diff --git a/src/main/java/io/github/eroshenkoam/allure/StatusColors.java b/src/main/java/io/github/eroshenkoam/allure/StatusColors.java new file mode 100644 index 0000000..07ce411 --- /dev/null +++ b/src/main/java/io/github/eroshenkoam/allure/StatusColors.java @@ -0,0 +1,35 @@ +package io.github.eroshenkoam.allure; + +import io.qameta.allure.model.Status; + +import java.awt.*; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class StatusColors { + + private final Map statusColors; + + public StatusColors() { + statusColors = new HashMap() {{ + put(Status.PASSED, "#97cc64"); + put(Status.FAILED, "#fd5a3e"); + put(Status.BROKEN, "#ffd050"); + put(Status.SKIPPED, "#aaaaaa"); + }}; + } + + public void setStatusColors(final Status status, String color) { + statusColors.put(status, color); + } + + public Color getStatusColor(final Status status) { + final String hex = Optional.ofNullable(status) + .map(statusColors::get) + .orElse("#BF98A6FF"); + return Color.decode(hex); + } + + +} diff --git a/src/main/java/io/github/eroshenkoam/allure/option/StatusColorOptions.java b/src/main/java/io/github/eroshenkoam/allure/option/StatusColorOptions.java new file mode 100644 index 0000000..b740a67 --- /dev/null +++ b/src/main/java/io/github/eroshenkoam/allure/option/StatusColorOptions.java @@ -0,0 +1,24 @@ +package io.github.eroshenkoam.allure.option; + +import lombok.Data; +import lombok.experimental.Accessors; +import picocli.CommandLine; + +import java.io.Serializable; + +@Data +@Accessors(chain = true) +public class StatusColorOptions implements Serializable { + + private static final long serialVersionUID = 1L; + + @CommandLine.Option(names = "--status.color.passed", defaultValue = "#97cc64") + private String passed; + @CommandLine.Option(names = "--status.color.failed", defaultValue = "#fd5a3e") + private String failed; + @CommandLine.Option(names = "--status.color.broken", defaultValue = "#ffd050") + private String broken; + @CommandLine.Option(names = "--status.color.skipped", defaultValue = "#aaaaaa") + private String skipped; + +} diff --git a/src/main/java/io/github/eroshenkoam/allure/util/ColorUtils.java b/src/main/java/io/github/eroshenkoam/allure/util/ColorUtils.java deleted file mode 100644 index b67a6ee..0000000 --- a/src/main/java/io/github/eroshenkoam/allure/util/ColorUtils.java +++ /dev/null @@ -1,31 +0,0 @@ -package io.github.eroshenkoam.allure.util; - -import io.qameta.allure.model.Status; - -import java.awt.Color; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -public final class ColorUtils { - - public static final Color HEADER_COLOR = new Color(236, 239, 241); - - private static final Map STATUS_TEXT_COLORS = new HashMap() {{ - put(Status.PASSED, new Color(120, 182, 60)); - put(Status.FAILED, new Color(255, 38, 2)); - put(Status.BROKEN, new Color(254, 190, 13)); - put(Status.SKIPPED, new Color(136, 136, 136)); - }}; - - private ColorUtils() { - throw new IllegalStateException("Do not instance"); - } - - public static Color statusTextColor(final Status status) { - return Optional.ofNullable(status) - .map(STATUS_TEXT_COLORS::get) - .orElseGet(() -> new Color(191, 152, 166)); - } - -}