-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add a new detector MvnPomCliComponentDetector
#544
Open
AbhinavAbhinav11
wants to merge
3
commits into
microsoft:main
Choose a base branch
from
AbhinavAbhinav11:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+316
−1
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
src/Microsoft.ComponentDetection.Detectors/maven/MvnPomCliComponentDetector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
namespace Microsoft.ComponentDetection.Detectors.Maven; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Xml; | ||
using Microsoft.ComponentDetection.Contracts; | ||
using Microsoft.ComponentDetection.Contracts.BcdeModels; | ||
using Microsoft.ComponentDetection.Contracts.Internal; | ||
using Microsoft.ComponentDetection.Contracts.TypedComponent; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public class MvnPomCliComponentDetector : FileComponentDetector | ||
{ | ||
public MvnPomCliComponentDetector( | ||
IComponentStreamEnumerableFactory componentStreamEnumerableFactory, | ||
IObservableDirectoryWalkerFactory walkerFactory, | ||
ILogger<MvnCliComponentDetector> logger) | ||
{ | ||
this.ComponentStreamEnumerableFactory = componentStreamEnumerableFactory; | ||
this.Scanner = walkerFactory; | ||
this.Logger = logger; | ||
} | ||
|
||
public override string Id => "MvnPomCli"; | ||
|
||
public override IList<string> SearchPatterns => new List<string>() { "*.pom" }; | ||
|
||
public override IEnumerable<string> Categories => new[] { Enum.GetName(typeof(DetectorClass), DetectorClass.Maven) }; | ||
|
||
public override IEnumerable<ComponentType> SupportedComponentTypes => new[] { ComponentType.Maven }; | ||
|
||
public override int Version => 2; | ||
|
||
protected override async Task OnFileFoundAsync(ProcessRequest processRequest, IDictionary<string, string> detectorArgs) | ||
{ | ||
await this.ProcessFileAsync(processRequest); | ||
} | ||
|
||
private async Task ProcessFileAsync(ProcessRequest processRequest) | ||
{ | ||
var singleFileComponentRecorder = processRequest.SingleFileComponentRecorder; | ||
var stream = processRequest.ComponentStream; | ||
|
||
try | ||
{ | ||
byte[] pomBytes = null; | ||
|
||
if ("*.pom".Equals(stream.Pattern, StringComparison.OrdinalIgnoreCase)) | ||
AbhinavAbhinav11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
using (var contentStream = File.Open(stream.Location, FileMode.Open)) | ||
{ | ||
pomBytes = new byte[contentStream.Length]; | ||
await contentStream.ReadAsync(pomBytes.AsMemory(0, (int)contentStream.Length)); | ||
|
||
using var pomStream = new MemoryStream(pomBytes, false); | ||
var doc = new XmlDocument(); | ||
doc.Load(pomStream); | ||
|
||
var nsmgr = new XmlNamespaceManager(doc.NameTable); | ||
nsmgr.AddNamespace("ns", "http://maven.apache.org/POM/4.0.0"); | ||
|
||
var dependencies = doc.SelectSingleNode("//ns:project/ns:dependencies", nsmgr); | ||
if (dependencies == null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (XmlNode node in dependencies.ChildNodes) | ||
melotic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
this.RegisterComponent(node, nsmgr, singleFileComponentRecorder); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
return; | ||
AbhinavAbhinav11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
// If something went wrong, just ignore the component | ||
this.Logger.LogError(e, "Error parsing pom maven component from {PomLocation}", stream.Location); | ||
singleFileComponentRecorder.RegisterPackageParseFailure(stream.Location); | ||
} | ||
} | ||
|
||
private void RegisterComponent(XmlNode node, XmlNamespaceManager nsmgr, ISingleFileComponentRecorder singleFileComponentRecorder) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method looks the same as |
||
{ | ||
var groupIdNode = node.SelectSingleNode("ns:groupId", nsmgr); | ||
var artifactIdNode = node.SelectSingleNode("ns:artifactId", nsmgr); | ||
var versionNode = node.SelectSingleNode("ns:version", nsmgr); | ||
|
||
if (groupIdNode == null || artifactIdNode == null || versionNode == null) | ||
{ | ||
return; | ||
AbhinavAbhinav11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var groupId = groupIdNode.InnerText; | ||
var artifactId = artifactIdNode.InnerText; | ||
var version = versionNode.InnerText; | ||
var dependencyScope = DependencyScope.MavenCompile; | ||
|
||
var component = new MavenComponent(groupId, artifactId, version); | ||
|
||
singleFileComponentRecorder.RegisterUsage( | ||
new DetectedComponent(component), | ||
isDevelopmentDependency: null, | ||
dependencyScope: dependencyScope); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also implement
IDefaultOffComponentDetector
? We don't want a new detector to be enabled by default (only explicitly opted-in) before we've had the chance to fully verify it correctly works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MavenPomComponentDetector
is markedIDefaultOffComponenDetector
, butMvnPomCliComponentDetector
is not