Skip to content

Commit

Permalink
Merge branch 'master' into sb_changeDiWorkingDir
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebillings committed Apr 21, 2020
2 parents 0f23c8d + 594bc6b commit aa8c0a8
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.synopsys.integration.configuration.util;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -52,6 +53,15 @@ public Optional<T> toOptional() {
return Optional.ofNullable(value);
}

public <U> Bdo<U> map(final Function<? super T, U> mapper) {
return Bdo.of(mapper.apply(value));
}

public Bdo<T> peek(final Consumer<T> consumer) {
consumer.accept(value);
return this;
}

public boolean isPresent() {
return toOptional().isPresent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -88,6 +89,10 @@ public <R> Bds<R> flatMap(final Function<? super T, ? extends Collection<? exten
return new Bds<>(stream.flatMap(streamMapper));
}

public void forEach(final Consumer<T> consumer) {
stream.forEach(consumer);
}

public Optional<T> minBy(final Comparator<? super T> comparator) {
return stream.min(comparator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ public DockerDetectable(final DetectableEnvironment environment, final DockerIns

@Override
public DetectableResult applicable() {
if (OperatingSystemType.determineFromSystem() == OperatingSystemType.WINDOWS) {
return new WrongOperatingSystemResult(OperatingSystemType.determineFromSystem());
}

if (!dockerDetectableOptions.hasDockerImageOrTar()) {
return new PropertyInsufficientDetectableResult();
}
Expand All @@ -83,6 +79,9 @@ public DetectableResult applicable() {

@Override
public DetectableResult extractable() throws DetectableException {
if (OperatingSystemType.determineFromSystem() == OperatingSystemType.WINDOWS) {
return new WrongOperatingSystemResult(OperatingSystemType.determineFromSystem());
}
javaExe = javaResolver.resolveJava();
if (javaExe == null) {
return new ExecutableNotFoundDetectableResult("java");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ public class YarnLockDetectable extends Detectable {

private final FileFinder fileFinder;
private final YarnLockExtractor yarnLockExtractor;
private final boolean productionOnly;
private final YarnLockOptions yarnLockOptions;

private File yarnLock;
private File packageJson;

public YarnLockDetectable(final DetectableEnvironment environment, final FileFinder fileFinder, final YarnLockExtractor yarnLockExtractor, boolean productionOnly) {
public YarnLockDetectable(final DetectableEnvironment environment, final FileFinder fileFinder, final YarnLockExtractor yarnLockExtractor, final YarnLockOptions yarnLockOptions) {
super(environment);
this.fileFinder = fileFinder;
this.yarnLockExtractor = yarnLockExtractor;
this.productionOnly = productionOnly; //TODO: Should this be in an options object? -jp
this.yarnLockOptions = yarnLockOptions;
}

@Override
Expand All @@ -75,6 +75,6 @@ public DetectableResult extractable() {

@Override
public Extraction extract(final ExtractionEnvironment extractionEnvironment) {
return yarnLockExtractor.extract(yarnLock, packageJson, productionOnly);
return yarnLockExtractor.extract(yarnLock, packageJson, yarnLockOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,23 @@ public YarnLockExtractor(final YarnLockParser yarnLockParser, final YarnTransfor
this.gson = gson;
}

public Extraction extract(final File yarnLockFile, final File packageJsonFile, boolean productionOnly) {
public Extraction extract(final File yarnLockFile, final File packageJsonFile, final YarnLockOptions yarnLockOptions) {
try {
final String packageJsonText = FileUtils.readFileToString(packageJsonFile, StandardCharsets.UTF_8);
final PackageJson packageJson = gson.fromJson(packageJsonText, PackageJson.class);

final List<String> yarnLockLines = FileUtils.readLines(yarnLockFile, StandardCharsets.UTF_8);
final YarnLock yarnLock = yarnLockParser.parseYarnLock(yarnLockLines);

final DependencyGraph dependencyGraph = yarnTransformer.transform(packageJson, yarnLock, productionOnly);
final DependencyGraph dependencyGraph = yarnTransformer.transform(packageJson, yarnLock, yarnLockOptions.useProductionOnly());

final CodeLocation detectCodeLocation = new CodeLocation(dependencyGraph);

return new Extraction.Builder().success(detectCodeLocation).build();
return new Extraction.Builder()
.projectName(packageJson.name)
.projectVersion(packageJson.version)
.success(detectCodeLocation)
.build();
} catch (final Exception e) {
return new Extraction.Builder().exception(e).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
import com.synopsys.integration.detectable.detectables.swift.SwiftPackageTransformer;
import com.synopsys.integration.detectable.detectables.yarn.YarnLockDetectable;
import com.synopsys.integration.detectable.detectables.yarn.YarnLockExtractor;
import com.synopsys.integration.detectable.detectables.yarn.YarnLockOptions;
import com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockParser;
import com.synopsys.integration.detectable.detectables.yarn.parse.YarnTransformer;

Expand Down Expand Up @@ -362,8 +363,8 @@ public SwiftCliDetectable createSwiftCliDetectable(final DetectableEnvironment e
return new SwiftCliDetectable(environment, fileFinder, swiftExtractor(), swiftResolver);
}

public YarnLockDetectable createYarnLockDetectable(final DetectableEnvironment environment, final boolean includeDevDependencies) {
return new YarnLockDetectable(environment, fileFinder, yarnLockExtractor(), includeDevDependencies);
public YarnLockDetectable createYarnLockDetectable(final DetectableEnvironment environment, final YarnLockOptions yarnLockOptions) {
return new YarnLockDetectable(environment, fileFinder, yarnLockExtractor(), yarnLockOptions);
}

//#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected void setup() throws IOException {
final ExecutableOutput bitbakeGOutput = createStandardOutput(
""
);
addExecutableOutput(bitbakeGOutput, "bash", "-c", "source " + getSourceDirectory().toFile().getCanonicalPath() + "/oe-init-build-env; " + "bitbake " + "-g " + "core-image-minimal");
addExecutableOutput(bitbakeGOutput, "bash", "-c", "source " + getSourceDirectory().toFile().getCanonicalPath() + File.separator + "oe-init-build-env; " + "bitbake " + "-g " + "core-image-minimal");

addFile(Paths.get("task-depends.dot"),
"digraph depends {",
Expand All @@ -62,7 +62,7 @@ protected void setup() throws IOException {
"base-passwd:",
" meta 3.5.29"
);
addExecutableOutput(bitbakeShowRecipesOutput, "bash", "-c", "source " + getSourceDirectory().toFile().getCanonicalPath() + "/oe-init-build-env; " + "bitbake-layers show-recipes");
addExecutableOutput(bitbakeShowRecipesOutput, "bash", "-c", "source " + getSourceDirectory().toFile().getCanonicalPath() + File.separator + "oe-init-build-env; " + "bitbake-layers show-recipes");
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.io.File;
import java.util.Optional;

import org.apache.commons.lang3.SystemUtils;
import org.json.JSONException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;

Expand All @@ -48,6 +50,8 @@ public class GradleReportParserFunctionalTest {

@Test
void extractCodeLocationTest() {
Assumptions.assumeFalse(SystemUtils.IS_OS_WINDOWS); //Does not work on windows due to path issues.

final GradleReportParser gradleReportParser = new GradleReportParser();
final Optional<GradleReport> gradleReport = gradleReportParser.parseReport(FunctionalTestFiles.asFile("/gradle/dependencyGraph.txt"));
Assertions.assertTrue(gradleReport.isPresent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.synopsys.integration.detectable.Detectable;
import com.synopsys.integration.detectable.DetectableEnvironment;
import com.synopsys.integration.detectable.Extraction;
import com.synopsys.integration.detectable.detectable.codelocation.CodeLocation;
import com.synopsys.integration.detectable.detectables.yarn.YarnLockOptions;
import com.synopsys.integration.detectable.functional.DetectableFunctionalTest;
import com.synopsys.integration.detectable.util.graph.NameVersionGraphAssert;

Expand All @@ -56,6 +58,7 @@ protected void setup() throws IOException {
addFile(Paths.get("package.json"),
"{",
" \"name\": \"babel\",",
" \"version\": \"1.2.3\",",
" \"private\": true,",
" \"license\": \"MIT\",",
" \"dependencies\": { ",
Expand All @@ -69,14 +72,18 @@ protected void setup() throws IOException {
@NotNull
@Override
public Detectable create(@NotNull final DetectableEnvironment detectableEnvironment) {
return detectableFactory.createYarnLockDetectable(detectableEnvironment, true);
return detectableFactory.createYarnLockDetectable(detectableEnvironment, new YarnLockOptions(true));
}

@Override
public void assertExtraction(@NotNull final Extraction extraction) {
Assertions.assertEquals(1, extraction.getCodeLocations().size());
final CodeLocation codeLocation = extraction.getCodeLocations().get(0);

NameVersionGraphAssert graphAssert = new NameVersionGraphAssert(Forge.NPMJS, extraction.getCodeLocations().get(0).getDependencyGraph());
Assertions.assertEquals("babel", extraction.getProjectName());
Assertions.assertEquals("1.2.3", extraction.getProjectVersion());

final NameVersionGraphAssert graphAssert = new NameVersionGraphAssert(Forge.NPMJS, codeLocation.getDependencyGraph());
graphAssert.hasRootSize(2);
graphAssert.hasRootDependency("async", "2.5.0");
graphAssert.hasRootDependency("lodash", "4.17.4");
Expand Down
3 changes: 3 additions & 0 deletions docs/templates/content/90-releasenotes.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

## Version 6.3.0
### New features
* (IDETECT-1917) The Yarn Detector will now extract project information from package.json files.
* (IDETECT-1867) ${solution_name} when configured to [fail on policy violations](../properties/configuration/project/#fail-on-policy-violation-severities), will now log policy violations.

### Changed features
* Users are now allowed to [upload source](../properties/configuration/signature scanner/#upload-source-mode) when only [license search](../properties/configuration/signature scanner/#signature-scanner-license-search) is provided. (IDETECT-1894)
* (IDETECT-1853) Detect is now compatible with Yocto 3.0.
* (IDETECT-1876) Detect will stop if docker is enabled on Windows.

### Resolved issues
* Resolved an issue where git extraction could fail if "git log" returned unexpected output. The commit hash will be used as a version as a last resort. (IDETECT-1906, [\#114](https://github.com/blackducksoftware/synopsys-detect/issues/114))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public void run(final ApplicationArguments applicationArguments) {
}

//Create status output file.
logger.info("");
try {
if (detectBootResultOptional.isPresent() && detectBootResultOptional.get().getDirectoryManager().isPresent()) {
DirectoryManager directoryManager = detectBootResultOptional.get().getDirectoryManager().get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
import com.synopsys.integration.detect.workflow.status.StatusType;
import com.synopsys.integration.detectable.detectable.executable.ExecutableRunner;
import com.synopsys.integration.detectable.detectable.file.impl.SimpleFileFinder;
import com.synopsys.integration.detectable.detectable.result.DetectableResult;
import com.synopsys.integration.detectable.detectable.result.WrongOperatingSystemResult;
import com.synopsys.integration.detector.base.DetectorType;
import com.synopsys.integration.detector.evaluation.DetectorEvaluationOptions;
import com.synopsys.integration.detector.finder.DetectorFinder;
Expand Down Expand Up @@ -188,6 +190,13 @@ private UniversalToolsResult runUniversalProjectTools(final PropertyConfiguratio
extractionEnvironmentProvider, codeLocationConverter, "DOCKER", DetectTool.DOCKER,
eventSystem);
final DetectableToolResult detectableToolResult = detectableTool.execute(directoryManager.getSourceDirectory());
if (detectableToolResult.getFailedExtractableResult().isPresent()) {
//TODO: Remove hack when windows docker support added. This workaround allows docker to throw a user friendly exception when not-extractable due to operating system.
DetectableResult extractable = detectableToolResult.getFailedExtractableResult().get();
if (WrongOperatingSystemResult.class.isAssignableFrom(extractable.getClass())) {
throw new DetectUserFriendlyException("Docker currently requires a non-Windows OS to run. Attempting to run Docker on Windows is not currently supported.", ExitCodeType.FAILURE_CONFIGURATION);
}
}
runResult.addDetectableToolResult(detectableToolResult);
anythingFailed = anythingFailed || detectableToolResult.isFailure();
logger.info("Docker actions finished.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ public DetectableToolResult execute(final File sourcePath) { //TODO: Caller publ
final DetectableResult applicable = detectable.applicable();

if (!applicable.getPassed()) {
logger.info("Was not applicable.");
logger.debug("Was not applicable.");
return DetectableToolResult.skip();
}

logger.info("Applicable passed.");
logger.debug("Applicable passed.");

DetectableResult extractable;
try {
Expand All @@ -101,10 +101,10 @@ public DetectableToolResult execute(final File sourcePath) { //TODO: Caller publ
logger.error("Was not extractable: " + extractable.toDescription());
eventSystem.publishEvent(Event.StatusSummary, new Status(name, StatusType.FAILURE));
eventSystem.publishEvent(Event.ExitCode, new ExitCodeRequest(ExitCodeType.FAILURE_GENERAL_ERROR, extractable.toDescription()));
return DetectableToolResult.failed();
return DetectableToolResult.failed(extractable);
}

logger.info("Extractable passed.");
logger.debug("Extractable passed.");

final ExtractionEnvironment extractionEnvironment = extractionEnvironmentProvider.createExtractionEnvironment(name);
final Extraction extraction = detectable.extract(extractionEnvironment);
Expand All @@ -115,7 +115,7 @@ public DetectableToolResult execute(final File sourcePath) { //TODO: Caller publ
eventSystem.publishEvent(Event.ExitCode, new ExitCodeRequest(ExitCodeType.FAILURE_GENERAL_ERROR, extractable.toDescription()));
return DetectableToolResult.failed();
} else {
logger.info("Extraction success.");
logger.debug("Extraction success.");
eventSystem.publishEvent(Event.StatusSummary, new Status(name, StatusType.SUCCESS));
}

Expand All @@ -132,7 +132,7 @@ public DetectableToolResult execute(final File sourcePath) { //TODO: Caller publ
projectInfo = new DetectToolProjectInfo(detectTool, nameVersion);
}

logger.info("Tool finished.");
logger.debug("Tool finished.");

return DetectableToolResult.success(detectCodeLocations, projectInfo, dockerTar);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.synopsys.integration.detect.workflow.codelocation.DetectCodeLocation;
import com.synopsys.integration.detect.workflow.project.DetectToolProjectInfo;
import com.synopsys.integration.detectable.detectable.result.DetectableResult;

public class DetectableToolResult {
private enum DetectableToolResultType {
Expand All @@ -46,24 +47,34 @@ private enum DetectableToolResultType {
private final File dockerTar;
@Nullable
private final DetectToolProjectInfo detectToolProjectInfo;
@Nullable
private final DetectableResult failedExtractableResult;

public DetectableToolResult(final DetectableToolResultType resultType, final DetectToolProjectInfo detectToolProjectInfo, final List<DetectCodeLocation> detectCodeLocations, final File dockerTar) {
public DetectableToolResult(final DetectableToolResultType resultType, final DetectToolProjectInfo detectToolProjectInfo, final List<DetectCodeLocation> detectCodeLocations, final File dockerTar,
final DetectableResult failedExtractableResult) {
this.resultType = resultType;
this.detectToolProjectInfo = detectToolProjectInfo;
this.detectCodeLocations = detectCodeLocations;
this.dockerTar = dockerTar;
this.failedExtractableResult = failedExtractableResult;
}

public static DetectableToolResult skip() {
return new DetectableToolResult(DetectableToolResultType.SKIPPED, null, Collections.emptyList(), null);
return new DetectableToolResult(DetectableToolResultType.SKIPPED, null, Collections.emptyList(), null, null);
}

//extractableResult is a workaround for docker. Technically we want to throw an exception when docker extractable fails for Windows but neither the tool nor the detectable supports that.
//This allows the caller to make determinations based on the extractable result.
public static DetectableToolResult failed(DetectableResult extractableResult) {
return new DetectableToolResult(DetectableToolResultType.FAILED, null, Collections.emptyList(), null, extractableResult);
}

public static DetectableToolResult failed() {
return new DetectableToolResult(DetectableToolResultType.FAILED, null, Collections.emptyList(), null);
return failed(null);
}

public static DetectableToolResult success(final List<DetectCodeLocation> codeLocations, @Nullable final DetectToolProjectInfo projectInfo, @Nullable final File dockerTar) {
return new DetectableToolResult(DetectableToolResultType.SUCCESS, projectInfo, codeLocations, dockerTar);
return new DetectableToolResult(DetectableToolResultType.SUCCESS, projectInfo, codeLocations, dockerTar, null);
}

public Optional<File> getDockerTar() {
Expand All @@ -81,4 +92,8 @@ public List<DetectCodeLocation> getDetectCodeLocations() {
public boolean isFailure() {
return resultType == DetectableToolResultType.FAILED;
}

public Optional<DetectableResult> getFailedExtractableResult() {
return Optional.ofNullable(failedExtractableResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public SwiftCliDetectable createSwiftCliDetectable(final DetectableEnvironment e
}

public YarnLockDetectable createYarnLockDetectable(final DetectableEnvironment environment) {
return detectableFactory.createYarnLockDetectable(environment, detectableOptionFactory.createYarnLockOptions().useProductionOnly());
return detectableFactory.createYarnLockDetectable(environment, detectableOptionFactory.createYarnLockOptions());
}

}
Loading

0 comments on commit aa8c0a8

Please sign in to comment.