-
Notifications
You must be signed in to change notification settings - Fork 64
/
CustomReportMetaPlugin.java
86 lines (69 loc) · 3.48 KB
/
CustomReportMetaPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package ru.iopump.qa.allure.helper.plugin;
import io.qameta.allure.core.LaunchResults;
import io.qameta.allure.summary.SummaryData;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Objects;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.CREATE_NEW;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static ru.iopump.qa.allure.helper.Util.MAPPER;
@Slf4j
public class CustomReportMetaPlugin implements AllureServerPlugin {
private final static String LOGO_FILE = "logo.png";
private final static String LOGO_DIR = "plugin/custom-logo";
private final static String LOGO_STYLE_FILE = "styles.css";
private final static String SUMMARY_DIR = "widgets/summary.json";
@Override
public void onGenerationStart(Collection<Path> resultsDirectories, Context context) {
}
@SneakyThrows
@Override
public void onGenerationFinish(Path reportDirectory, Collection<LaunchResults> launchResults, Context context) {
var logo = context.getAllureProperties().logo();
if (logo != null) {
var bytes = logo.isReadable() ? logo.getContentAsByteArray() : null;
if (bytes != null) {
//noinspection resource
var customLogoDirectory = Files
.find(reportDirectory, 3, (path, basicFileAttributes) -> basicFileAttributes.isDirectory() && path.toString().endsWith(LOGO_DIR))
.findFirst()
.orElseThrow(() -> new InternalError("Custom logo plugin directory not found..."));
var logoName = Objects.requireNonNullElse(logo.getFilename(), LOGO_FILE);
var customLogoPath = customLogoDirectory.resolve(logoName);
var customLogoCssPath = customLogoDirectory.resolve(LOGO_STYLE_FILE);
Files.write(customLogoPath, bytes);
var cssForNewLogo = """
.side-nav__brand {
background: url('img.png') no-repeat left center !important;
padding-left: 200px !important;
margin-left: 10px;
}
""";
cssForNewLogo = cssForNewLogo.replace("img.png", logoName);
Files.writeString(customLogoCssPath, cssForNewLogo, UTF_8, Files.exists(customLogoCssPath) ? TRUNCATE_EXISTING : CREATE_NEW);
log.info("{}: {} copied to {}", getName(), logoName, customLogoPath);
}
}
var title = context.getAllureProperties().title();
if (title != null) {
//noinspection resource
var summaryPath = Files
.find(reportDirectory, 3, (path, basicFileAttributes) -> basicFileAttributes.isRegularFile() && path.toString().endsWith(SUMMARY_DIR))
.findFirst()
.orElseThrow(() -> new InternalError("Summary file not found..."));
var summaryData = MAPPER.readValue(summaryPath.toFile(), SummaryData.class);
summaryData.setReportName(title);
var newSummary = MAPPER.writeValueAsString(summaryData);
Files.writeString(summaryPath, newSummary, UTF_8, TRUNCATE_EXISTING);
log.info("{}: Summary file updated with new title: {}", getName(), title);
}
}
@Override
public String getName() {
return "Logo Plugin";
}
}