diff --git a/src/LCT.Common.UTests/CommonHelperTest.cs b/src/LCT.Common.UTests/CommonHelperTest.cs index 8cd6ed21..669e2beb 100644 --- a/src/LCT.Common.UTests/CommonHelperTest.cs +++ b/src/LCT.Common.UTests/CommonHelperTest.cs @@ -54,25 +54,25 @@ public void RemoveMultipleExcludedComponents_ReturnSuccess() { //Arrange List ComponentsForBom = new List(); - 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()}); + ComponentsForBom.Add(new Component() { Name = "Debian", Version = "3.1.1", Purl = "pkg:npm/Debian@3.1.1", Properties = new List() }); + ComponentsForBom.Add(new Component() { Name = "Debian", Version = "3.1.2", Purl = "pkg:npm/Debian@3.1.2", Properties = new List() }); + ComponentsForBom.Add(new Component() { Name = "Newton", Version = "3.1.3", Purl = "pkg:npm/Newton@3.1.3", Properties = new List() }); + ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.4", Purl = "pkg:npm/Log4t@3.1.4", Properties = new List() }); + ComponentsForBom.Add(new Component() { Name = "Log4t", Version = "3.1.5",Purl= "pkg:npm/Log4t@3.1.5", Properties = new List() }); + int noOfExcludedComponents = 0; List list = new List(); 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"); } @@ -268,9 +268,9 @@ public void RemoveExcludedComponents_WhenExcludedComponentMatches_ReturnsExclude // Arrange List componentList = new List { - 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() }, + new Component { Name = "Component2", Version = "2.0", Properties = new List() }, + new Component { Name = "Component3", Version = "3.0", Properties = new List() } }; List excludedComponents = new List { "Component1:*", "Component2:2.0" }; int noOfExcludedComponents = 0; @@ -279,9 +279,7 @@ public void RemoveExcludedComponents_WhenExcludedComponentMatches_ReturnsExclude List 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); } diff --git a/src/LCT.Common/CommonHelper.cs b/src/LCT.Common/CommonHelper.cs index 9e234a5b..0abdbbf6 100644 --- a/src/LCT.Common/CommonHelper.cs +++ b/src/LCT.Common/CommonHelper.cs @@ -44,9 +44,8 @@ public static List RemoveExcludedComponents(List Component List ExcludedComponentsFromPurl = ExcludedComponents?.Where(ec => ec.StartsWith("pkg:")).ToList(); List 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; } @@ -297,29 +296,37 @@ private static string Sw360URL(string sw360Env, string releaseId) return sw360URL; } - private static List RemoveExcludedComponentsFromPurl(List ComponentList, List ExcludedComponentsFromPurl, ref int noOfExcludedComponents) + private static List AddExcludedComponentsPropertyFromPurl(List ComponentList, List ExcludedComponentsFromPurl, ref int noOfExcludedComponents) { - List ExcludedList = new List(); + 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 RemoveOtherExcludedComponents(List ComponentList, List otherExcludedComponents, ref int noOfExcludedComponents) + private static string NormalizePurl(string purl) + { + if (purl.Contains("%40")) + { + return purl.Replace("%40", "@"); + } + return purl; + } + private static List AddExcludedComponentsPropertyFromNameAndVersion(List ComponentList, List otherExcludedComponents, ref int noOfExcludedComponents) { - List ExcludedList = new List(); + Property excludeProperty = new() { Name = Dataconstant.Cdx_ExcludeComponent, Value = "true" }; foreach (string excludedComponent in otherExcludedComponents) { string[] excludedcomponent = excludedComponent.ToLower().Split(':'); @@ -334,12 +341,12 @@ private static List RemoveOtherExcludedComponents(List Com (component.Version.ToLowerInvariant().Contains(excludedcomponent[1].ToLowerInvariant()) || excludedcomponent[1].ToLowerInvariant() == "*")) { noOfExcludedComponents++; - ExcludedList.Add(component); + component.Properties.Add(excludeProperty); } } } - return ExcludedList; + return ComponentList; } #endregion } diff --git a/src/LCT.Common/Constants/Dataconstant.cs b/src/LCT.Common/Constants/Dataconstant.cs index 1b58d47b..da50c646 100644 --- a/src/LCT.Common/Constants/Dataconstant.cs +++ b/src/LCT.Common/Constants/Dataconstant.cs @@ -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 PurlCheck() { diff --git a/src/LCT.Common/Model/Components.cs b/src/LCT.Common/Model/Components.cs index 5a806e26..527defa0 100644 --- a/src/LCT.Common/Model/Components.cs +++ b/src/LCT.Common/Model/Components.cs @@ -61,6 +61,8 @@ public class Components [JsonIgnore] public string IsDev { get; set; } - + [JsonIgnore] + public string ExcludeComponent { get; set; } + } } diff --git a/src/LCT.PackageIdentifier.UTest/ConanParserTests.cs b/src/LCT.PackageIdentifier.UTest/ConanParserTests.cs index aeee702d..177012b1 100644 --- a/src/LCT.PackageIdentifier.UTest/ConanParserTests.cs +++ b/src/LCT.PackageIdentifier.UTest/ConanParserTests.cs @@ -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"; diff --git a/src/LCT.PackageIdentifier/Model/BomKpiData.cs b/src/LCT.PackageIdentifier/Model/BomKpiData.cs index 83c83245..1028388f 100644 --- a/src/LCT.PackageIdentifier/Model/BomKpiData.cs +++ b/src/LCT.PackageIdentifier/Model/BomKpiData.cs @@ -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")] diff --git a/src/LCT.SW360PackageCreator/ComponentCreator.cs b/src/LCT.SW360PackageCreator/ComponentCreator.cs index ee83d6a2..33451d25 100644 --- a/src/LCT.SW360PackageCreator/ComponentCreator.cs +++ b/src/LCT.SW360PackageCreator/ComponentCreator.cs @@ -73,7 +73,7 @@ private async Task> GetListOfBomData(List 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 } @@ -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); @@ -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;