diff --git a/src/Microsoft.ComponentDetection.Detectors/dockerfile/DockerfileComponentDetector.cs b/src/Microsoft.ComponentDetection.Detectors/dockerfile/DockerfileComponentDetector.cs index 00760e336..fa163d999 100644 --- a/src/Microsoft.ComponentDetection.Detectors/dockerfile/DockerfileComponentDetector.cs +++ b/src/Microsoft.ComponentDetection.Detectors/dockerfile/DockerfileComponentDetector.cs @@ -4,7 +4,6 @@ namespace Microsoft.ComponentDetection.Detectors.Dockerfile; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reactive.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.ComponentDetection.Common; @@ -46,18 +45,18 @@ public DockerfileComponentDetector( protected override IList SkippedFolders => ["node_modules"]; - protected override Task> OnPrepareDetectionAsync( - IObservable processRequests, - IDictionary detectorArgs, - CancellationToken cancellationToken = default) => - Task.FromResult(processRequests.Where(processRequest => - !this.IsInSkippedFolder(processRequest.ComponentStream.Location))); - protected override async Task OnFileFoundAsync(ProcessRequest processRequest, IDictionary detectorArgs, CancellationToken cancellationToken = default) { var singleFileComponentRecorder = processRequest.SingleFileComponentRecorder; var file = processRequest.ComponentStream; var filePath = file.Location; + var skippedFolder = this.SkippedFolders.FirstOrDefault(folder => filePath.Contains(folder)); + if (!string.IsNullOrEmpty(skippedFolder)) + { + this.Logger.LogDebug("Ignoring Dockerfile at {DockerfileLocation}, as it is inside a {SkippedFolder} folder.", filePath, skippedFolder); + return; + } + try { this.Logger.LogInformation("Discovered dockerfile: {Location}", file.Location); @@ -73,7 +72,7 @@ protected override async Task OnFileFoundAsync(ProcessRequest processRequest, ID } catch (ParseException e) { - this.Logger.LogWarning(e, "Ignoring file that doesn't appear to be a Dockerfile: {Location}", filePath); + this.Logger.LogDebug(e, "Ignoring file that doesn't appear to be a Dockerfile: {Location}", filePath); } catch (Exception e) { @@ -81,12 +80,6 @@ protected override async Task OnFileFoundAsync(ProcessRequest processRequest, ID } } - private bool IsInSkippedFolder(string filePath) - { - var pathSegments = filePath.Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries); - return this.SkippedFolders.Any(skippedFolder => pathSegments.Contains(skippedFolder, StringComparer.OrdinalIgnoreCase)); - } - private Task ParseDockerFileAsync(string fileContents, string fileLocation, ISingleFileComponentRecorder singleFileComponentRecorder, Dictionary stageNameMap) { var dockerfileModel = Dockerfile.Parse(fileContents); diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/DockerfileComponentDetectorTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/DockerfileComponentDetectorTests.cs index 90d685172..ed7f45ff6 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/DockerfileComponentDetectorTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/DockerfileComponentDetectorTests.cs @@ -229,12 +229,20 @@ public async Task TestDockerfile_ShikiLanguageDefinitionIsIgnoredAsync(string fi componentRecorder.GetDetectedComponents().Should().BeEmpty(); loggerMock.Verify( logger => logger.Log( - LogLevel.Warning, + LogLevel.Debug, It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny>()), Times.Once); + loggerMock.Verify( + logger => logger.Log( + LogLevel.Warning, + It.IsAny(), + It.IsAny(), + It.IsAny(), + It.IsAny>()), + Times.Never); loggerMock.Verify( logger => logger.Log( LogLevel.Error,