-
Notifications
You must be signed in to change notification settings - Fork 113
Added author detection for Npm and Nuget Component #51
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
Changes from all commits
c5c0cf5
4c2a8d8
8f68abf
efcb7a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using System; | ||
|
|
||
| namespace Microsoft.ComponentDetection.Contracts.Internal | ||
| { | ||
| public class NpmAuthor | ||
| { | ||
| public NpmAuthor(string name, string email = null) | ||
| { | ||
| Name = name ?? throw new ArgumentNullException(nameof(name)); | ||
| Email = string.IsNullOrEmpty(email) ? null : email; | ||
| } | ||
|
|
||
| public string Name { get; set; } | ||
|
|
||
| public string Email { get; set; } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |||||||||||
| using Microsoft.ComponentDetection.Contracts.TypedComponent; | ||||||||||||
| using Newtonsoft.Json.Linq; | ||||||||||||
| using NuGet.Versioning; | ||||||||||||
| using System.Text.RegularExpressions; | ||||||||||||
|
|
||||||||||||
| namespace Microsoft.ComponentDetection.Detectors.Npm | ||||||||||||
| { | ||||||||||||
|
|
@@ -81,16 +82,68 @@ protected virtual bool ProcessIndividualPackageJTokens(string filePath, ISingleF | |||||||||||
| { | ||||||||||||
| var name = packageJToken["name"].ToString(); | ||||||||||||
| var version = packageJToken["version"].ToString(); | ||||||||||||
|
|
||||||||||||
| var authorToken = packageJToken["author"]; | ||||||||||||
|
|
||||||||||||
| if (!SemanticVersion.TryParse(version, out _)) | ||||||||||||
| { | ||||||||||||
| Logger.LogWarning($"Unable to parse version \"{version}\" for package \"{name}\" found at path \"{filePath}\". This may indicate an invalid npm package component and it will not be registered."); | ||||||||||||
| return false; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| var detectedComponent = new DetectedComponent(new NpmComponent(name, version)); | ||||||||||||
| singleFileComponentRecorder.RegisterUsage(detectedComponent); | ||||||||||||
| NpmComponent npmComponent = new NpmComponent(name, version, author: GetAuthor(authorToken, name, filePath)); | ||||||||||||
|
|
||||||||||||
| singleFileComponentRecorder.RegisterUsage(new DetectedComponent(npmComponent)); | ||||||||||||
| return true; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| private NpmAuthor GetAuthor(JToken authorToken, string packageName, string filePath) | ||||||||||||
| { | ||||||||||||
| var authorString = authorToken?.ToString(); | ||||||||||||
| if (string.IsNullOrEmpty(authorString)) | ||||||||||||
| { | ||||||||||||
| return null; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| string authorName; | ||||||||||||
| string authorEmail; | ||||||||||||
| string authorSingleStringPattern = @"^(?<name>([^<(]+?)?)[ \t]*(?:<(?<email>([^>(]+?))>)?[ \t]*(?:\(([^)]+?)\)|$)"; | ||||||||||||
| Match authorMatch = new Regex(authorSingleStringPattern).Match(authorString); | ||||||||||||
|
|
||||||||||||
| /* | ||||||||||||
| * for parsing author in Json Format | ||||||||||||
| * for e.g. | ||||||||||||
| * "author": { | ||||||||||||
| * "name": "John Doe", | ||||||||||||
| * "email": "johndoe@outlook.com", | ||||||||||||
| * "name": "https://jd.com", | ||||||||||||
| */ | ||||||||||||
| if (authorToken.HasValues) | ||||||||||||
| { | ||||||||||||
| authorName = authorToken["name"]?.ToString(); | ||||||||||||
| authorEmail = authorToken["email"]?.ToString(); | ||||||||||||
|
|
||||||||||||
| /* | ||||||||||||
| * for parsing author in single string format. | ||||||||||||
| * for e.g. | ||||||||||||
| * "author": "John Doe <johdoe@outlook.com> https://jd.com" | ||||||||||||
| */ | ||||||||||||
| } else if (authorMatch.Success) | ||||||||||||
| { | ||||||||||||
| authorName = authorMatch.Groups["name"].ToString().Trim(); | ||||||||||||
| authorEmail = authorMatch.Groups["email"].ToString().Trim(); | ||||||||||||
| } else | ||||||||||||
| { | ||||||||||||
|
Comment on lines
+134
to
+135
Member
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.
Suggested change
|
||||||||||||
| Logger.LogWarning("Unable to parse author:[{authorString}] for package:[{packageName}] found at path:[{filePath}]. This may indicate an invalid npm package author, and author will not be registered."); | ||||||||||||
|
Member
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 needs
Suggested change
|
||||||||||||
| return null; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if (string.IsNullOrEmpty(authorName)) | ||||||||||||
| { | ||||||||||||
| Logger.LogWarning("Unable to parse author:[{authorString}] for package:[{packageName}] found at path:[{filePath}]. This may indicate an invalid npm package author, and author will not be registered."); | ||||||||||||
|
Member
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. Same here
Suggested change
|
||||||||||||
| return null; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return new NpmAuthor(authorName, authorEmail); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
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.