Skip to content
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
@@ -1,12 +1,13 @@
package com.backbase.oss.boat;

import lombok.extern.slf4j.Slf4j;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

@Slf4j
public abstract class AbstractGenerateMojo extends GenerateMojo {
Expand All @@ -16,25 +17,27 @@ public abstract class AbstractGenerateMojo extends GenerateMojo {
"ServerVariable.java", "StringUtil.java", "Authentication.java", "HttpBasicAuth.java", "HttpBearerAuth.java",
"ApiKeyAuth.java", "JavaTimeFormatter.java"
);
private static final String FALSE = "false";
private static final String TRUE = "true";

public void execute(String generatorName, String library, boolean isEmbedded, boolean reactive,
boolean generateSupportingFiles) throws MojoExecutionException, MojoFailureException {

Map<String, String> options = new HashMap<>();
options.put("library", library);
options.put("java8", "true");
options.put("java8", TRUE);
options.put("dateLibrary", "java8");
options.put("reactive", Boolean.toString(reactive));
options.put("performBeanValidation", "true");
options.put("skipDefaultInterface", "true");
options.put("interfaceOnly", "true");
options.put("useTags", "true");
options.put("useBeanValidation", "true");
options.put("useClassLevelBeanValidation", "false");
options.put("useOptional", "false");
options.put("useJakartaEe", "true");
options.put("useSpringBoot3", "true");
options.put("containerDefaultToNull", "false");
options.put("performBeanValidation", TRUE);
options.put("skipDefaultInterface", TRUE);
options.put("interfaceOnly", TRUE);
options.put("useTags", TRUE);
options.put("useBeanValidation", TRUE);
options.put("useClassLevelBeanValidation", FALSE);
options.put("useOptional", FALSE);
options.put("useJakartaEe", TRUE);
options.put("useSpringBoot3", TRUE);
options.put("containerDefaultToNull", FALSE);

this.generatorName = generatorName;
this.generateSupportingFiles = generateSupportingFiles;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ public void execute() throws MojoExecutionException {

}

public static String renderChangedOpenApi(Render render, ChangedOpenApi changedOpenApi) {
public static String renderChangedOpenApi(
Render render, ChangedOpenApi changedOpenApi) throws MojoExecutionException {
try (OutputStream outputStream = new ByteArrayOutputStream()) {
render.render(changedOpenApi, new OutputStreamWriter(outputStream));
return outputStream.toString();
} catch (IOException e) {
throw new RuntimeException(e);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not throw generic RuntimeException

throw new MojoExecutionException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.experimental.UtilityClass;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.plugin.MojoExecutionException;
import org.openapitools.openapidiff.core.compare.OpenApiDiff;
import org.openapitools.openapidiff.core.compare.OpenApiDiffOptions;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
Expand Down Expand Up @@ -114,26 +115,31 @@ private static void writeChangelogInOpenAPI(Path openApiFilePath, String changel
Files.write(openApiFilePath, SerializerUtils.toYamlString(diffedApi).getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
}

private static String renderChangeLog(List<ChangedOpenApi> changeLog) {
private static String renderChangeLog(List<ChangedOpenApi> changeLog) throws MojoExecutionException{
StringBuilder markDown = new StringBuilder();
markDown.append("# Changelog\n");
changeLog.forEach(diff -> {
markDown.append("## ")
for (ChangedOpenApi changedOpenApi : changeLog) {
markDown.append(generateDiffReport(changedOpenApi));
}
return markDown.toString();
}

private static String generateDiffReport(ChangedOpenApi diff) throws MojoExecutionException {
StringBuilder markDown = new StringBuilder();
markDown.append("## ")
.append(diff.getOldSpecOpenApi().getInfo().getVersion())
.append(" - ")
.append(diff.getNewSpecOpenApi().getInfo().getVersion())
.append("\n");

if (!diff.isDifferent()) {
markDown.append("No Changes\n");
} else {
if (diff.isIncompatible()) {
markDown.append("**Note:** API has incompatible changes!!\n");
}
String changes = DiffMojo.renderChangedOpenApi(markdownRender, diff);
markDown.append(changes);
if (!diff.isDifferent()) {
markDown.append("No Changes\n");
} else {
if (diff.isIncompatible()) {
markDown.append("**Note:** API has incompatible changes!!\n");
}
});
markDown.append(DiffMojo.renderChangedOpenApi(markdownRender, diff));
}
return markDown.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
import com.backbase.oss.boat.bay.client.model.*;
import com.backbase.oss.boat.loader.OpenAPILoader;
import com.backbase.oss.boat.loader.OpenAPILoaderException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import feign.auth.BasicAuthRequestInterceptor;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
Expand All @@ -28,6 +23,13 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static java.lang.String.format;

/**
Expand Down Expand Up @@ -140,77 +142,70 @@ public class RadioMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {

BasicAuthRequestInterceptor basicAuthRequestInterceptor = null;

ObjectMapper objectMapper = JsonMapper.builder()
.findAndAddModules()
.build();

if (StringUtils.isNotEmpty(boatBayUsername) && StringUtils.isNotEmpty(boatBayPassword)) {
getLog().info("Basic Authentication set for username " + boatBayUsername);
basicAuthRequestInterceptor = new BasicAuthRequestInterceptor(boatBayUsername, boatBayPassword);
} else {
getLog().info("No Authentication set");
}

List<UploadSpec> allSpecs = new ArrayList<>();

for (SpecConfig spec : specs) {
allSpecs.add(mapToUploadSpec(spec));
}

ApiClient apiClient = new ApiClient().setBasePath(boatBayUrl);
if (basicAuthRequestInterceptor != null) {
apiClient.addAuthorization("Basic Auth", basicAuthRequestInterceptor);
}

applyAuth(apiClient);
BoatMavenPluginApi api = apiClient.buildClient(BoatMavenPluginApi.class);

UploadRequestBody uploadRequestBody = UploadRequestBody.builder()
.groupId(groupId).artifactId(artifactId).version(version).specs(allSpecs).build();

List<BoatLintReport> reports =null;
try {
reports = api.uploadSpec(portalKey, sourceKey, uploadRequestBody);
}catch (Exception e){
getLog().error("BoatBay error :: " + e.getMessage());
if(failOnBoatBayErrorResponse)
throw new MojoFailureException("BoatBay error", e);
.groupId(groupId).artifactId(artifactId).version(version).specs(new ArrayList<>()).build();
for (SpecConfig spec : specs) {
uploadRequestBody.getSpecs().add(mapToUploadSpec(spec));
}

// Process Result
if(reports!=null) {
try {
File outputFile = new File(getOutput(), "radioOutput.json");
objectMapper.writerWithDefaultPrettyPrinter().writeValue(outputFile, reports);
// Log summary of report
reports.forEach(report -> {
getLog().info(format("Spec %s summary :", report.getSpec().getKey()));
getLog().info(format("Changes are %s ", report.getSpec().getChanges()));
getLog().info("Number of Violations:" + report.getViolations().size());
});
// Log link to reports
getLog().info("UPLOAD TO BOAT-BAY SUCCESSFUL, check the full report: " + outputFile.getCanonicalPath());

if (failOnBreakingChange) {
boolean doesSpecsHaveBreakingChanges = reports.stream()
.anyMatch(report -> report.getSpec().getChanges().equals(Changes.BREAKING));
if (doesSpecsHaveBreakingChanges)
throw new MojoFailureException("Specs have Breaking Changes. Check full report.");
try {
List<BoatLintReport> reports = api.uploadSpec(portalKey, sourceKey, uploadRequestBody);
writeReportFile(reports);

if (failOnBreakingChange) {
boolean doesSpecsHaveBreakingChanges = reports.stream()
.anyMatch(report -> report.getSpec().getChanges() == Changes.BREAKING);
if (doesSpecsHaveBreakingChanges) {
throw new MojoFailureException("Specs have Breaking Changes. Check full report.");
}
}

if (failOnLintViolation) {
boolean doesSpecsHaveMustViolations = reports.stream()
.anyMatch(report -> report.getViolations().stream()
.anyMatch(violation -> violation.getSeverity().equals(Severity.MUST)));
if (doesSpecsHaveMustViolations)
throw new MojoFailureException("Specs have Must Violations. Check full report.");
if (failOnLintViolation) {
boolean doesSpecsHaveMustViolations = reports.stream()
.anyMatch(report -> report.getViolations().stream()
.anyMatch(violation -> violation.getSeverity() == Severity.MUST));
if (doesSpecsHaveMustViolations) {
throw new MojoFailureException("Specs have Must Violations. Check full report.");
}
} catch (IOException e) {
throw new MojoFailureException("Failed to write output", e);
}
} catch (IOException e) {
throw new MojoExecutionException(e);
} catch (RuntimeException e){
getLog().error("BoatBay error :: " + e.getMessage());
if (failOnBoatBayErrorResponse) {
throw new MojoFailureException("BoatBay error", e);
}
}
}

private void writeReportFile(List<BoatLintReport> reports) throws IOException {
ObjectMapper objectMapper = JsonMapper.builder()
.findAndAddModules()
.build();
File outputFile = new File(getOutput(), "radioOutput.json");
objectMapper.writerWithDefaultPrettyPrinter().writeValue(outputFile, reports);
// Log summary of report
reports.forEach(report -> {
getLog().info(format("Spec %s summary :", report.getSpec().getKey()));
getLog().info(format("Changes are %s ", report.getSpec().getChanges()));
getLog().info("Number of Violations:" + report.getViolations().size());
});
// Log link to reports
getLog().info("UPLOAD TO BOAT-BAY SUCCESSFUL, check the full report: " + outputFile.getCanonicalPath());
}

private void applyAuth(ApiClient apiClient) {
if (StringUtils.isNotEmpty(boatBayUsername) && StringUtils.isNotEmpty(boatBayPassword)) {
getLog().info("Basic Authentication set for username " + boatBayUsername);
apiClient.addAuthorization(
"Basic Auth", new BasicAuthRequestInterceptor(boatBayUsername, boatBayPassword));
} else {
getLog().info("No Authentication set");
}
}

private UploadSpec mapToUploadSpec(SpecConfig spec) throws MojoExecutionException {
Expand Down
Loading