use of com.amazonaws.gurureviewercli.model.ScanMetaData in project aws-codeguru-cli by aws.
the class Main method main.
public static void main(String[] argv) {
val textIO = new TextIO(new SystemTextTerminal());
val main = new Main();
val jCommander = JCommander.newBuilder().addObject(main).build();
if (argv.length == 0) {
jCommander.usage();
return;
}
try {
jCommander.parse(argv);
val config = Configuration.builder().textIO(textIO).interactiveMode(!main.noPrompt).bucketName(main.bucketName).build();
main.validateInitialConfig(config);
// try to build the AWS client objects first.
main.createAWSClients(config);
String repoName = config.getRootDir().toFile().getName();
config.setRepoName(repoName);
// check if repo is valid git.
val gitMetaData = main.readGitMetaData(config, Paths.get(main.repoDir).toRealPath());
ScanMetaData scanMetaData = null;
List<RecommendationSummary> results = new ArrayList<>();
try {
val sourcePaths = main.sourceDirs.stream().map(Paths::get).map(Path::toAbsolutePath).map(Path::normalize).collect(Collectors.toList());
List<Path> buildPaths = null;
if (main.buildDirs != null) {
buildPaths = main.buildDirs.stream().map(Paths::get).map(Path::toAbsolutePath).map(Path::normalize).collect(Collectors.toList());
}
scanMetaData = ScanAdapter.startScan(config, gitMetaData, sourcePaths, buildPaths);
results.addAll(ScanAdapter.fetchResults(config, scanMetaData));
} finally {
if (scanMetaData != null) {
// try to clean up objects from S3.
main.tryDeleteS3Object(config.getS3Client(), scanMetaData.getBucketName(), scanMetaData.getSourceKey());
main.tryDeleteS3Object(config.getS3Client(), scanMetaData.getBucketName(), scanMetaData.getBuildKey());
}
}
val outputPath = Paths.get(main.outputDir);
if (!outputPath.toFile().exists()) {
if (!outputPath.toFile().mkdirs()) {
Log.error("Failed to create output directory %s.", outputPath);
}
}
ResultsAdapter.saveResults(outputPath, results, scanMetaData);
Log.info("Analysis finished.");
} catch (GuruCliException e) {
Log.error("%s: %s", e.getErrorCode(), e.getMessage());
e.printStackTrace();
System.exit(3);
} catch (ParameterException e) {
Log.error(e);
jCommander.usage();
System.exit(1);
} catch (Exception e) {
e.printStackTrace();
Log.error(e);
System.exit(2);
}
System.exit(0);
}
use of com.amazonaws.gurureviewercli.model.ScanMetaData in project aws-codeguru-cli by aws.
the class ResultsAdapter method createHtmlReport.
private static void createHtmlReport(final Path outputDir, final ScanMetaData scanMetaData, final List<RecommendationSummary> recommendations) throws IOException {
int validFindings = 0;
// sort by file name and line number
sortByFileName(recommendations);
Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder().build();
val htmlFile = outputDir.resolve("codeguru-report.html");
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(htmlFile.toFile()), StandardCharsets.UTF_8)) {
writer.write("<!DOCTYPE html>\n<html lang=\"en\">\n");
writer.write("<body>\n");
writer.write("<h2>CodeGuru Reviewer Recommendations</h2>\n");
val awsUrlPrfix = "https://console.aws.amazon.com/codeguru/reviewer";
val associationUrl = String.format("%s?region=%s#/ciworkflows/associationdetails/%s", awsUrlPrfix, scanMetaData.getRegion(), scanMetaData.getAssociationArn());
val scanUrl = String.format("%s?region=%s#/codereviews/details/%s", awsUrlPrfix, scanMetaData.getRegion(), scanMetaData.getCodeReviewArn());
writer.write(renderer.render(parser.parse(String.format("**CodeGuru Repository ARN**: [%s](%s)%n", scanMetaData.getAssociationArn(), associationUrl))));
writer.write(renderer.render(parser.parse(String.format("**CodeGuru Scan ARN**: [%s](%s)%n", scanMetaData.getCodeReviewArn(), scanUrl))));
writer.write("\n<br/><hr style=\"width:90%\"><br/>\n");
for (val recommendation : recommendations) {
val filePath = scanMetaData.getRepositoryRoot().resolve(recommendation.filePath()).toAbsolutePath();
if (filePath == null || !filePath.toFile().isFile()) {
if (filePath != null && !(filePath.endsWith(".") || filePath.endsWith("/"))) {
Log.warn("Dropping finding because file not found on disk: %s", filePath);
}
continue;
}
validFindings++;
String lineMsg;
if (!recommendation.startLine().equals(recommendation.endLine()) && recommendation.endLine() != null) {
lineMsg = String.format("### In: [%s](%s) L%d %n", filePath, filePath.toUri(), recommendation.startLine());
} else {
lineMsg = String.format("### In: [%s](%s) L%d - L%d %n", filePath, filePath.toUri(), recommendation.startLine(), recommendation.endLine());
}
Node document = parser.parse(String.format("### In: [%s](%s) L%d %n", filePath, filePath.toUri(), recommendation.startLine()));
writer.write(renderer.render(document));
document = parser.parse("**Issue:** " + recommendation.description());
writer.write(renderer.render(document));
writer.write(String.format("<p><strong>Severity:</strong> %s<p/>", recommendation.severity()));
if (recommendation.ruleMetadata() != null && recommendation.ruleMetadata().ruleId() != null) {
val manifest = recommendation.ruleMetadata();
writer.write(String.format("<p><strong>Rule ID:</strong> %s<p/>", manifest.ruleId()));
writer.write(String.format("<p><strong>Rule Name:</strong> %s<p/>", manifest.ruleName()));
document = parser.parse("**Description:** " + manifest.longDescription());
writer.write(renderer.render(document));
if (manifest.ruleTags() != null && !manifest.ruleTags().isEmpty()) {
val mdList = manifest.ruleTags().stream().map(s -> String.format("- %s%n", s)).collect(Collectors.joining());
document = parser.parse("**Tags:**\n" + mdList);
writer.write(renderer.render(document));
}
}
writer.write("\n<hr style=\"width:80%\">\n");
}
writer.write("</body>\n");
writer.write("</html>\n");
}
Log.info("Report with %d recommendations written to:%n%s", validFindings, htmlFile.normalize().toUri());
}
Aggregations