Skip to content

Commit

Permalink
Merge branch 'development' into Bug/Remove_Invalid_Npm_Dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
crvreddy authored Jan 31, 2025
2 parents 13e5ec6 + 536e1be commit 605b526
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 35 deletions.
28 changes: 13 additions & 15 deletions src/LCT.Common.UTests/CommonHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ public void RemoveMultipleExcludedComponents_ReturnSuccess()
{
//Arrange
List<Component> ComponentsForBom = new List<Component>();
ComponentsForBom.Add(new Component() { Name = "Debian", Version = "3.1.0" });
ComponentsForBom.Add(new Component() { Name = "Debian", Version = "3.1.1" });
ComponentsForBom.Add(new Component() { Name = "Debian", Version = "3.1.2" });
ComponentsForBom.Add(new Component() { Name = "Newton", Version = "3.1.3" });
ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.4" });
ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.5",Purl= "pkg:npm/foobar@12.3.1" });
ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.5", Purl = "pkg:npm/foobar@12.3.2" });
ComponentsForBom.Add(new Component() { Name = "Debian", Version = "3.1.0", Purl = "pkg:npm/Debian@3.1.0", Properties = new List<Property>()});
ComponentsForBom.Add(new Component() { Name = "Debian", Version = "3.1.1", Purl = "pkg:npm/Debian@3.1.1", Properties = new List<Property>() });
ComponentsForBom.Add(new Component() { Name = "Debian", Version = "3.1.2", Purl = "pkg:npm/Debian@3.1.2", Properties = new List<Property>() });
ComponentsForBom.Add(new Component() { Name = "Newton", Version = "3.1.3", Purl = "pkg:npm/Newton@3.1.3", Properties = new List<Property>() });
ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.4", Purl = "pkg:npm/Log4t@3.1.4", Properties = new List<Property>() });
ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.5",Purl= "pkg:npm/Log4t@3.1.5", Properties = new List<Property>() });

int noOfExcludedComponents = 0;

List<string> list = new List<string>();
list.Add("Debian:*");
list.Add("Newton:3.1.3");
list.Add("pkg:npm/foobar@12.3.1");
list.Add("pkg:npm/Log4t@3.1.5");

//Act
CommonHelper.RemoveExcludedComponents(ComponentsForBom, list, ref noOfExcludedComponents);

//Assert
Assert.That(noOfExcludedComponents, Is.EqualTo(5), "Returns the count of excluded components");
Assert.That(noOfExcludedComponents, Is.EqualTo(5), "Returns the count of excluded components");

}

Expand Down Expand Up @@ -268,9 +268,9 @@ public void RemoveExcludedComponents_WhenExcludedComponentMatches_ReturnsExclude
// Arrange
List<Component> componentList = new List<Component>
{
new Component { Name = "Component1", Version = "1.0" },
new Component { Name = "Component2", Version = "2.0" },
new Component { Name = "Component3", Version = "3.0" }
new Component { Name = "Component1", Version = "1.0", Properties = new List<Property>() },
new Component { Name = "Component2", Version = "2.0", Properties = new List<Property>() },
new Component { Name = "Component3", Version = "3.0", Properties = new List<Property>() }
};
List<string> excludedComponents = new List<string> { "Component1:*", "Component2:2.0" };
int noOfExcludedComponents = 0;
Expand All @@ -279,9 +279,7 @@ public void RemoveExcludedComponents_WhenExcludedComponentMatches_ReturnsExclude
List<Component> result = CommonHelper.RemoveExcludedComponents(componentList, excludedComponents, ref noOfExcludedComponents);

// Assert
Assert.AreEqual(1, result.Count);
Assert.IsFalse(result.Any(c => c.Name == "Component1" && c.Version == "1.0"));
Assert.IsTrue(result.Any(c => c.Name == "Component3" && c.Version == "3.0"));
Assert.AreEqual(3, result.Count);
Assert.AreEqual(2, noOfExcludedComponents);
}

Expand Down
35 changes: 21 additions & 14 deletions src/LCT.Common/CommonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ public static List<Component> RemoveExcludedComponents(List<Component> Component
List<string> ExcludedComponentsFromPurl = ExcludedComponents?.Where(ec => ec.StartsWith("pkg:")).ToList();
List<string> otherExcludedComponents = ExcludedComponents?.Where(ec => !ec.StartsWith("pkg:")).ToList();

ExcludedList.AddRange(RemoveExcludedComponentsFromPurl(ComponentList, ExcludedComponentsFromPurl, ref noOfExcludedComponents));
ExcludedList.AddRange(RemoveOtherExcludedComponents(ComponentList, otherExcludedComponents, ref noOfExcludedComponents));
ComponentList.RemoveAll(item => ExcludedList.Contains(item));
AddExcludedComponentsPropertyFromPurl(ComponentList, ExcludedComponentsFromPurl, ref noOfExcludedComponents);
AddExcludedComponentsPropertyFromNameAndVersion(ComponentList, otherExcludedComponents, ref noOfExcludedComponents);
return ComponentList;
}

Expand Down Expand Up @@ -297,29 +296,37 @@ private static string Sw360URL(string sw360Env, string releaseId)
return sw360URL;
}

