From b8a190d882dd9dc8489d34d41cd238c7fc17a8fe Mon Sep 17 00:00:00 2001 From: Arnold Galovics Date: Fri, 29 Sep 2023 19:49:40 +0200 Subject: [PATCH] #107: Include version number in filename for file based reportings --- .../swagger/brake/report/AbstractFileReporter.java | 4 ++-- .../redskap/swagger/brake/report/HtmlReporter.java | 13 ++++++++++--- .../redskap/swagger/brake/report/JsonReporter.java | 12 +++++++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/swagger-brake/src/main/java/io/redskap/swagger/brake/report/AbstractFileReporter.java b/swagger-brake/src/main/java/io/redskap/swagger/brake/report/AbstractFileReporter.java index ffe8235..4708552 100644 --- a/swagger-brake/src/main/java/io/redskap/swagger/brake/report/AbstractFileReporter.java +++ b/swagger-brake/src/main/java/io/redskap/swagger/brake/report/AbstractFileReporter.java @@ -27,7 +27,7 @@ public void report(Collection breakingChanges, Options options, String json = toFileContent(breakingChanges, apiInfo); try { directoryCreator.create(options.getOutputFilePath()); - String filePath = options.getOutputFilePath() + File.separator + getFilename(); + String filePath = options.getOutputFilePath() + File.separator + getFilename(apiInfo); fileWriter.write(filePath, json); log.info("Report can be found at {}", filePath); } catch (Exception e) { @@ -35,7 +35,7 @@ public void report(Collection breakingChanges, Options options, } } - protected abstract String getFilename(); + protected abstract String getFilename(ApiInfo apiInfo); protected abstract String toFileContent(Collection breakingChanges, ApiInfo apiInfo); } diff --git a/swagger-brake/src/main/java/io/redskap/swagger/brake/report/HtmlReporter.java b/swagger-brake/src/main/java/io/redskap/swagger/brake/report/HtmlReporter.java index f4e8564..1f308ac 100644 --- a/swagger-brake/src/main/java/io/redskap/swagger/brake/report/HtmlReporter.java +++ b/swagger-brake/src/main/java/io/redskap/swagger/brake/report/HtmlReporter.java @@ -1,5 +1,7 @@ package io.redskap.swagger.brake.report; +import static org.apache.commons.lang3.StringUtils.isNotBlank; + import io.redskap.swagger.brake.core.ApiInfo; import io.redskap.swagger.brake.core.BreakingChange; import io.redskap.swagger.brake.report.file.DirectoryCreator; @@ -21,7 +23,8 @@ @Component @Slf4j class HtmlReporter extends AbstractFileReporter implements CheckableReporter { - private static final String FILENAME = "swagger-brake.html"; + private static final String DEFAULT_FILENAME = "swagger-brake.html"; + private static final String VERSIONED_FILENAME_TEMPLATE = "swagger-brake-%s.html"; private final JsonConverter jsonConverter; private final MustacheContentResolver mustacheContentResolver; @@ -32,8 +35,12 @@ public HtmlReporter(FileWriter fileWriter, DirectoryCreator directoryCreator, Js } @Override - protected String getFilename() { - return FILENAME; + protected String getFilename(ApiInfo apiInfo) { + String result = DEFAULT_FILENAME; + if (apiInfo != null && isNotBlank(apiInfo.getVersion())) { + result = VERSIONED_FILENAME_TEMPLATE.formatted(apiInfo.getVersion()); + } + return result; } @Override diff --git a/swagger-brake/src/main/java/io/redskap/swagger/brake/report/JsonReporter.java b/swagger-brake/src/main/java/io/redskap/swagger/brake/report/JsonReporter.java index f2fe16c..b5f1b20 100644 --- a/swagger-brake/src/main/java/io/redskap/swagger/brake/report/JsonReporter.java +++ b/swagger-brake/src/main/java/io/redskap/swagger/brake/report/JsonReporter.java @@ -1,6 +1,7 @@ package io.redskap.swagger.brake.report; import static java.util.stream.Collectors.groupingBy; +import static org.apache.commons.lang3.StringUtils.isNotBlank; import io.redskap.swagger.brake.core.ApiInfo; import io.redskap.swagger.brake.core.BreakingChange; @@ -19,7 +20,8 @@ @Slf4j @Component class JsonReporter extends AbstractFileReporter implements CheckableReporter { - private static final String FILENAME = "swagger-brake.json"; + private static final String DEFAULT_FILENAME = "swagger-brake.json"; + private static final String VERSIONED_FILENAME_TEMPLATE = "swagger-brake-%s.json"; private final JsonConverter jsonConverter; public JsonReporter(FileWriter fileWriter, DirectoryCreator directoryCreator, JsonConverter jsonConverter) { @@ -28,8 +30,12 @@ public JsonReporter(FileWriter fileWriter, DirectoryCreator directoryCreator, Js } @Override - protected String getFilename() { - return FILENAME; + protected String getFilename(ApiInfo apiInfo) { + String result = DEFAULT_FILENAME; + if (apiInfo != null && isNotBlank(apiInfo.getVersion())) { + result = VERSIONED_FILENAME_TEMPLATE.formatted(apiInfo.getVersion()); + } + return result; } @Override