diff --git a/src/main/java/io/qameta/allure/bamboo/AllureArtifactsManager.java b/src/main/java/io/qameta/allure/bamboo/AllureArtifactsManager.java index 34ba725..3e2fd4f 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureArtifactsManager.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureArtifactsManager.java @@ -49,6 +49,7 @@ import com.atlassian.sal.api.ApplicationProperties; import com.atlassian.sal.api.UrlMode; import com.google.common.collect.ImmutableList; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.apache.tools.ant.types.FileSet; import org.jetbrains.annotations.NotNull; @@ -61,11 +62,14 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Optional; @@ -88,14 +92,15 @@ import static java.util.Objects.requireNonNull; import static java.util.stream.Collectors.toMap; import static javax.ws.rs.core.UriBuilder.fromPath; -import static org.apache.commons.io.FileUtils.deleteQuietly; -import static org.apache.commons.io.FileUtils.forceMkdir; -import static org.apache.commons.io.FileUtils.moveDirectory; import static org.apache.commons.lang3.StringUtils.isEmpty; import static org.codehaus.plexus.util.FileUtils.copyDirectory; import static org.codehaus.plexus.util.FileUtils.copyURLToFile; -@SuppressWarnings({"ClassDataAbstractionCoupling", "PMD.AvoidInstantiatingObjectsInLoops", "PMD.GodClass"}) +@SuppressWarnings({ + "ClassDataAbstractionCoupling", + "PMD.AvoidInstantiatingObjectsInLoops", + "PMD.GodClass" +}) public class AllureArtifactsManager { private static final Logger LOGGER = LoggerFactory.getLogger(AllureArtifactsManager.class); @@ -181,7 +186,7 @@ private String getLocalStorageURL(final String planKeyString, final String buildNumber, final String filePath) { try { - final File file = getLocalStoragePath(planKeyString, buildNumber).resolve(filePath).toFile(); + final File file = getLocalStorageReportPath(planKeyString, buildNumber).resolve(filePath).toFile(); final String fullPath = (file.isDirectory()) ? new File(file, INDEX_HTML).getAbsolutePath() : file.getAbsolutePath(); return new File(fullPath).toURI().toURL().toString(); @@ -215,7 +220,7 @@ Collection downloadAllArtifactsTo(final @NotNull ChainResultsSummary chain artifact.getLabel(), artifactName, chainResultsSummary.getPlanKey(), chainResultsSummary.getBuildNumber()); final File stageDir = new File(baseDir, UUID.randomUUID().toString()); - forceMkdir(stageDir); + FileUtils.forceMkdir(stageDir); resultsPaths.add(stageDir.toPath()); final ArtifactLinkDataProvider dataProvider = artifactLinkManager.getArtifactLinkDataProvider(artifact); @@ -304,11 +309,11 @@ Optional uploadReportArtifacts(final @NotNull ImmutableChain if (isAgentArtifactHandler(artifactHandler)) { final String planKey = chain.getPlanKey().getKey(); final String buildNumber = String.valueOf(summary.getBuildNumber()); - final File destDir = getLocalStoragePath(planKey, buildNumber).toFile(); + final File destDir = getLocalStorageReportPath(planKey, buildNumber).toFile(); if (destDir.exists()) { - deleteQuietly(destDir); + FileUtils.deleteQuietly(destDir); } - moveDirectory(reportDir, destDir); + FileUtils.moveDirectory(reportDir, destDir); return Optional.of(allureBuildResult(true, null) .withHandlerClass(artifactHandler.getClass().getName())); } @@ -344,10 +349,43 @@ public ArtifactHandlerPublishingResult call() { return Optional.empty(); } - private Path getLocalStoragePath(final String planKey, final String buildNumber) { + void cleanupOldReportArtifacts(final @NotNull ImmutableChain chain, + final Integer maxStoredReportsCount) { + if (maxStoredReportsCount == null || maxStoredReportsCount <= 0) { + return; + } + for (final ArtifactHandler artifactHandler : getArtifactHandlers()) { + if (!isAgentArtifactHandler(artifactHandler)) { + continue; + } + final String planKey = chain.getPlanKey().getKey(); + final Path reports = getLocalStoragePlanReportsPath(planKey); + final List buildNumbers = new ArrayList<>(); + try (DirectoryStream ds = Files.newDirectoryStream(reports, Files::isDirectory)) { + for (Path p : ds) { + if (!StringUtils.isNumeric(p.getFileName().toString())) { + continue; + } + buildNumbers.add(Long.parseLong(p.getFileName().toString())); + } + } catch (Exception e) { + LOGGER.error("Failed to clean up old Allure Report", e); + } + buildNumbers.stream() + .sorted(Comparator.reverseOrder()) + .skip(maxStoredReportsCount) + .forEach(bn -> FileUtils.deleteQuietly(reports.resolve(bn.toString()).toFile())); + } + } + + private Path getLocalStorageReportPath(final String planKey, final String buildNumber) { return Paths.get(settingsManager.getSettings().getLocalStoragePath(), REPORTS_SUBDIR, planKey, buildNumber); } + private Path getLocalStoragePlanReportsPath(final String planKey) { + return Paths.get(settingsManager.getSettings().getLocalStoragePath(), REPORTS_SUBDIR, planKey); + } + private void logAndThrow(final Exception e, final String message) { LOGGER.error(message, e); diff --git a/src/main/java/io/qameta/allure/bamboo/AllureBuildCompleteAction.java b/src/main/java/io/qameta/allure/bamboo/AllureBuildCompleteAction.java index 3da0f84..5e29a60 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureBuildCompleteAction.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureBuildCompleteAction.java @@ -181,6 +181,11 @@ private void buildReport(final Path artifactsTempDir, .ifPresent(result -> result.dumpToCustomData(customBuildData)); } FileUtils.deleteQuietly(copyPath.toFile()); + if (globalConfig.isEnabledReportsCleanup() + && buildConfig.getMaxStoredReportsCount() != null + && buildConfig.getMaxStoredReportsCount() > 0) { + artifactsManager.cleanupOldReportArtifacts(chain, buildConfig.getMaxStoredReportsCount()); + } } catch (Exception e) { LOGGER.error("Failed to build allure report for {}", chain.getName(), e); allureBuildResult(false, stackTraceToString(e)).dumpToCustomData(customBuildData); diff --git a/src/main/java/io/qameta/allure/bamboo/AllureBuildConfig.java b/src/main/java/io/qameta/allure/bamboo/AllureBuildConfig.java index 9665fef..b358c96 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureBuildConfig.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureBuildConfig.java @@ -16,39 +16,53 @@ package io.qameta.allure.bamboo; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; import javax.annotation.Nullable; import java.io.Serializable; import java.util.Map; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_ARTIFACT_NAME; +import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_CUSTOM_LOGO_PATH; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_ENABLED; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_EXECUTABLE; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_FAILED_ONLY; -import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CUSTOM_LOGO_PATH; +import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_MAX_STORED_REPORTS_COUNT; import static java.lang.Boolean.FALSE; import static java.lang.Boolean.TRUE; import static java.util.Optional.ofNullable; public final class AllureBuildConfig implements Serializable { + + private static final long serialVersionUID = 1L; + + private static final String DEFAULT_ARTIFACT_NAME = "allure-results"; + private static final String DEFAULT_CUSTOM_LOGO_URL = "https://qameta.io/allure-report/img/reportlogo.svg"; + private final boolean onlyForFailed; private final String executable; private final boolean enabled; private final String artifactName; private final String logoUrl; - private static final String DEFAULT_ARTIFACT_NAME = "allure-results"; - public static final String DEFAULT_CUSTOM_LOGO_URL = "https://qameta.io/allure-report/img/reportlogo.svg"; + private final Integer maxStoredReportsCount; private AllureBuildConfig(final String executable, final String enabled, final String onlyForFailed, final String artifactName, - final String logoUrl) { - this.onlyForFailed = StringUtils.isEmpty(onlyForFailed) ? TRUE : Boolean.parseBoolean(onlyForFailed); - this.enabled = StringUtils.isEmpty(enabled) ? FALSE : Boolean.parseBoolean(enabled); + final String logoUrl, + final String maxStoredReportsCount) { + this.onlyForFailed = StringUtils.isBlank(onlyForFailed) + ? TRUE : Boolean.parseBoolean(onlyForFailed); + this.enabled = StringUtils.isBlank(enabled) + ? FALSE : Boolean.parseBoolean(enabled); this.executable = executable; this.artifactName = artifactName; - this.logoUrl = !logoUrl.isEmpty() ? logoUrl : AllureBuildConfig.DEFAULT_CUSTOM_LOGO_URL; + this.logoUrl = StringUtils.isBlank(logoUrl) + ? AllureBuildConfig.DEFAULT_CUSTOM_LOGO_URL : logoUrl; + this.maxStoredReportsCount = + StringUtils.isBlank(maxStoredReportsCount) || !StringUtils.isNumeric(maxStoredReportsCount) + ? -1 : NumberUtils.toInt(maxStoredReportsCount); } static AllureBuildConfig fromContext(final Map context) { @@ -57,7 +71,9 @@ static AllureBuildConfig fromContext(final Map context) { getSingleValue(context, ALLURE_CONFIG_ENABLED, FALSE.toString()), getSingleValue(context, ALLURE_CONFIG_FAILED_ONLY, FALSE.toString()), getSingleValue(context, ALLURE_CONFIG_ARTIFACT_NAME, AllureBuildConfig.DEFAULT_ARTIFACT_NAME), - getSingleValue(context, ALLURE_CUSTOM_LOGO_PATH, AllureBuildConfig.DEFAULT_CUSTOM_LOGO_URL)); + getSingleValue(context, ALLURE_CONFIG_CUSTOM_LOGO_PATH, AllureBuildConfig.DEFAULT_CUSTOM_LOGO_URL), + getSingleValue(context, ALLURE_CONFIG_MAX_STORED_REPORTS_COUNT, null) + ); } @Nullable @@ -93,4 +109,8 @@ public String getCustomLogoUrl() { return this.logoUrl; } + public Integer getMaxStoredReportsCount() { + return maxStoredReportsCount; + } + } diff --git a/src/main/java/io/qameta/allure/bamboo/AllureBuildResult.java b/src/main/java/io/qameta/allure/bamboo/AllureBuildResult.java index be1bf0c..81521d8 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureBuildResult.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureBuildResult.java @@ -25,6 +25,9 @@ import static org.apache.commons.lang3.StringUtils.isEmpty; class AllureBuildResult implements Serializable { + + private static final long serialVersionUID = 1L; + private final boolean success; private String artifactHandlerClass; private String failureDetails; diff --git a/src/main/java/io/qameta/allure/bamboo/AllureCommandLineSupport.java b/src/main/java/io/qameta/allure/bamboo/AllureCommandLineSupport.java index 087dcd8..e9085a1 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureCommandLineSupport.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureCommandLineSupport.java @@ -29,7 +29,9 @@ import static org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS; public class AllureCommandLineSupport { - private static final Pattern RESULT_TC_COUNT_REGEX = Pattern.compile(".+Found (\\d+) test cases.+", Pattern.DOTALL); + + private static final Pattern RESULT_TC_COUNT_REGEX + = Pattern.compile(".+Found (\\d+) test cases.+", Pattern.DOTALL); private static final int GENERATE_TIMEOUT_MS = (int) MINUTES.toMillis(10); String runCommand(final String cmd, final String... args) { diff --git a/src/main/java/io/qameta/allure/bamboo/AllureConstants.java b/src/main/java/io/qameta/allure/bamboo/AllureConstants.java index 73d4e35..f141f2c 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureConstants.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureConstants.java @@ -20,22 +20,10 @@ */ final class AllureConstants { - /** - * The name of artifact. - */ - static final String ALLURE_ARTIFACT_NAME = "Allure Report"; /** * The executable prefix. */ static final String ALLURE_EXECUTION_PREFIX = "system.builder.allure"; - /** - * The directory with allure results relative from build directory. - */ - static final String ALLURE_CONFIG_RESULTS_PATH = "custom.allure.config.results.path"; - /** - * The subdirectory (or subdirectories) to put generated report into. - */ - static final String ALLURE_CONFIG_REPORT_PATH = "custom.allure.config.report.path"; /** * The name of executable. */ @@ -47,7 +35,6 @@ final class AllureConstants { static final String ALLURE_CONFIG_ENABLED = "custom.allure.config.enabled"; static final String ALLURE_CONFIG_FAILED_ONLY = "custom.allure.config.failed.only"; static final String ALLURE_CONFIG_ARTIFACT_NAME = "custom.allure.artifact.name"; - static final String ALLURE_CONFIG_STORAGE_TYPE = "custom.allure.config.storage.type"; static final String ALLURE_CONFIG_DOWNLOAD_ENABLED = "custom.allure.config.download.enabled"; static final String ALLURE_CONFIG_ENABLED_BY_DEFAULT = "custom.allure.config.enabled.default"; static final String ALLURE_CONFIG_DOWNLOAD_URL = "custom.allure.config.download.url"; @@ -55,9 +42,14 @@ final class AllureConstants { static final String ALLURE_CONFIG_LOCAL_STORAGE = "custom.allure.config.local.storage"; // ALLURE CUSTOM LOGO - static final String ALLURE_CUSTOM_LOGO_ENABLED = "custom.allure.config.logo.enabled"; - static final String ALLURE_CUSTOM_LOGO_PATH = "custom.allure.logo.url"; + static final String ALLURE_CONFIG_CUSTOM_LOGO_ENABLED = "custom.allure.config.logo.enabled"; + static final String ALLURE_CONFIG_CUSTOM_LOGO_PATH = "custom.allure.logo.url"; + + // ALLURE CLEANUP OLD REPORTS + static final String ALLURE_CONFIG_REPORTS_CLEANUP_ENABLED = "custom.allure.config.reports.cleanup.enabled"; + static final String ALLURE_CONFIG_MAX_STORED_REPORTS_COUNT = "custom.allure.max.stored.reports.count"; private AllureConstants() { + // do not instantiate } } diff --git a/src/main/java/io/qameta/allure/bamboo/AllureDownloader.java b/src/main/java/io/qameta/allure/bamboo/AllureDownloader.java index 33b5b8f..e913137 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureDownloader.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureDownloader.java @@ -35,6 +35,7 @@ import static org.apache.commons.io.FileUtils.moveDirectory; class AllureDownloader { + private static final Logger LOGGER = LoggerFactory.getLogger(AllureDownloader.class); private final AllureSettingsManager settingsManager; diff --git a/src/main/java/io/qameta/allure/bamboo/AllureGlobalConfig.java b/src/main/java/io/qameta/allure/bamboo/AllureGlobalConfig.java index dfc0c44..c80ffcf 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureGlobalConfig.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureGlobalConfig.java @@ -24,34 +24,44 @@ import java.util.Map; import java.util.Optional; +import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_CUSTOM_LOGO_ENABLED; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_DOWNLOAD_CLI_URL; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_DOWNLOAD_ENABLED; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_DOWNLOAD_URL; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_ENABLED_BY_DEFAULT; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_LOCAL_STORAGE; -import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CUSTOM_LOGO_ENABLED; +import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_REPORTS_CLEANUP_ENABLED; import static java.lang.Boolean.FALSE; import static java.lang.Boolean.TRUE; import static java.lang.Boolean.parseBoolean; import static org.apache.commons.lang3.SystemUtils.getJavaIoTmpDir; class AllureGlobalConfig implements Serializable { + + private static final long serialVersionUID = 1L; + private static final String DEFAULT_DOWNLOAD_BASE_URL = "https://github.com/allure-framework/allure2/releases/download/"; private static final String DEFAULT_CLI_BASE_URL = "https://repo.maven.apache.org/maven2/io/qameta/allure/"; - public static final String DEFAULT_LOCAL_STORAGE_PATH = new File(getJavaIoTmpDir(), "allure-reports").getPath(); + public static final String DEFAULT_LOCAL_STORAGE_PATH + = new File(getJavaIoTmpDir(), "allure-reports").getPath(); private final boolean downloadEnabled; private final boolean customLogoEnabled; private final boolean enabledByDefault; + private final boolean enabledReportsCleanup; private final String localStoragePath; private final String downloadBaseUrl; private final String downloadCliBaseUrl; AllureGlobalConfig() { - this(TRUE.toString(), FALSE.toString(), - DEFAULT_DOWNLOAD_BASE_URL, DEFAULT_LOCAL_STORAGE_PATH, - DEFAULT_CLI_BASE_URL, TRUE.toString()); + this(TRUE.toString(), + FALSE.toString(), + DEFAULT_DOWNLOAD_BASE_URL, + DEFAULT_LOCAL_STORAGE_PATH, + DEFAULT_CLI_BASE_URL, + TRUE.toString(), + FALSE.toString()); } AllureGlobalConfig(final String downloadEnabled, @@ -59,16 +69,24 @@ class AllureGlobalConfig implements Serializable { final String downloadBaseUrl, final String localStoragePath, final String cmdLineUrl, - final String customLogoEnable) { - this.downloadEnabled = StringUtils.isEmpty(downloadEnabled) ? TRUE : parseBoolean(downloadEnabled); - this.enabledByDefault = StringUtils.isEmpty(enabledByDefault) ? FALSE : parseBoolean(enabledByDefault); - this.downloadBaseUrl = StringUtils.isEmpty(downloadBaseUrl) ? DEFAULT_DOWNLOAD_BASE_URL : downloadBaseUrl; - this.downloadCliBaseUrl = StringUtils.isEmpty(cmdLineUrl) ? DEFAULT_CLI_BASE_URL : cmdLineUrl; - this.localStoragePath = StringUtils.isEmpty(localStoragePath) ? DEFAULT_LOCAL_STORAGE_PATH : localStoragePath; - this.customLogoEnabled = StringUtils.isEmpty(customLogoEnable) ? TRUE : parseBoolean(customLogoEnable); + final String customLogoEnable, + final String enabledReportsCleanup) { + this.downloadEnabled = StringUtils.isBlank(downloadEnabled) + ? TRUE : parseBoolean(downloadEnabled); + this.enabledByDefault = StringUtils.isBlank(enabledByDefault) + ? FALSE : parseBoolean(enabledByDefault); + this.downloadBaseUrl = StringUtils.isBlank(downloadBaseUrl) + ? DEFAULT_DOWNLOAD_BASE_URL : downloadBaseUrl; + this.downloadCliBaseUrl = StringUtils.isBlank(cmdLineUrl) + ? DEFAULT_CLI_BASE_URL : cmdLineUrl; + this.localStoragePath = StringUtils.isBlank(localStoragePath) + ? DEFAULT_LOCAL_STORAGE_PATH : localStoragePath; + this.customLogoEnabled = StringUtils.isBlank(customLogoEnable) + ? TRUE : parseBoolean(customLogoEnable); + this.enabledReportsCleanup = StringUtils.isBlank(enabledReportsCleanup) + ? FALSE : parseBoolean(enabledReportsCleanup); } - @NotNull static AllureGlobalConfig fromContext(final Map context) { return new AllureGlobalConfig( @@ -77,7 +95,8 @@ static AllureGlobalConfig fromContext(final Map context) { getSingleValue(context, ALLURE_CONFIG_DOWNLOAD_URL, DEFAULT_DOWNLOAD_BASE_URL), getSingleValue(context, ALLURE_CONFIG_LOCAL_STORAGE, DEFAULT_LOCAL_STORAGE_PATH), getSingleValue(context, ALLURE_CONFIG_DOWNLOAD_CLI_URL, DEFAULT_CLI_BASE_URL), - getSingleValue(context, ALLURE_CUSTOM_LOGO_ENABLED, FALSE.toString()) + getSingleValue(context, ALLURE_CONFIG_CUSTOM_LOGO_ENABLED, FALSE.toString()), + getSingleValue(context, ALLURE_CONFIG_REPORTS_CLEANUP_ENABLED, FALSE.toString()) ); } @@ -96,7 +115,8 @@ void toContext(final @NotNull Map context) { context.put(ALLURE_CONFIG_DOWNLOAD_URL, getDownloadBaseUrl()); context.put(ALLURE_CONFIG_DOWNLOAD_CLI_URL, getDownloadCliBaseUrl()); context.put(ALLURE_CONFIG_LOCAL_STORAGE, getLocalStoragePath()); - context.put(ALLURE_CUSTOM_LOGO_ENABLED, isCustomLogoEnabled()); + context.put(ALLURE_CONFIG_CUSTOM_LOGO_ENABLED, isCustomLogoEnabled()); + context.put(ALLURE_CONFIG_REPORTS_CLEANUP_ENABLED, isEnabledReportsCleanup()); } boolean isDownloadEnabled() { @@ -111,6 +131,10 @@ boolean isCustomLogoEnabled() { return customLogoEnabled; } + boolean isEnabledReportsCleanup() { + return enabledReportsCleanup; + } + String getDownloadBaseUrl() { return downloadBaseUrl; } diff --git a/src/main/java/io/qameta/allure/bamboo/AllureReportConfig.java b/src/main/java/io/qameta/allure/bamboo/AllureReportConfig.java deleted file mode 100644 index 3a05502..0000000 --- a/src/main/java/io/qameta/allure/bamboo/AllureReportConfig.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2016-2024 Qameta Software Inc - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.qameta.allure.bamboo; - -import java.io.Serializable; -import java.util.Map; - -import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_EXECUTABLE; -import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_REPORT_PATH; -import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_RESULTS_PATH; - -final class AllureReportConfig implements Serializable { - - private final String resultsPath; - private final String reportPath; - private final String executable; - - private AllureReportConfig(final String resultsPath, - final String reportPath, - final String executable) { - this.resultsPath = resultsPath; - this.reportPath = reportPath; - this.executable = executable; - } - - String getResultsPath() { - return resultsPath; - } - - String getReportPath() { - return reportPath; - } - - String getExecutable() { - return executable; - } - - static AllureReportConfig fromContext(final Map context) { - final String resultsPath = context.get(ALLURE_CONFIG_RESULTS_PATH); - final String reportPath = context.get(ALLURE_CONFIG_REPORT_PATH); - final String executable = context.get(ALLURE_CONFIG_EXECUTABLE); - return new AllureReportConfig(resultsPath, reportPath, executable); - } - -} diff --git a/src/main/java/io/qameta/allure/bamboo/AllureReportTask.java b/src/main/java/io/qameta/allure/bamboo/AllureReportTask.java index 5c98110..4a4af83 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureReportTask.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureReportTask.java @@ -24,7 +24,6 @@ /** * Executes report generation. - * Created by bvo2002 on 30.11.16. */ public class AllureReportTask implements TaskType { @@ -37,5 +36,4 @@ public TaskResult execute(final @NotNull TaskContext taskContext) { + " the suggested way of configuration as listed in the Allure docs! "); return TaskResultBuilder.newBuilder(taskContext).success().build(); } - } diff --git a/src/main/java/io/qameta/allure/bamboo/AllureSettingsManager.java b/src/main/java/io/qameta/allure/bamboo/AllureSettingsManager.java index 870a7d5..662ebe5 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureSettingsManager.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureSettingsManager.java @@ -18,12 +18,13 @@ import com.atlassian.sal.api.pluginsettings.PluginSettings; import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory; +import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_CUSTOM_LOGO_ENABLED; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_DOWNLOAD_CLI_URL; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_DOWNLOAD_ENABLED; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_DOWNLOAD_URL; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_ENABLED_BY_DEFAULT; import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_LOCAL_STORAGE; -import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CUSTOM_LOGO_ENABLED; +import static io.qameta.allure.bamboo.AllureConstants.ALLURE_CONFIG_REPORTS_CLEANUP_ENABLED; public class AllureSettingsManager { @@ -35,20 +36,29 @@ public AllureSettingsManager(final PluginSettingsFactory settingsFactory) { AllureGlobalConfig getSettings() { final String downloadEnabled = (String) settings.get(ALLURE_CONFIG_DOWNLOAD_ENABLED); - final String customLogoEnabled = (String) settings.get(ALLURE_CUSTOM_LOGO_ENABLED); + final String customLogoEnabled = (String) settings.get(ALLURE_CONFIG_CUSTOM_LOGO_ENABLED); final String enableByDefault = (String) settings.get(ALLURE_CONFIG_ENABLED_BY_DEFAULT); final String downloadBaseUrl = (String) settings.get(ALLURE_CONFIG_DOWNLOAD_URL); final String downloadCliBaseUrl = (String) settings.get(ALLURE_CONFIG_DOWNLOAD_CLI_URL); final String localStorage = (String) settings.get(ALLURE_CONFIG_LOCAL_STORAGE); - return new AllureGlobalConfig(downloadEnabled, enableByDefault, - downloadBaseUrl, localStorage, downloadCliBaseUrl, customLogoEnabled); + final String enabledReportsCleanup = (String) settings.get(ALLURE_CONFIG_REPORTS_CLEANUP_ENABLED); + + return new AllureGlobalConfig( + downloadEnabled, + enableByDefault, + downloadBaseUrl, + localStorage, + downloadCliBaseUrl, + customLogoEnabled, + enabledReportsCleanup); } void saveSettings(final AllureGlobalConfig config) { settings.put(ALLURE_CONFIG_DOWNLOAD_ENABLED, String.valueOf(config.isDownloadEnabled())); - settings.put(ALLURE_CUSTOM_LOGO_ENABLED, String.valueOf(config.isCustomLogoEnabled())); + settings.put(ALLURE_CONFIG_CUSTOM_LOGO_ENABLED, String.valueOf(config.isCustomLogoEnabled())); settings.put(ALLURE_CONFIG_DOWNLOAD_URL, String.valueOf(config.getDownloadBaseUrl())); settings.put(ALLURE_CONFIG_LOCAL_STORAGE, String.valueOf(config.getLocalStoragePath())); settings.put(ALLURE_CONFIG_ENABLED_BY_DEFAULT, String.valueOf(config.isEnabledByDefault())); + settings.put(ALLURE_CONFIG_REPORTS_CLEANUP_ENABLED, String.valueOf(config.isEnabledReportsCleanup())); } } diff --git a/src/main/java/io/qameta/allure/bamboo/AllureViewReportCondition.java b/src/main/java/io/qameta/allure/bamboo/AllureViewReportCondition.java index afc33e0..f3c1f99 100644 --- a/src/main/java/io/qameta/allure/bamboo/AllureViewReportCondition.java +++ b/src/main/java/io/qameta/allure/bamboo/AllureViewReportCondition.java @@ -26,6 +26,7 @@ import java.util.Map; public class AllureViewReportCondition implements Condition { + private static final Logger LOGGER = LoggerFactory.getLogger(AllureViewReportCondition.class); private final ResultsSummaryManager resultsSummaryManager; @@ -46,19 +47,20 @@ public boolean shouldDisplay(final Map context) { (String) context.get("buildKey") ); final String buildNumberString = (String) context.get("buildNumber"); - if (buildKey != null && buildNumberString != null) { - try { - final int buildNumber = Integer.parseInt(buildNumberString); - final ResultsSummary resultsSummary = this.resultsSummaryManager - .getResultsSummary(PlanKeys.getPlanResultKey(buildKey, buildNumber)); - if (resultsSummary != null) { - final AllureBuildResult buildResult = AllureBuildResult - .fromCustomData(resultsSummary.getCustomBuildData()); - return buildResult.hasInfo() && (resultsSummary.isFinished() || resultsSummary.isNotBuilt()); - } - } catch (Exception e) { - LOGGER.error("Failed to evaluate condition", e); + if (StringUtils.isBlank(buildKey) || StringUtils.isBlank(buildNumberString)) { + return false; + } + try { + final int buildNumber = Integer.parseInt(buildNumberString); + final ResultsSummary resultsSummary = this.resultsSummaryManager + .getResultsSummary(PlanKeys.getPlanResultKey(buildKey, buildNumber)); + if (resultsSummary != null) { + final AllureBuildResult buildResult = AllureBuildResult + .fromCustomData(resultsSummary.getCustomBuildData()); + return buildResult.hasInfo() && (resultsSummary.isFinished() || resultsSummary.isNotBuilt()); } + } catch (Exception e) { + LOGGER.error("Failed to evaluate condition", e); } return false; } diff --git a/src/main/java/io/qameta/allure/bamboo/util/ExceptionUtil.java b/src/main/java/io/qameta/allure/bamboo/util/ExceptionUtil.java index d5ebce6..65c78f5 100644 --- a/src/main/java/io/qameta/allure/bamboo/util/ExceptionUtil.java +++ b/src/main/java/io/qameta/allure/bamboo/util/ExceptionUtil.java @@ -21,6 +21,7 @@ public final class ExceptionUtil { private ExceptionUtil() { + // do not instantiate } public static String stackTraceToString(final Throwable e) { diff --git a/src/main/resources/allure-bamboo.properties b/src/main/resources/allure-bamboo.properties index 9f91d2a..4ca9a30 100644 --- a/src/main/resources/allure-bamboo.properties +++ b/src/main/resources/allure-bamboo.properties @@ -1,44 +1,19 @@ -allureplugin.policy=Generate Report -allureplugin.policy.description=Choose condition when report should be built -allureplugin.policy.error=Select for which builds to generate report -allureplugin.policy.all=For all builds -allureplugin.policy.with_problems=For builds with problems -allureplugin.policy.failed=For failed builds -allure.result.directory.label=Allure result directory -allure.result.directory.description=Specify the directory with allure results relative from build directory. \ - An example allure-results/ -allure.report.path.prefix.label=Report artifact subdirectory -allure.report.path.prefix.description=The subdirectory (or subdirectories) to put generated report into. \ - An example allure-report/ -allure.issues.tracker.pattern.label=Issue tracker pattern -allure.issues.tracker.pattern.description=\ - Specify the issue tracker pattern. E.g. http://bugtracker.example.org/issues/%s \ - For more information you can see an official documentation. -allure.tests.management.pattern.label=Test management pattern -allure.tests.management.pattern.description=\ - Specify the test management system pattern. E.g. http://tms.example.org/tests/%s \ - For more information you can see an official documentation. allure.task.help.link=https://allurereport.org/ allure.task.help.title=Go to Allure website! -error.property.empty=The property value should not be empty -error.path.absolute=The path should be relative -error.address.placeholder=The pattern should contain exactly one placeholder <%s> webitems.system.admin.build.allureReport=Allure Report admin.allureReportConfig.edit.title=Configure Allure Reporting admin.allureReportConfig.description= allure.plugin.title=Allure Report allure.config.title=Allure Reporting -allure.config.enable.checkbox.label=Enable Allure Building -allure.config.enabled.default.label=Build Allure for all builds by default +custom.allure.config.enabled.label=Enable Allure Building +custom.allure.config.enabled.default.label=Build Allure for all builds by default custom.allure.config.failed.only.label=Build report only for failed builds custom.allure.artifact.name.label=Artifact name to use -custom.allure.config.executable.label=Allure executable (home dir) -allure.config.download.enabled.label=Download if no executable present -allure.config.download.url.label=Allure binary base url +custom.allure.config.download.enabled.label=Download if no executable present +custom.allure.config.download.url.label=Allure binary base url allure.config.download.url.error.required=Allure binary base url is required -allure.config.local.storage.label=Allure local storage -allure.config.local.storage.label.required=Allure local storage is required -build.allure.title=Allure Report -buildResult.allure.title=Allure Report -allure.config.logo.enabled.label=Enable report custom logo +custom.allure.config.local.storage.label=Allure local storage +custom.allure.config.logo.enabled.label=Enable report custom logo custom.allure.logo.url.label=Custom logo +custom.allure.config.reports.cleanup.enabled.label=Enable reports cleanup +custom.allure.max.stored.reports.count.label=Count reports to store diff --git a/src/main/resources/templates/editAllureBambooConfiguration.ftl b/src/main/resources/templates/editAllureBambooConfiguration.ftl index a0ab169..e8c5e12 100644 --- a/src/main/resources/templates/editAllureBambooConfiguration.ftl +++ b/src/main/resources/templates/editAllureBambooConfiguration.ftl @@ -1,5 +1,5 @@ [@ui.bambooSection titleKey="allure.config.title"] - [@ww.checkbox labelKey='allure.config.enable.checkbox.label' name='custom.allure.config.enabled' toggle='true' /] + [@ww.checkbox labelKey='custom.allure.config.enabled.label' name='custom.allure.config.enabled' toggle='true' /] [@ww.checkbox labelKey='custom.allure.config.failed.only.label' name='custom.allure.config.failed.only' toggle='true' /] @@ -9,6 +9,8 @@ [@ww.textfield labelKey="custom.allure.artifact.name.label" name="custom.allure.artifact.name" required="false"/] - [@ww.textarea labelKey="custom.allure.logo.url.label" name="custom.allure.logo.url" required="false"/] + [@ww.textfield labelKey="custom.allure.logo.url.label" name="custom.allure.logo.url" required="false"/] + + [@ww.textfield labelKey="custom.allure.max.stored.reports.count.label" name="custom.allure.max.stored.reports.count" required="false"/] [/@ui.bambooSection] diff --git a/src/main/resources/templates/editAllureReportConfig.ftl b/src/main/resources/templates/editAllureReportConfig.ftl index 8b9ff1a..31b3ae5 100644 --- a/src/main/resources/templates/editAllureReportConfig.ftl +++ b/src/main/resources/templates/editAllureReportConfig.ftl @@ -18,15 +18,17 @@ submitLabelKey='global.buttons.update' titleKey='admin.allureReportConfig.edit.title' cancelUri='/admin/editAllureReportConfig.action' ] - [@ww.checkbox labelKey='allure.config.download.enabled.label' name='custom.allure.config.download.enabled' toggle='true' /] + [@ww.checkbox labelKey='custom.allure.config.download.enabled.label' name='custom.allure.config.download.enabled' toggle='true' /] - [@ww.checkbox labelKey='allure.config.logo.enabled.label' name='custom.allure.config.logo.enabled' toggle='true' /] + [@ww.checkbox labelKey='custom.allure.config.logo.enabled.label' name='custom.allure.config.logo.enabled' toggle='false' /] - [@ww.checkbox labelKey='allure.config.enabled.default.label' name='custom.allure.config.enabled.default' toggle='true' /] + [@ww.checkbox labelKey='custom.allure.config.enabled.default.label' name='custom.allure.config.enabled.default' toggle='true' /] - [@ww.textfield labelKey="allure.config.download.url.label" name="custom.allure.config.download.url" required="true"/] + [@ww.checkbox labelKey='custom.allure.config.reports.cleanup.enabled.label' name='custom.allure.config.reports.cleanup.enabled' toggle='false' /] - [@ww.textfield labelKey="allure.config.local.storage.label" name="custom.allure.config.local.storage" required="true"/] + [@ww.textfield labelKey="custom.allure.config.download.url.label" name="custom.allure.config.download.url" required="true"/] + + [@ww.textfield labelKey="custom.allure.config.local.storage.label" name="custom.allure.config.local.storage" required="true"/] [/@ww.form]