private static List<Component> RemoveExcludedComponentsFromPurl(List<Component> ComponentList, List<string> ExcludedComponentsFromPurl, ref int noOfExcludedComponents)
private static List<Component> AddExcludedComponentsPropertyFromPurl(List<Component> ComponentList, List<string> ExcludedComponentsFromPurl, ref int noOfExcludedComponents)
{
List<Component> ExcludedList = new List<Component>();


foreach (string excludedComponent in ExcludedComponentsFromPurl)
{
Property excludeProperty = new() { Name = Dataconstant.Cdx_ExcludeComponent, Value = "true" };
foreach (var component in ComponentList)
{
if (component.Purl != null && component.Purl.Equals(excludedComponent, StringComparison.OrdinalIgnoreCase))
string componentPurl = NormalizePurl(component.Purl);
if (component.Purl != null && componentPurl.Equals(excludedComponent, StringComparison.OrdinalIgnoreCase))
{
component.Properties.Add(excludeProperty);
noOfExcludedComponents++;
ExcludedList.Add(component);
}
}
}

return ExcludedList;
return ComponentList;
}

private static List<Component> RemoveOtherExcludedComponents(List<Component> ComponentList, List<string> otherExcludedComponents, ref int noOfExcludedComponents)
private static string NormalizePurl(string purl)
{
if (purl.Contains("%40"))
{
return purl.Replace("%40", "@");
}
return purl;
}
private static List<Component> AddExcludedComponentsPropertyFromNameAndVersion(List<Component> ComponentList, List<string> otherExcludedComponents, ref int noOfExcludedComponents)
{
List<Component> ExcludedList = new List<Component>();

Property excludeProperty = new() { Name = Dataconstant.Cdx_ExcludeComponent, Value = "true" };
foreach (string excludedComponent in otherExcludedComponents)
{
string[] excludedcomponent = excludedComponent.ToLower().Split(':');
Expand All @@ -334,12 +341,12 @@ private static List<Component> RemoveOtherExcludedComponents(List<Component> Com
(component.Version.ToLowerInvariant().Contains(excludedcomponent[1].ToLowerInvariant()) || excludedcomponent[1].ToLowerInvariant() == "*"))
{
noOfExcludedComponents++;
ExcludedList.Add(component);
component.Properties.Add(excludeProperty);
}
}
}

return ExcludedList;
return ComponentList;
}
#endregion
}
Expand Down
1 change: 1 addition & 0 deletions src/LCT.Common/Constants/Dataconstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static class Dataconstant
public const string Cdx_JfrogRepoPath = "internal:siemens:clearing:jfrog-repo-path";
public const string Cdx_Siemensfilename = "internal:siemens:clearing:siemens:filename";
public const string Cdx_SiemensDirect = "internal:siemens:clearing:siemens:direct";
public const string Cdx_ExcludeComponent = "internal:siemens:clearing:sw360:exclude";

public static Dictionary<string, string> PurlCheck()
{
Expand Down
4 changes: 3 additions & 1 deletion src/LCT.Common/Model/Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class Components

[JsonIgnore]
public string IsDev { get; set; }

[JsonIgnore]
public string ExcludeComponent { get; set; }

}
}
2 changes: 1 addition & 1 deletion src/LCT.PackageIdentifier.UTest/ConanParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void ParseLockFile_GivenAInputFilePath_ReturnDevDependentComp()
public void ParseLockFile_GivenAInputFilePathExcludeComponent_ReturnComponentCount()
{
//Arrange
int totalComponentsAfterExclusion = 15;
int totalComponentsAfterExclusion = 17;
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string outFolder = Path.GetDirectoryName(exePath);
string packagefilepath = outFolder + @"\PackageIdentifierUTTestFiles";
Expand Down
2 changes: 1 addition & 1 deletion src/LCT.PackageIdentifier/Model/BomKpiData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class BomKpiData
public int UnofficialComponents { get; set; }


[DisplayName(@"Total Components Excluded")]
[DisplayName(@"Total Components Excluded SW360")]
public int ComponentsExcluded { get; set; }

[DisplayName(@"Components With SourceURL")]
Expand Down
9 changes: 6 additions & 3 deletions src/LCT.SW360PackageCreator/ComponentCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private async Task<List<Components>> GetListOfBomData(List<Component> components
{
Logger.Debug($"{item.Name}-{item.Version} found as internal component. ");
}
else if (componentsData.IsDev == "true" && appSettings.SW360.IgnoreDevDependency)
else if ((componentsData.IsDev == "true" && appSettings.SW360.IgnoreDevDependency)||componentsData.ExcludeComponent == "true")
{
//do nothing
}
Expand Down Expand Up @@ -148,14 +148,13 @@ private void UpdateToLocalBomFile(Components componentsData, string currName, st
private static bool GetPackageType(Component package, ref Components componentsData)
{
bool isInternalComponent = false;

foreach (var property in package.Properties)
{
if (property.Name?.ToLower() == Dataconstant.Cdx_ProjectType.ToLower())
{
componentsData.ProjectType = property.Value;
}

if (property.Name?.ToLower() == Dataconstant.Cdx_IsInternal.ToLower())
{
_ = bool.TryParse(property.Value, out isInternalComponent);
Expand All @@ -164,6 +163,10 @@ private static bool GetPackageType(Component package, ref Components componentsD
{
componentsData.IsDev = property.Value;
}
if (property.Name?.ToLower() == Dataconstant.Cdx_ExcludeComponent.ToLower())
{
componentsData.ExcludeComponent = property.Value;
}
}

return isInternalComponent;
Expand Down

0 comments on commit 605b526

Please sign in to comment.