Skip to content

Conversation

@RushabhBhansali
Copy link
Contributor

@RushabhBhansali RushabhBhansali commented Jan 24, 2022

Added author Detection for NpmComponents and NugetComponent

Executive Order requirements 4. e.x. and 4.e.vi. require provenance (origination) information of both open-source software and software code and components.

This change enables component detection to extract author information for NPM packages from package.json files and for Nuget Component from .nuspec file.

@RushabhBhansali RushabhBhansali requested a review from a team as a code owner January 24, 2022 19:13
@github-actions
Copy link

👋 Hi! It looks like you modified some files in the Detectors folder.
You may need to bump the detector versions if any of the following scenarios apply:

  • The detector detects more or fewer components than before
  • The detector generates different parent/child graph relationships than before
  • The detector generates different devDependencies values than before

If none of the above scenarios apply, feel free to ignore this comment 🙂

@RushabhBhansali RushabhBhansali changed the title Added author detection for NpmComponents Added author detection for Npm and Nuget Component Jan 25, 2022
// for e.g. "author": "John Doe <johnDoe@outlook.com> (https://jd.com)"
else
{
bool isEmailPresent = authorString.Contains("<");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be wary of such a simple validation here. Expecting the data to be well formed may not always be accurate and trying to exact match may lead to unexpected errors getting thrown.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

introduced JSON and regex-based verification of author.


string name = metadataNode["id"].InnerText;
string version = metadataNode["version"].InnerText;
string[] authors = metadataNode["authors"]?.InnerText.Split(",").Select(email => email.Trim()).ToArray();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: not an email form your .Select

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

}

var detectedComponent = new DetectedComponent(new NpmComponent(name, version));
var npmComponent = new NpmComponent(name, version);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update the class to have a constructor that takes in an optional NpmAuthor parameter? Having to create it and then do .Author on the next line seems unnecessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, for an optional parameter, something like

var npmComponent = new NpmComponent(name, version)
{
    Author = GetAuthor(authorToken, name, filePath),
};

But it's a style choice between those two options

Copy link
Contributor Author

@RushabhBhansali RushabhBhansali Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going with Coby's recommendation of introducing optional parameter in constructors, since I see that being used elsewhere in the code base.

return ("package.json", packageJsonTemplate, Path.Combine(Path.GetTempPath(), "package.json"));
}

public static (string, string, string) GetPackageJsonNoDependenciesForNameAndVersion(string packageName, string packageVersion)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General testing comment: try to introduce some cases with malformed data that shouldn't exist, but we should cover this scenario without throwing exceptions up the stack

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added test for malformed values.

@JamieMagee JamieMagee added detector:npm The npm detector detector:nuget The NuGet detector type:feature Feature (new functionality) labels Jan 26, 2022
@RushabhBhansali RushabhBhansali force-pushed the users/rbhansali/provenance-npmComponent branch from 91fd379 to ab4b156 Compare January 26, 2022 21:57
Added author detection for npmComponents and NugetComponents
@RushabhBhansali RushabhBhansali force-pushed the users/rbhansali/provenance-npmComponent branch from ab4b156 to 8f68abf Compare January 26, 2022 22:19
@RushabhBhansali RushabhBhansali enabled auto-merge (squash) January 26, 2022 23:47
Comment on lines +130 to +131
} else if (authorMatch.Success)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (authorMatch.Success)
{
}
else if (authorMatch.Success)
{

Comment on lines +134 to +135
} else
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else
{
}
else
{

authorEmail = authorMatch.Groups["email"].ToString().Trim();
} else
{
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.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs $ to make sure the strings are interpolated.

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.");
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.");


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.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

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.");
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.");

Copy link
Member

@JamieMagee JamieMagee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of small comments. Fixes provided inline. Approving so as not to block.

@RushabhBhansali RushabhBhansali merged commit c05d165 into main Jan 27, 2022
@RushabhBhansali RushabhBhansali deleted the users/rbhansali/provenance-npmComponent branch January 27, 2022 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

detector:npm The npm detector detector:nuget The NuGet detector type:feature Feature (new functionality)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants