Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,18 +45,18 @@ public DockerfileComponentDetector(

protected override IList<string> SkippedFolders => ["node_modules"];
Comment thread
Copilot marked this conversation as resolved.

protected override Task<IObservable<ProcessRequest>> OnPrepareDetectionAsync(
IObservable<ProcessRequest> processRequests,
IDictionary<string, string> detectorArgs,
CancellationToken cancellationToken = default) =>
Task.FromResult(processRequests.Where(processRequest =>
!this.IsInSkippedFolder(processRequest.ComponentStream.Location)));

protected override async Task OnFileFoundAsync(ProcessRequest processRequest, IDictionary<string, string> 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));
Comment thread
grvillic marked this conversation as resolved.
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);
Expand All @@ -73,20 +72,14 @@ 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)
{
this.Logger.LogError(e, "Failed to process Dockerfile: {Location}", filePath);
}
}

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<string, string> stageNameMap)
{
var dockerfileModel = Dockerfile.Parse(fileContents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Once);
loggerMock.Verify(
logger => logger.Log(
LogLevel.Warning,
It.IsAny<EventId>(),
It.IsAny<It.IsAnyType>(),
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
Times.Never);
loggerMock.Verify(
logger => logger.Log(
LogLevel.Error,
Expand Down
Loading