Skip to content
Draft
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
Expand Up @@ -52,13 +52,27 @@ public static ApplyResult apply(
IAviatorLogger logger,
Set<String> issueIdFilter,
Logger skipLog) {
return apply(source, sourceCodeDirectory, logger, issueIdFilter, skipLog, false);
}

/**
* Applies or previews remediations for each source entry until done or the issue-id filter is exhausted.
* Caller owns {@code source} lifecycle (try-with-resources).
*/
public static ApplyResult apply(
IRemediationsFprSource source,
String sourceCodeDirectory,
IAviatorLogger logger,
Set<String> issueIdFilter,
Logger skipLog,
boolean previewMode) {
Accumulator acc = new Accumulator(issueIdFilter);
source.forEachEntry((fprPath, label, id, index, total) -> {
if (acc.remaining != null && acc.remaining.isEmpty()) {
return false;
}
RemediationMetric metric = applyOne(
fprPath, label, index, total, sourceCodeDirectory, logger, acc.remaining, skipLog);
fprPath, label, index, total, sourceCodeDirectory, logger, acc.remaining, skipLog, previewMode);
if (metric == null) {
acc.skipped++;
} else {
Expand All @@ -85,11 +99,12 @@ private static RemediationMetric applyOne(
String sourceCodeDirectory,
IAviatorLogger logger,
Set<String> issueFilter,
Logger skipLog) {
Logger skipLog,
boolean previewMode) {
logger.progress("Processing FPR " + index + "/" + total + " (" + entryLabel + ")");
logger.progress("Status: Processing FPR with Aviator for Applying Auto Remediations");
logger.progress("Status: Processing FPR with Aviator for " + (previewMode ? "Previewing" : "Applying") + " Auto Remediations");
try (FprHandle fprHandle = new FprHandle(fprPath)) {
return ApplyAutoRemediationOnSource.applyRemediations(fprHandle, sourceCodeDirectory, logger, issueFilter);
return ApplyAutoRemediationOnSource.applyRemediations(fprHandle, sourceCodeDirectory, logger, issueFilter, previewMode);
} catch (AviatorSimpleException e) {
skipLog.warn("Skipping entry {} as {}", entryLabel, e.getMessage());
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ private AviatorRemediationMetricsHelper() {}
public static RemediationMetric aggregateMetrics(Set<String> requestedIssueIds, Collection<RemediationMetric> metrics) {
Set<String> modifiedFiles = new LinkedHashSet<>();
Map<String, Integer> skippedByReason = new LinkedHashMap<>();
List<com.fortify.cli.aviator.fpr.processor.preview.PreviewDetail> allPreviewDetails = new ArrayList<>();
Collection<RemediationMetric> safeMetrics = metrics == null ? List.of() : metrics;

// Aggregate preview details from all metrics
for (RemediationMetric metric : safeMetrics) {
if (metric.previewDetails() != null) {
allPreviewDetails.addAll(metric.previewDetails());
}
}

if (requestedIssueIds == null) {
int totalRemediations = 0;
int appliedRemediations = 0;
Expand All @@ -49,14 +58,16 @@ public static RemediationMetric aggregateMetrics(Set<String> requestedIssueIds,
appliedRemediations += metric.appliedRemediations();
accumulateFilesAndSkips(metric, modifiedFiles, skippedByReason);
}
return RemediationMetric.unfiltered(totalRemediations, appliedRemediations, modifiedFiles, skippedByReason);
return RemediationMetric.unfiltered(totalRemediations, appliedRemediations, modifiedFiles, skippedByReason,
allPreviewDetails.isEmpty() ? null : allPreviewDetails);
}
Set<String> appliedIssueIds = new LinkedHashSet<>();
for (RemediationMetric metric : safeMetrics) {
appliedIssueIds.addAll(metric.appliedIssueIds());
accumulateFilesAndSkips(metric, modifiedFiles, skippedByReason);
}
return RemediationMetric.filtered(requestedIssueIds, appliedIssueIds, modifiedFiles, skippedByReason);
return RemediationMetric.filtered(requestedIssueIds, appliedIssueIds, modifiedFiles, skippedByReason,
allPreviewDetails.isEmpty() ? null : allPreviewDetails);
}

private static void accumulateFilesAndSkips(
Expand Down Expand Up @@ -120,8 +131,25 @@ public static void putRemediationMetricFields(ObjectNode result, RemediationMetr

/** Metric fields plus {@code __action__} (shared by SSC/FoD result builders). */
public static void putMetricAndAction(ObjectNode result, RemediationMetric metric) {
putMetricAndAction(result, metric, false);
}

/** Metric fields plus {@code __action__} and optional preview details (shared by SSC/FoD result builders). */
public static void putMetricAndAction(ObjectNode result, RemediationMetric metric, boolean previewMode) {
putRemediationMetricFields(result, metric);
result.put(IActionCommandResultSupplier.actionFieldName, actionLabel(metric));

if (previewMode && metric != null && metric.previewDetails() != null) {
result.set("previewDetails", toPreviewDetailsArray(metric.previewDetails()));
}
}

private static ArrayNode toPreviewDetailsArray(List<?> previewDetails) {
ArrayNode array = JsonHelper.getObjectMapper().createArrayNode();
if (previewDetails != null) {
previewDetails.forEach(detail -> array.add(JsonHelper.getObjectMapper().valueToTree(detail)));
}
return array;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,27 @@ public class ApplyAutoRemediationOnSource {

public static RemediationMetric applyRemediations(FprHandle fprHandle, String sourceCodeDirectory, IAviatorLogger logger)
throws AviatorSimpleException, AviatorTechnicalException {
return applyRemediations(fprHandle, sourceCodeDirectory, logger, null);
return applyRemediations(fprHandle, sourceCodeDirectory, logger, null, false);
}

public static RemediationMetric applyRemediations(FprHandle fprHandle, String sourceCodeDirectory, IAviatorLogger logger,
Set<String> issueIdFilter)
throws AviatorSimpleException, AviatorTechnicalException {
return applyRemediations(fprHandle, sourceCodeDirectory, logger, issueIdFilter, false);
}

public static RemediationMetric applyRemediations(FprHandle fprHandle, String sourceCodeDirectory, IAviatorLogger logger,
Set<String> issueIdFilter, boolean previewMode)
throws AviatorSimpleException, AviatorTechnicalException {

LOG.info("Starting apply auto-remediation process for file: {}", fprHandle.getFprPath());
LOG.info("Starting {} process for file: {}", previewMode ? "preview" : "apply auto-remediation", fprHandle.getFprPath());

if (!fprHandle.hasRemediations()) {
throw new AviatorSimpleException("FPR file does not contain remediations.xml file.");
}
LOG.info("FPR validation successful");

RemediationProcessor remediationProcessor = new RemediationProcessor(fprHandle, sourceCodeDirectory, issueIdFilter);
RemediationProcessor remediationProcessor = new RemediationProcessor(fprHandle, sourceCodeDirectory, issueIdFilter, previewMode);
return remediationProcessor.processRemediationXML();

}
Expand Down
Loading
Loading