Skip to content

Commit

Permalink
Merge pull request #48 from siemens/development
Browse files Browse the repository at this point in the history
Merging Development to main
  • Loading branch information
MadanReddyK authored Jun 15, 2023
2 parents 2a66c59 + 6fa69a5 commit 05bb394
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/LCT.PackageIdentifier/NpmProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ public List<Component> ParsePackageLockJson(string filepath, CommonAppSettings a
GetComponentsForBom(filepath, appSettings, ref bundledComponents, ref lstComponentForBOM, ref noOfDevDependent, depencyComponentList);
}

// the below logic for angular 16+version due to package-lock.json file format change
if (dependencies == null)
{
var pacakages = jsonDeserialized["packages"];
if (pacakages?.Children() != null)
{
IEnumerable<JProperty> depencyComponentList = pacakages?.Children().OfType<JProperty>();
GetPackagesForBom(filepath,appSettings, ref bundledComponents, ref lstComponentForBOM,
ref noOfDevDependent, depencyComponentList);
}
}

if (appSettings.Npm.ExcludedComponents != null)
{
lstComponentForBOM = CommonHelper.RemoveExcludedComponents(lstComponentForBOM, appSettings.Npm.ExcludedComponents, ref noOfExcludedComponents);
Expand Down Expand Up @@ -117,6 +129,64 @@ public List<Component> ParsePackageLockJson(string filepath, CommonAppSettings a
return lstComponentForBOM;
}

private static void GetPackagesForBom(string filepath, CommonAppSettings appSettings, ref List<BundledComponents> bundledComponents, ref List<Component> lstComponentForBOM, ref int noOfDevDependent, IEnumerable<JProperty> depencyComponentList)
{
BomCreator.bomKpiData.ComponentsinPackageLockJsonFile += depencyComponentList.Count();

foreach (JProperty prop in depencyComponentList)
{
if (string.IsNullOrEmpty(prop.Name))
{
BomCreator.bomKpiData.ComponentsinPackageLockJsonFile--;
continue;
}

Component components = new Component();
var properties = JObject.Parse(Convert.ToString(prop.Value));

// ignoring the dev= true components, because they are not needed in clearing
if (IsDevDependency(appSettings.RemoveDevDependency, prop.Value[Dev], ref noOfDevDependent))
{
continue;
}

string folderPath = CommonHelper.TrimEndOfString(filepath, $"\\{FileConstant.PackageLockFileName}");
string packageName = CommonHelper.GetSubstringOfLastOccurance(prop.Name, $"node_modules/");
string componentName = packageName.StartsWith('@') ? packageName.Replace("@", "%40") : packageName;

if (packageName.Contains('@'))
{
components.Group = packageName.Split('/')[0];
components.Name = packageName.Split('/')[1];
}
else
{
components.Name = packageName;
}

components.Description = folderPath;
components.Version = Convert.ToString(properties[Version]);
components.Purl = $"{ApiConstant.NPMExternalID}{componentName}@{components.Version}";
components.BomRef = $"{ApiConstant.NPMExternalID}{componentName}@{components.Version}";

CheckAndAddToBundleComponents(bundledComponents, prop, components);

lstComponentForBOM.Add(components);
lstComponentForBOM = RemoveBundledComponentFromList(bundledComponents, lstComponentForBOM);
}
}

private static void CheckAndAddToBundleComponents(List<BundledComponents> bundledComponents, JProperty prop, Component components)
{
if (prop.Value[Bundled] != null &&
!(bundledComponents.Any(x => x.Name == components.Name && x.Version.ToLowerInvariant() == components.Version)))
{
BundledComponents component = new() { Name = components.Name, Version = components.Version };
bundledComponents.Add(component);
}
}


private void GetComponentsForBom(string filepath, CommonAppSettings appSettings,
ref List<BundledComponents> bundledComponents, ref List<Component> lstComponentForBOM,
ref int noOfDevDependent, IEnumerable<JProperty> depencyComponentList)
Expand Down

0 comments on commit 05bb394

Please sign in to comment.