forked from aws/aws-codeguru-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for BitBucket CodeInsights format
Adding bitbucket codeinsight export format
- Loading branch information
1 parent
8855a9b
commit 7519bae
Showing
12 changed files
with
298 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lombok.anyConstructor.addConstructorProperties=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/amazonaws/gurureviewercli/model/bitbucket/CodeInsightsAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.amazonaws.gurureviewercli.model.bitbucket; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
/** | ||
* Bitbucket CodeInsight annotation. | ||
* See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-reports/#api-group-reports | ||
* | ||
* Example | ||
* { | ||
* "external_id": "CodeGuruReviewer-02-annotation002", | ||
* "title": "Bug report", | ||
* "annotation_type": "BUG", | ||
* "summary": "This line might introduce a bug.", | ||
* "severity": "MEDIUM", | ||
* "path": "my-service/src/main/java/com/myCompany/mysystem/logic/Helper.java", | ||
* "line": 13 | ||
* } | ||
*/ | ||
@Log4j2 | ||
@Builder | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CodeInsightsAnnotation { | ||
|
||
private String title; | ||
|
||
@JsonProperty("external_id") | ||
private String externalId; | ||
|
||
@JsonProperty("annotation_type") | ||
private String annotationType; | ||
|
||
private String path; | ||
|
||
private long line; | ||
|
||
private String summary; | ||
|
||
private String severity; | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/amazonaws/gurureviewercli/model/bitbucket/CodeInsightsReport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.amazonaws.gurureviewercli.model.bitbucket; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
/** | ||
* Bitbucket CodeInsight report format. | ||
* See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-reports/#api-group-reports | ||
* Example: | ||
* { | ||
* "title": "Amazon CodeGuru Reviewer Scan Report", | ||
* "details": "Some more text.", | ||
* "report_type": "SECURITY", | ||
* "reporter": "Amazon CodeGuru Reviewer", | ||
* "link": "http://www.CodeGuruReviewer.com/reports/001", | ||
* "result": "FAILED", | ||
* "data": [ | ||
* { | ||
* "title": "Duration (seconds)", | ||
* "type": "DURATION", | ||
* "value": 14 | ||
* }, | ||
* { | ||
* "title": "Safe to merge?", | ||
* "type": "BOOLEAN", | ||
* "value": false | ||
* } | ||
* ] | ||
* } | ||
*/ | ||
@Log4j2 | ||
@Builder | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CodeInsightsReport { | ||
|
||
private String title; | ||
|
||
private String details; | ||
|
||
private String result; | ||
|
||
private String link; | ||
|
||
private List<CodeInsightsReportData> data; | ||
|
||
@JsonProperty("reporter") | ||
private String reporter; | ||
|
||
@JsonProperty("report_type") | ||
private final String reportType = "SECURITY"; | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/amazonaws/gurureviewercli/model/bitbucket/CodeInsightsReportData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.amazonaws.gurureviewercli.model.bitbucket; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
/** | ||
* Bitbucket CodeInsight report data. | ||
* See https://developer.atlassian.com/cloud/bitbucket/rest/api-group-reports/#api-group-reports | ||
*/ | ||
@Log4j2 | ||
@Builder | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CodeInsightsReportData { | ||
|
||
private String title; | ||
|
||
private String type; | ||
|
||
private Object value; | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/com/amazonaws/gurureviewercli/util/CodeInsightExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.amazonaws.gurureviewercli.util; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.stream.Collectors; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import lombok.val; | ||
import software.amazon.awssdk.services.codegurureviewer.model.RecommendationSummary; | ||
import software.amazon.awssdk.services.codegurureviewer.model.Severity; | ||
|
||
import com.amazonaws.gurureviewercli.model.ScanMetaData; | ||
import com.amazonaws.gurureviewercli.model.bitbucket.CodeInsightsAnnotation; | ||
import com.amazonaws.gurureviewercli.model.bitbucket.CodeInsightsReport; | ||
|
||
/** | ||
* Export Report and Annotations file for BitBucket CodeInsights. | ||
*/ | ||
public final class CodeInsightExport { | ||
private static final String REPORT_FILE_NAME = "report.json"; | ||
private static final String ANNOTATIONS_FILE_NAME = "annotations.json"; | ||
|
||
private static final JsonMapper JSON_MAPPER = | ||
JsonMapper.builder() | ||
.serializationInclusion(JsonInclude.Include.NON_ABSENT) | ||
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
.disable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS) | ||
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) | ||
.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE) | ||
.build(); | ||
|
||
public static void report(final Collection<RecommendationSummary> recommendations, | ||
final ScanMetaData scanMetaData, | ||
final Path outputDir) throws IOException { | ||
val reportTitle = "CodeGuru Reviewer report"; | ||
val url = String.format("https://console.aws.amazon.com/codeguru/reviewer?region=%s#/codereviews/details/%s", | ||
scanMetaData.getRegion(), scanMetaData.getCodeReviewArn()); | ||
val report = CodeInsightsReport.builder() | ||
.title(reportTitle) | ||
.reporter("CodeGuru Reviewer CLI") | ||
.details(String.format("CodeGuru Reviewer reported %d recommendations", | ||
recommendations.size())) | ||
.result(recommendations.isEmpty() ? "PASSED" : "FAILED") | ||
.link(url) | ||
.data(new ArrayList<>()) | ||
.build(); | ||
|
||
val annotations = recommendations.stream().map(r -> convert(r, reportTitle)) | ||
.collect(Collectors.toList()); | ||
|
||
JSON_MAPPER.writeValue(outputDir.resolve(REPORT_FILE_NAME).toFile(), report); | ||
JSON_MAPPER.writeValue(outputDir.resolve(ANNOTATIONS_FILE_NAME).toFile(), annotations); | ||
} | ||
|
||
private static CodeInsightsAnnotation convert(final RecommendationSummary recommendation, | ||
final String reportTitle) { | ||
String description = recommendation.recommendationCategoryAsString(); | ||
if (recommendation.ruleMetadata() != null) { | ||
description = recommendation.ruleMetadata().shortDescription(); | ||
} | ||
|
||
return CodeInsightsAnnotation.builder() | ||
.title(reportTitle) | ||
.externalId(recommendation.recommendationId()) | ||
.path(recommendation.filePath()) | ||
.line(recommendation.startLine()) | ||
.summary(description) | ||
.annotationType("Vulnerability".toUpperCase()) | ||
.severity(convertSeverity(recommendation.severity())) | ||
.build(); | ||
} | ||
|
||
private static String convertSeverity(Severity guruSeverity) { | ||
if (guruSeverity != null) { | ||
return guruSeverity.toString().toUpperCase(); // Bitbucket uses the same severity levels as CodeGuru. | ||
} | ||
return "Unknown"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.