Skip to content

Commit

Permalink
chore: FoD command refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Oct 23, 2023
1 parent 8ada4d7 commit d71ef52
Show file tree
Hide file tree
Showing 32 changed files with 204 additions and 303 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

Expand All @@ -27,6 +28,7 @@
*/
@Retention(RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface CommandGroup {
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@
package com.fortify.cli.fod._common.scan.cli.cmd;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.common.output.transform.IRecordTransformer;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.rest.FoDUrls;
import com.fortify.cli.fod._common.scan.cli.mixin.FoDScanResolverMixin;
import com.fortify.cli.fod._common.scan.helper.FoDScanDescriptor;
import com.fortify.cli.fod._common.scan.helper.FoDScanHelper;
import com.fortify.cli.fod._common.scan.helper.FoDScanType;

import kong.unirest.UnirestInstance;
import picocli.CommandLine.Mixin;

public abstract class AbstractFoDScanCancelCommand extends AbstractFoDJsonNodeOutputCommand implements IRecordTransformer, IActionCommandResultSupplier {
@CommandGroup("*-scan")
public abstract class AbstractFoDScanCancelCommand extends AbstractFoDJsonNodeOutputCommand implements IActionCommandResultSupplier {
@Mixin private FoDScanResolverMixin.PositionalParameter scanResolver;

@Override
Expand All @@ -40,10 +40,6 @@ public final JsonNode getJsonNode(UnirestInstance unirest) {

protected abstract FoDScanType getScanType();

public final JsonNode transformRecord(JsonNode record) {
return FoDScanHelper.renameFields(record);
}

@Override
public final String getActionCommandResult() {
return "CANCELLED";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package com.fortify.cli.fod._common.scan.cli.cmd;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.json.JsonNodeHolder;
import com.fortify.cli.common.util.DisableTest;
import com.fortify.cli.common.util.DisableTest.TestType;
Expand All @@ -25,18 +25,18 @@
import kong.unirest.UnirestInstance;
import picocli.CommandLine.Mixin;

@DisableTest(TestType.CMD_DEFAULT_TABLE_OPTIONS_PRESENT)
@DisableTest(TestType.CMD_DEFAULT_TABLE_OPTIONS_PRESENT) @CommandGroup("*-scan-config")
public abstract class AbstractFoDScanConfigGetCommand extends AbstractFoDJsonNodeOutputCommand {
@Mixin private FoDDelimiterMixin delimiterMixin; // Is automatically injected in resolver mixins
@Mixin private FoDReleaseByQualifiedNameOrIdResolverMixin.RequiredOption releaseResolver;

@Override
public final JsonNode getJsonNode(UnirestInstance unirest) {
var releaseId = releaseResolver.getReleaseId(unirest);
var result = getDescriptor(unirest, releaseId).asJsonNode();
var result = getDescriptor(unirest, releaseId).asObjectNode();
return result.get("assessmentTypeId").asText().equals("0")
? new ObjectMapper().createObjectNode().put("state", "Not configured")
: result;
? result.put("state", "Not configured")
: result.put("state", "Configured");
}

protected abstract JsonNodeHolder getDescriptor(UnirestInstance unirest, String releaseId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.cli.mixin.CommonOptionMixins;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
Expand All @@ -29,6 +30,7 @@
import lombok.SneakyThrows;
import picocli.CommandLine.Mixin;

@CommandGroup("*-scan-download")
public abstract class AbstractFoDScanDownloadCommand extends AbstractFoDJsonNodeOutputCommand implements IActionCommandResultSupplier {
@Mixin private FoDDelimiterMixin delimiterMixin; // Is automatically injected in resolver mixins
@Mixin private FoDScanResolverMixin.PositionalParameter scanResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public abstract class AbstractFoDScanDownloadFprCommand extends AbstractFoDScanDownloadCommand {
@Override
protected GetRequest getDownloadRequest(UnirestInstance unirest, FoDScanDescriptor scanDescriptor) {
return unirest.get("GET /api/v3/scans/{scanId}/fpr")
return unirest.get("/api/v3/scans/{scanId}/fpr")
.routeParam("scanId", scanDescriptor.getScanId())
.accept("application/octet-stream")
.queryString("scanType", scanDescriptor.getScanType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.cli.mixin.CommonOptionMixins;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
Expand All @@ -30,6 +31,7 @@
import lombok.SneakyThrows;
import picocli.CommandLine.Mixin;

@CommandGroup("*-scan-download")
public abstract class AbstractFoDScanDownloadLatestCommand extends AbstractFoDJsonNodeOutputCommand implements IActionCommandResultSupplier {
@Mixin private FoDDelimiterMixin delimiterMixin; // Is automatically injected in resolver mixins
@Mixin private FoDReleaseByQualifiedNameOrIdResolverMixin.RequiredOption releaseResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
package com.fortify.cli.fod._common.scan.cli.cmd;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.output.transform.IRecordTransformer;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.scan.cli.mixin.FoDScanResolverMixin;
import com.fortify.cli.fod._common.scan.helper.FoDScanHelper;
import com.fortify.cli.fod._common.scan.helper.FoDScanType;

import kong.unirest.UnirestInstance;
import picocli.CommandLine.Mixin;

public abstract class AbstractFoDScanGetCommand extends AbstractFoDJsonNodeOutputCommand implements IRecordTransformer {
@CommandGroup("*-scan")
public abstract class AbstractFoDScanGetCommand extends AbstractFoDJsonNodeOutputCommand {
@Mixin private FoDScanResolverMixin.PositionalParameter scanResolver;

@Override
Expand All @@ -33,11 +33,6 @@ public JsonNode getJsonNode(UnirestInstance unirest) {

protected abstract FoDScanType getScanType();

@Override
public JsonNode transformRecord(JsonNode record) {
return FoDScanHelper.renameFields(record);
}

@Override
public boolean isSingular() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fortify.cli.common.cli.mixin.CommonOptionMixins;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
Expand All @@ -28,6 +29,7 @@
import kong.unirest.UnirestInstance;
import picocli.CommandLine.Mixin;

@CommandGroup("*-scan-import")
public abstract class AbstractFoDScanImportCommand extends AbstractFoDJsonNodeOutputCommand implements IActionCommandResultSupplier {
@Mixin private FoDDelimiterMixin delimiterMixin; // Is automatically injected in resolver mixins
@Mixin private FoDReleaseByQualifiedNameOrIdResolverMixin.RequiredOption releaseResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,37 @@
package com.fortify.cli.fod._common.scan.cli.cmd;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IRecordTransformer;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDBaseRequestOutputCommand;
import com.fortify.cli.fod._common.rest.FoDUrls;
import com.fortify.cli.fod._common.scan.helper.FoDScanHelper;
import com.fortify.cli.fod._common.scan.helper.FoDScanType;
import com.fortify.cli.fod.app.cli.mixin.FoDAppResolverMixin;
import com.fortify.cli.fod.release.cli.mixin.FoDReleaseByQualifiedNameOrIdResolverMixin;

import kong.unirest.HttpRequest;
import kong.unirest.UnirestInstance;
import picocli.CommandLine.Mixin;

@CommandGroup("*-scan")
public abstract class AbstractFoDScanListCommand extends AbstractFoDBaseRequestOutputCommand implements IRecordTransformer {
@Mixin private FoDDelimiterMixin delimiterMixin; // Is automatically injected in resolver mixins
@Mixin private FoDAppResolverMixin.OptionalOption appResolver;
@Mixin private FoDReleaseByQualifiedNameOrIdResolverMixin.OptionalOption releaseResolver;
@Mixin private FoDReleaseByQualifiedNameOrIdResolverMixin.RequiredOption releaseResolver;

@Override
public final HttpRequest<?> getBaseRequest(UnirestInstance unirest) {
var appId = appResolver.getAppId(unirest);
var releaseId = releaseResolver.getReleaseId(unirest);
HttpRequest<?> baseRequest;
if ( appId!=null ) {
if (releaseId!=null) {
// specifying a release takes precedence over app as its more specific
baseRequest = unirest.get(FoDUrls.RELEASE_SCANS).routeParam("relId", releaseId);
} else {
baseRequest = unirest.get(FoDUrls.APP_SCANS).routeParam("appId", appId);
}
} else if ( releaseId!=null ) {
baseRequest = unirest.get(FoDUrls.RELEASE_SCANS).routeParam("relId", releaseId);
} else {
baseRequest = unirest.get(FoDUrls.SCANS);
}
var baseRequest = unirest.get(FoDUrls.RELEASE_SCANS).routeParam("relId", releaseId);
return FoDScanHelper.addDefaultScanListParams(baseRequest);
}

@Override
public final JsonNode transformRecord(JsonNode record) {
var scanType = getScanType();
return scanType!=null && !scanType.equals(record.get("scanType"))
return scanType!=null && !scanType.name().equals(record.get("scanType").asText())
? null
: FoDScanHelper.renameFields(record);
: record;
}

protected abstract FoDScanType getScanType();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* Copyright 2021, 2023 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.fod._common.scan.cli.cmd;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.fod._common.cli.mixin.FoDDelimiterMixin;
import com.fortify.cli.fod._common.output.cli.AbstractFoDJsonNodeOutputCommand;
import com.fortify.cli.fod._common.scan.helper.FoDScanDescriptor;
import com.fortify.cli.fod.release.cli.mixin.FoDReleaseByQualifiedNameOrIdResolverMixin;
import com.fortify.cli.fod.release.helper.FoDReleaseDescriptor;

import kong.unirest.UnirestInstance;
import picocli.CommandLine.Mixin;

@CommandGroup("*-scan")
public abstract class AbstractFoDScanStartCommand extends AbstractFoDJsonNodeOutputCommand implements IActionCommandResultSupplier {
@Mixin private FoDDelimiterMixin delimiterMixin; // Is automatically injected in resolver mixins
@Mixin private FoDReleaseByQualifiedNameOrIdResolverMixin.RequiredOption releaseResolver;

@Override
public final JsonNode getJsonNode(UnirestInstance unirest) {
var releaseDescriptor = releaseResolver.getReleaseDescriptor(unirest);
return startScan(unirest, releaseDescriptor).asJsonNode();
}

protected abstract FoDScanDescriptor startScan(UnirestInstance unirest, FoDReleaseDescriptor releaseDescriptor);

@Override
public final String getActionCommandResult() {
return "STARTED";
}

@Override
public final boolean isSingular() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

import java.util.Set;

import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.rest.cli.cmd.AbstractWaitForCommand;
import com.fortify.cli.common.rest.wait.WaitHelper.WaitHelperBuilder;
import com.fortify.cli.fod._common.output.mixin.FoDProductHelperStandardMixin;
import com.fortify.cli.fod._common.scan.cli.mixin.FoDScanResolverMixin;
import com.fortify.cli.fod._common.scan.helper.FoDScanHelper;
import com.fortify.cli.fod._common.scan.helper.FoDScanStatus;
import com.fortify.cli.fod._common.scan.helper.FoDScanStatus.FoDScanStatusIterable;
import com.fortify.cli.fod._common.scan.helper.FoDScanType;
Expand All @@ -29,6 +29,7 @@
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;

@CommandGroup("*-scan")
public abstract class AbstractFoDScanWaitForCommand extends AbstractWaitForCommand {
@Getter @Mixin FoDProductHelperStandardMixin productHelper;
@Mixin private FoDScanResolverMixin.PositionalParameterMulti scansResolver;
Expand All @@ -39,7 +40,6 @@ public abstract class AbstractFoDScanWaitForCommand extends AbstractWaitForComma
protected final WaitHelperBuilder configure(UnirestInstance unirest, WaitHelperBuilder builder) {
return builder
.recordsSupplier(scansResolver::getScanDescriptorJsonNodes)
.recordTransformer(FoDScanHelper::renameFields)
.currentStateProperty("analysisStatusType")
.knownStates(FoDScanStatus.getKnownStateNames())
.failureStates(FoDScanStatus.getFailureStateNames())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fortify.cli.common.json.JsonHelper;
import com.fortify.cli.common.output.transform.fields.RenameFieldsTransformer;
import com.fortify.cli.fod._common.rest.FoDUrls;
import com.fortify.cli.fod.rest.lookup.helper.FoDLookupDescriptor;
import com.fortify.cli.fod.rest.lookup.helper.FoDLookupHelper;
Expand All @@ -42,10 +41,6 @@ public class FoDScanHelper {
// max retention period (in years) of FPRs
public static int MAX_RETENTION_PERIOD = 2;

public static final JsonNode renameFields(JsonNode record) {
return new RenameFieldsTransformer(new String[]{}).transform(record);
}

public static final FoDScanDescriptor getScanDescriptor(UnirestInstance unirest, String scanId) {
var result = unirest.get(FoDUrls.SCAN + "/summary")
.routeParam("scanId", scanId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.formkiq.graalvm.annotations.Reflectable;
import com.fortify.cli.common.json.JsonNodeHolder;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package com.fortify.cli.fod._common.scan.helper.oss;

import java.io.File;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand All @@ -31,8 +33,6 @@
import kong.unirest.UnirestInstance;
import lombok.Getter;

import java.io.File;

public class FoDScanOssHelper extends FoDScanHelper {
@Getter
private static final ObjectMapper objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.fortify.cli.fod._common.scan.helper.oss;

import com.formkiq.graalvm.annotations.Reflectable;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
*******************************************************************************/
package com.fortify.cli.fod.app.cli.cmd;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.cli.util.CommandGroup;
import com.fortify.cli.common.output.cli.mixin.OutputHelperMixins;
import com.fortify.cli.common.output.transform.IRecordTransformer;
import com.fortify.cli.fod._common.output.cli.AbstractFoDBaseRequestOutputCommand;
import com.fortify.cli.fod._common.rest.FoDUrls;
import com.fortify.cli.fod._common.scan.helper.FoDScanHelper;
Expand All @@ -26,8 +25,8 @@
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;

@Command(name = "list-scans", aliases = "lss")
public class FoDAppScanListCommand extends AbstractFoDBaseRequestOutputCommand implements IRecordTransformer {
@Command(name = "list-scans", aliases = "lss") @CommandGroup("scan")
public class FoDAppScanListCommand extends AbstractFoDBaseRequestOutputCommand {
@Getter @Mixin private OutputHelperMixins.TableWithQuery outputHelper;
@Mixin private FoDAppResolverMixin.RequiredOption appResolver;

Expand All @@ -37,11 +36,6 @@ public HttpRequest<?> getBaseRequest(UnirestInstance unirest) {
.routeParam("appId", appResolver.getAppId(unirest)));
}

@Override
public JsonNode transformRecord(JsonNode record) {
return FoDScanHelper.renameFields(record);
}

@Override
public boolean isSingular() {
return false;
Expand Down
Loading

0 comments on commit d71ef52

Please sign in to comment.