Skip to content

Commit

Permalink
chore: Improve Debricked import feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Jan 19, 2023
1 parent 86b0868 commit dab4f0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public final class ProgressHelper {
private static final String LINE_CLEAR = "\033[2K";
private static final String LINE_START = "\r";

private ProgressHelper() {}

public static final IProgressHelper createProgressHelper() {
if ( hasAnsiConsole ) { return new AnsiConsoleProgressHelper(); }
else if ( hasConsole ) { return new BasicConsoleProgressHelper(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.util.RawValue;
import com.fortify.cli.common.http.proxy.helper.ProxyHelper;
Expand All @@ -43,6 +44,8 @@
import com.fortify.cli.common.rest.runner.config.UnirestUnexpectedHttpResponseConfigurer;
import com.fortify.cli.common.rest.runner.config.UnirestUrlConfigConfigurer;
import com.fortify.cli.common.util.FixInjection;
import com.fortify.cli.common.util.ProgressHelper;
import com.fortify.cli.common.util.ProgressHelper.IProgressHelper;
import com.fortify.cli.common.util.StringUtils;
import com.fortify.cli.ssc.appversion_artifact.cli.cmd.AbstractSSCAppVersionArtifactUploadCommand;
import com.fortify.cli.ssc.appversion_artifact.cli.cmd.imprt.debricked.DebrickedLoginOptions.DebrickedAccessTokenCredentialOptions;
Expand All @@ -65,6 +68,7 @@ public class SSCAppVersionArtifactImportFromDebrickedCommand extends AbstractSSC
@Getter @Mixin private SSCOutputHelperMixins.ImportFromDebricked outputHelper;
@Mixin private DebrickedLoginOptions debrickedLoginOptions;
@Inject private GenericUnirestRunner debrickedUnirestRunner;
private final IProgressHelper progressHelper = ProgressHelper.createProgressHelper();

@Option(names = {"-e", "--engine-type"}, required = true, defaultValue = "DEBRICKED")
@Getter private String engineType;
Expand Down Expand Up @@ -97,14 +101,18 @@ protected File getFile() {

@Override
protected void preUpload(UnirestInstance unirest, File file) {
progressHelper.writeProgress("Status: Generating & downloading SBOM");
debrickedUnirestRunner.run(u->downloadSbom(u, file));
progressHelper.writeProgress("Status: Uploading SBOM to SSC");
}

@Override
protected void postUpload(UnirestInstance unirest, File file) {
if ( StringUtils.isBlank(fileName) ) {
file.delete();
}
progressHelper.writeProgress("Status: SBOM uploaded to SSC");
progressHelper.clearProgress();
}

private Void downloadSbom(UnirestInstance debrickedUnirest, File file) {
Expand Down Expand Up @@ -164,10 +172,15 @@ private String getRepositoryId(UnirestInstance debrickedUnirest) {
Integer.parseInt(repository);
return repository;
} catch ( NumberFormatException e ) {
JsonNode data = debrickedUnirest.get("/api/1.0/open/repositories/get-repositories-names-links")
.asObject(JsonNode.class)
ArrayNode data = debrickedUnirest.get("/api/1.0/open/repositories/get-repositories-names-links")
.asObject(ArrayNode.class)
.getBody();
return JsonHelper.evaluateJsonPath(data, "$[?(@.name == \""+repository+"\")].id", String.class);
ArrayNode repositoryIds = JsonHelper.evaluateJsonPath(data, "$[?(@.name == \""+repository+"\")].id", ArrayNode.class);
switch ( repositoryIds.size() ) {
case 0: throw new IllegalArgumentException(String.format("Debricked repository with name %s not found; please use full repository name like <org>/<repo>", repository));
case 1: return repositoryIds.get(0).asText();
default: throw new IllegalArgumentException(String.format("Multiple debricked repositories with name %s foundl please use repository id instead", repository));
}
}
}

Expand Down

0 comments on commit dab4f0f

Please sign in to comment.