-
Notifications
You must be signed in to change notification settings - Fork 34
feat: add --preview flag to preview aviator remediations before applying #1076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rsenden
merged 9 commits into
feat/v3.x/aviator/26.4
from
dhanwanthp/feat/auto_remediations_preview
Aug 28, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3864b09
feat: add --preview flag to preview aviator remediations before applying
205ecde
fix: updated the output of --preview flag for apply-remediations comm…
7bcba4a
fix: Changed preview to a real type, reordered some of the output met…
7333299
fix: align FoD --preview option with repository convention
9318199
refactor: address code review feedback
00ec633
refactor: address PR review comments
85bc159
fix: resolve build issue flagged by spotlessJavaCheck due to formatti…
c22db1c
refactor: address PR review comments
ea4927c
refactor: address PR review feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...java/com/fortify/cli/aviator/_common/cli/mixin/AbstractApplyRemediationsOptionsMixin.java
This file contains hidden or 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,78 @@ | ||
| /* | ||
| * Copyright 2021-2026 Open Text. | ||
| * | ||
| * The only warranties for products and services of Open Text | ||
| * and its affiliates and licensors ("Open Text") are as may | ||
| * be set forth in the express warranty statements accompanying | ||
| * such products and services. Nothing herein should be construed | ||
| * as constituting an additional warranty. Open Text shall not be | ||
| * liable for technical or editorial errors or omissions contained | ||
| * herein. The information contained herein is subject to change | ||
| * without notice. | ||
| */ | ||
| package com.fortify.cli.aviator._common.cli.mixin; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import com.fortify.cli.aviator._common.remediations_cache.IApplyRemediationsOptions; | ||
| import com.fortify.cli.common.exception.FcliSimpleException; | ||
|
|
||
| import lombok.Getter; | ||
| import picocli.CommandLine.Option; | ||
|
|
||
| /** | ||
| * Abstract base for apply-remediations options. Provides shared CLI options and validation template method. | ||
| * Product-specific subclasses declare source-selection mixins and implement validation hooks. | ||
| */ | ||
| @Getter | ||
| public abstract class AbstractApplyRemediationsOptionsMixin implements IApplyRemediationsOptions { | ||
| @Option(names = {"--source-dir"}) | ||
| private String sourceCodeDirectory = System.getProperty("user.dir"); | ||
|
|
||
| @Option(names = {"--issue-ids"}, split = ",") | ||
| private List<String> issueIds; | ||
|
|
||
| @Option(names = {"--preview"}) | ||
| private boolean previewMode = false; | ||
|
|
||
| /** | ||
| * Validates all options by calling validation hooks in order. | ||
| * Template method: ensures consistent validation sequence across SSC and FoD. | ||
| */ | ||
| @Override | ||
| public final void validate() { | ||
| validateSourceSelection(); | ||
| validateSourceDir(); | ||
| validateIssueIdsConstraints(); | ||
| } | ||
|
|
||
| /** Hook for product-specific source selection validation (--from-cache vs online selection). */ | ||
| protected abstract void validateSourceSelection(); | ||
|
|
||
| /** Hook to determine if --from-cache is selected (needed for --issue-ids constraint validation). */ | ||
| protected abstract boolean isCacheMode(); | ||
|
|
||
| private void validateSourceDir() { | ||
| FcliSimpleException.throwIf( | ||
| StringUtils.isBlank(sourceCodeDirectory), | ||
| "--source-dir must specify a valid directory path"); | ||
| Path path = Path.of(sourceCodeDirectory); | ||
| FcliSimpleException.throwIf( | ||
| !Files.exists(path) || !Files.isDirectory(path), | ||
| "--source-dir path does not exist or is not a directory: %s", sourceCodeDirectory); | ||
| FcliSimpleException.throwIf( | ||
| !Files.isReadable(path), | ||
| "--source-dir path is not accessible: %s", sourceCodeDirectory); | ||
| } | ||
|
|
||
| private void validateIssueIdsConstraints() { | ||
| FcliSimpleException.throwIf( | ||
| issueIds != null && !issueIds.isEmpty() && !isCacheMode(), | ||
| "--issue-ids can only be used with --from-cache; " | ||
| + "create a cache with download-remediations-cache and rerun with --from-cache"); | ||
| } | ||
| } |
66 changes: 66 additions & 0 deletions
66
...m/fortify/cli/aviator/_common/output/cli/cmd/AbstractAviatorApplyRemediationsCommand.java
This file contains hidden or 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,66 @@ | ||
| /* | ||
| * Copyright 2021-2026 Open Text. | ||
| * | ||
| * The only warranties for products and services of Open Text | ||
| * and its affiliates and licensors ("Open Text") are as may | ||
| * be set forth in the express warranty statements accompanying | ||
| * such products and services. Nothing herein should be construed | ||
| * as constituting an additional warranty. Open Text shall not be | ||
| * liable for technical or editorial errors or omissions contained | ||
| * herein. The information contained herein is subject to change | ||
| * without notice. | ||
| */ | ||
| package com.fortify.cli.aviator._common.output.cli.cmd; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fortify.cli.aviator._common.cli.mixin.AbstractApplyRemediationsOptionsMixin; | ||
| import com.fortify.cli.aviator._common.remediations_cache.IRemediationsFprSource; | ||
| import com.fortify.cli.aviator._common.remediations_cache.RemediationsApplyHelper; | ||
| import com.fortify.cli.aviator._common.remediations_cache.RemediationsApplyHelper.ApplyResult; | ||
| import com.fortify.cli.aviator._common.util.AviatorIssueIdFilterUtils; | ||
| import com.fortify.cli.aviator.config.AviatorLoggerImpl; | ||
| import com.fortify.cli.common.output.cli.cmd.AbstractOutputCommand; | ||
| import com.fortify.cli.common.output.cli.cmd.IJsonNodeSupplier; | ||
| import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins; | ||
| import com.fortify.cli.common.progress.cli.mixin.ProgressWriterFactoryMixin; | ||
| import com.fortify.cli.common.progress.helper.IProgressWriter; | ||
|
|
||
| import lombok.Getter; | ||
| import picocli.CommandLine.Mixin; | ||
|
|
||
| /** | ||
| * Abstract base command for applying remediations. Orchestrates validation, FPR source acquisition, | ||
| * and remediation application. Product-specific subclasses provide options mixin and implement hooks. | ||
| */ | ||
| public abstract class AbstractAviatorApplyRemediationsCommand extends AbstractOutputCommand | ||
| implements IJsonNodeSupplier { | ||
|
|
||
| @Getter @Mixin private OutputHelperMixins.DetailsNoQuery outputHelper; | ||
| @Mixin private ProgressWriterFactoryMixin progressWriterFactoryMixin; | ||
|
|
||
| /** Subclasses declare their product-specific options mixin (SSC or FoD). */ | ||
| protected abstract AbstractApplyRemediationsOptionsMixin getApplyOptions(); | ||
|
|
||
| @Override | ||
| public final JsonNode getJsonNode() { | ||
| AbstractApplyRemediationsOptionsMixin applyOptions = getApplyOptions(); | ||
| applyOptions.validate(); | ||
| Set<String> issueIdFilter = AviatorIssueIdFilterUtils.normalizeIssueIds(applyOptions.getIssueIds()); | ||
| try (IProgressWriter progressWriter = progressWriterFactoryMixin.create()) { | ||
| AviatorLoggerImpl logger = new AviatorLoggerImpl(progressWriter); | ||
| try (IRemediationsFprSource fprSource = openFprSource(logger, progressWriter)) { | ||
| ApplyResult result = RemediationsApplyHelper.apply(fprSource, applyOptions, issueIdFilter, logger); | ||
| return buildResultNode(fprSource, result, issueIdFilter); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| protected abstract IRemediationsFprSource openFprSource(AviatorLoggerImpl logger, IProgressWriter progressWriter); | ||
|
|
||
| protected abstract JsonNode buildResultNode(IRemediationsFprSource fprSource, ApplyResult result, Set<String> issueIdFilter); | ||
|
|
||
| @Override | ||
| public final boolean isSingular() { return true; } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
...in/java/com/fortify/cli/aviator/_common/remediations_cache/IApplyRemediationsOptions.java
This file contains hidden or 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,24 @@ | ||
| /* | ||
| * Copyright 2021-2026 Open Text. | ||
| * | ||
| * The only warranties for products and services of Open Text | ||
| * and its affiliates and licensors ("Open Text") are as may | ||
| * be set forth in the express warranty statements accompanying | ||
| * such products and services. Nothing herein should be construed | ||
| * as constituting an additional warranty. Open Text shall not be | ||
| * liable for technical or editorial errors or omissions contained | ||
| * herein. The information contained herein is subject to change | ||
| * without notice. | ||
| */ | ||
| package com.fortify.cli.aviator._common.remediations_cache; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** Abstraction over the shared apply-remediations CLI options, allowing RemediationsApplyHelper | ||
| * to remain independent of concrete Picocli types. */ | ||
| public interface IApplyRemediationsOptions { | ||
| String getSourceCodeDirectory(); | ||
| List<String> getIssueIds(); | ||
| boolean isPreviewMode(); | ||
| void validate(); | ||
| } |
This file contains hidden or 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
43 changes: 0 additions & 43 deletions
43
...rc/main/java/com/fortify/cli/aviator/_common/util/AviatorApplyRemediationsCliSupport.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import com.fortify.cli.aviator.fpr.processor.RemediationProcessor.RemediationMetric; | ||
| import com.fortify.cli.aviator.fpr.processor.preview.PreviewDetail; | ||
| import com.fortify.cli.common.json.JsonHelper; | ||
| import com.fortify.cli.common.output.transform.IActionCommandResultSupplier; | ||
|
|
||
|
|
@@ -38,25 +39,49 @@ private AviatorRemediationMetricsHelper() {} | |
| * aggregation (XML totals); non-null selects filtered aggregation (requested IDs). | ||
| */ | ||
| public static RemediationMetric aggregateMetrics(Set<String> requestedIssueIds, Collection<RemediationMetric> metrics) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems a fairly long method; can we improve this through self-describing sub-methods, and/or through utility (builder) methods on |
||
| Collection<RemediationMetric> safeMetrics = metrics == null ? List.of() : metrics; | ||
| return requestedIssueIds == null | ||
| ? aggregateUnfiltered(safeMetrics) | ||
| : aggregateFiltered(requestedIssueIds, safeMetrics); | ||
| } | ||
|
|
||
| private static RemediationMetric aggregateUnfiltered(Collection<RemediationMetric> metrics) { | ||
| int totalRemediations = 0, appliedRemediations = 0; | ||
| Set<String> modifiedFiles = new LinkedHashSet<>(); | ||
| Map<String, Integer> skippedByReason = new LinkedHashMap<>(); | ||
| Collection<RemediationMetric> safeMetrics = metrics == null ? List.of() : metrics; | ||
| if (requestedIssueIds == null) { | ||
| int totalRemediations = 0; | ||
| int appliedRemediations = 0; | ||
| for (RemediationMetric metric : safeMetrics) { | ||
| totalRemediations += metric.totalRemediations(); | ||
| appliedRemediations += metric.appliedRemediations(); | ||
| accumulateFilesAndSkips(metric, modifiedFiles, skippedByReason); | ||
| List<PreviewDetail> previewDetails = new ArrayList<>(); | ||
| boolean previewMode = false; | ||
| for (RemediationMetric metric : metrics) { | ||
| totalRemediations += metric.totalRemediations(); | ||
| appliedRemediations += metric.appliedRemediations(); | ||
| accumulateFilesAndSkips(metric, modifiedFiles, skippedByReason); | ||
| if (metric instanceof RemediationMetric.Preview preview) { | ||
| previewMode = true; | ||
| previewDetails.addAll(preview.previewDetails()); | ||
| } | ||
| return RemediationMetric.unfiltered(totalRemediations, appliedRemediations, modifiedFiles, skippedByReason); | ||
| } | ||
| return previewMode | ||
| ? RemediationMetric.previewUnfiltered(totalRemediations, appliedRemediations, modifiedFiles, skippedByReason, previewDetails) | ||
| : RemediationMetric.unfiltered(totalRemediations, appliedRemediations, modifiedFiles, skippedByReason); | ||
| } | ||
|
|
||
| private static RemediationMetric aggregateFiltered(Set<String> requestedIssueIds, Collection<RemediationMetric> metrics) { | ||
| Set<String> appliedIssueIds = new LinkedHashSet<>(); | ||
| for (RemediationMetric metric : safeMetrics) { | ||
| Set<String> modifiedFiles = new LinkedHashSet<>(); | ||
| Map<String, Integer> skippedByReason = new LinkedHashMap<>(); | ||
| List<PreviewDetail> previewDetails = new ArrayList<>(); | ||
| boolean previewMode = false; | ||
| for (RemediationMetric metric : metrics) { | ||
| appliedIssueIds.addAll(metric.appliedIssueIds()); | ||
| accumulateFilesAndSkips(metric, modifiedFiles, skippedByReason); | ||
| if (metric instanceof RemediationMetric.Preview preview) { | ||
| previewMode = true; | ||
| previewDetails.addAll(preview.previewDetails()); | ||
| } | ||
| } | ||
| return RemediationMetric.filtered(requestedIssueIds, appliedIssueIds, modifiedFiles, skippedByReason); | ||
| return previewMode | ||
| ? RemediationMetric.previewFiltered(requestedIssueIds, appliedIssueIds, modifiedFiles, skippedByReason, previewDetails) | ||
| : RemediationMetric.filtered(requestedIssueIds, appliedIssueIds, modifiedFiles, skippedByReason); | ||
| } | ||
|
|
||
| private static void accumulateFilesAndSkips( | ||
|
|
@@ -92,7 +117,12 @@ public static String formatSkippedReasons(Map<String, Integer> skippedByReason) | |
| } | ||
|
|
||
| public static String actionLabel(RemediationMetric metric) { | ||
| return metric != null && metric.appliedRemediations() > 0 ? "Remediation-Applied" : "No-Remediation-Applied"; | ||
| boolean previewMode = metric instanceof RemediationMetric.Preview; | ||
| if (metric != null && metric.appliedRemediations() > 0) { | ||
| return previewMode ? "Remediation-Previewed" : "Remediation-Applied"; | ||
| } else { | ||
| return previewMode ? "No-Remediation-Previewed" : "No-Remediation-Applied"; | ||
| } | ||
| } | ||
|
|
||
| public static String na(String value) { | ||
|
|
@@ -118,10 +148,22 @@ public static void putRemediationMetricFields(ObjectNode result, RemediationMetr | |
| result.set("modifiedFiles", toArrayNode(modifiedFiles)); | ||
| } | ||
|
|
||
| /** Metric fields plus {@code __action__} (shared by SSC/FoD result builders). */ | ||
| /** Metric fields plus {@code __action__} and, for preview results, preview details (shared by SSC/FoD result builders). */ | ||
| public static void putMetricAndAction(ObjectNode result, RemediationMetric metric) { | ||
| putRemediationMetricFields(result, metric); | ||
| result.put(IActionCommandResultSupplier.actionFieldName, actionLabel(metric)); | ||
|
|
||
| if (metric instanceof RemediationMetric.Preview preview) { | ||
| result.set("previewDetails", toPreviewDetailsArray(preview.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; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although much better than before, I think definitions and responsibilities between abstract base class, concrete sub-classes, and options/mixins can be further improved.
Maybe something like the following?
ApplyRemediationsOptionsMixintoAbstractApplyRemediationsOptionsMixinvalidatemethod to this abstract mixin and its interface, calling individual validation methods for validating source selection, source dir,--issue-ids/--from-cacheinterdependency, ...So, basically, generic & command-specific options are provided through single mixin on FoD/SSC command classes, and those mixins provide the necessary validation logic. This allows the abstract base class to focus on process, instead of handling both process and data (validation).