Skip to content

Commit 69e2f05

Browse files
author
Sridhar, NIVETHA (ext) (FT D AA IN SGI EA EBT BE)
committed
Code clean up in package identifier and package creator
1 parent 26aa7ec commit 69e2f05

File tree

84 files changed

+4427
-122
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+4427
-122
lines changed

src/LCT.Facade/SW360ApicommunicationFacade.cs

Lines changed: 173 additions & 3 deletions
Large diffs are not rendered by default.

src/LCT.PackageIdentifier/AlpineProcesser.cs

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,27 @@ namespace LCT.PackageIdentifier
2626
/// </summary>
2727
public class AlpineProcessor(ICycloneDXBomParser cycloneDXBomParser, ISpdxBomParser spdxBomParser) : IParser
2828
{
29+
#region Fields
2930
static readonly ILog Logger = LoggerFactory.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
3031
private readonly ICycloneDXBomParser _cycloneDXBomParser = cycloneDXBomParser;
3132
private readonly ISpdxBomParser _spdxBomParser = spdxBomParser;
3233
private static Bom ListUnsupportedComponentsForBom = new Bom { Components = new List<Component>(), Dependencies = new List<Dependency>() };
34+
#endregion
35+
36+
#region Properties
37+
#endregion
3338

34-
#region public method
39+
#region Constructors
40+
// Primary constructor parameters are declared on the class.
41+
#endregion
3542

43+
#region Methods
44+
/// <summary>
45+
/// Parses Alpine package files from the configured input folder and builds a BOM.
46+
/// </summary>
47+
/// <param name="appSettings">Application settings with input and processing options.</param>
48+
/// <param name="unSupportedBomList">Reference BOM that will be populated with unsupported components.</param>
49+
/// <returns>BOM built from discovered Alpine packages.</returns>
3650
public Bom ParsePackageFile(CommonAppSettings appSettings, ref Bom unSupportedBomList)
3751
{
3852
List<string> configFiles;
@@ -77,12 +91,26 @@ public Bom ParsePackageFile(CommonAppSettings appSettings, ref Bom unSupportedBo
7791
return bom;
7892
}
7993

94+
/// <summary>
95+
/// Removes components excluded in settings from the provided BOM.
96+
/// </summary>
97+
/// <param name="appSettings">Application settings.</param>
98+
/// <param name="cycloneDXBOM">BOM to filter.</param>
99+
/// <returns>Filtered BOM.</returns>
80100
public static Bom RemoveExcludedComponents(CommonAppSettings appSettings, Bom cycloneDXBOM)
81101
{
82102
return CommonHelper.RemoveExcludedComponentsFromBom(appSettings, cycloneDXBOM,
83103
noOfExcludedComponents => BomCreator.bomKpiData.ComponentsExcludedSW360 += noOfExcludedComponents);
84104
}
85105

106+
/// <summary>
107+
/// Asynchronously retrieves repository details for the provided components and returns a modified list.
108+
/// </summary>
109+
/// <param name="componentsForBOM">List of components to enrich.</param>
110+
/// <param name="appSettings">Application settings.</param>
111+
/// <param name="jFrogService">JFrog service to query (may be null).</param>
112+
/// <param name="bomhelper">BOM helper utilities.</param>
113+
/// <returns>Asynchronously returns the modified list of components.</returns>
86114
public async Task<List<Component>> GetJfrogRepoDetailsOfAComponent(List<Component> componentsForBOM, CommonAppSettings appSettings,
87115
IJFrogService jFrogService,
88116
IBomHelper bomhelper)
@@ -97,24 +125,42 @@ public async Task<List<Component>> GetJfrogRepoDetailsOfAComponent(List<Componen
97125
return modifiedBOM;
98126
}
99127

128+
/// <summary>
129+
/// Asynchronously identifies internal components using JFrog and BOM helper; returns input unchanged in current implementation.
130+
/// </summary>
131+
/// <param name="componentData">Component identification data to analyze.</param>
132+
/// <param name="appSettings">Application settings.</param>
133+
/// <param name="jFrogService">JFrog service to query.</param>
134+
/// <param name="bomhelper">BOM helper utilities.</param>
135+
/// <returns>Asynchronously returns the (possibly modified) component identification data.</returns>
100136
public async Task<ComponentIdentification> IdentificationOfInternalComponents(ComponentIdentification componentData,
101137
CommonAppSettings appSettings, IJFrogService jFrogService, IBomHelper bomhelper)
102138
{
103139
await Task.Yield();
104140
return componentData;
105141
}
106142

107-
#endregion
108-
109-
#region private methods
110-
143+
/// <summary>
144+
/// Parses a CycloneDX SBOM file and returns a list of AlpinePackage entries.
145+
/// </summary>
146+
/// <param name="filePath">Path to the CycloneDX/ SPDX file.</param>
147+
/// <param name="dependenciesForBOM">List to collect BOM dependencies.</param>
148+
/// <param name="appSettings">Application settings.</param>
149+
/// <returns>List of parsed AlpinePackage instances.</returns>
111150
public List<AlpinePackage> ParseCycloneDX(string filePath, List<Dependency> dependenciesForBOM, CommonAppSettings appSettings)
112151
{
113152
List<AlpinePackage> alpinePackages = new List<AlpinePackage>();
114153
ExtractDetailsForJson(filePath, ref alpinePackages, dependenciesForBOM, appSettings);
115154
return alpinePackages;
116155
}
117156

157+
/// <summary>
158+
/// Extracts package details from a BOM file and populates provided collections with packages and dependencies.
159+
/// </summary>
160+
/// <param name="filePath">Path to the BOM file.</param>
161+
/// <param name="alpinePackages">Reference list to append discovered Alpine packages.</param>
162+
/// <param name="dependenciesForBOM">List to collect dependencies discovered in the BOM.</param>
163+
/// <param name="appSettings">Application settings.</param>
118164
private void ExtractDetailsForJson(string filePath, ref List<AlpinePackage> alpinePackages, List<Dependency> dependenciesForBOM, CommonAppSettings appSettings)
119165
{
120166
Bom listUnsupportedComponents = new Bom { Components = new List<Component>(), Dependencies = new List<Dependency>() };
@@ -151,6 +197,10 @@ private void ExtractDetailsForJson(string filePath, ref List<AlpinePackage> alpi
151197
ListUnsupportedComponentsForBom.Dependencies.AddRange(listUnsupportedComponents.Dependencies);
152198
}
153199

200+
/// <summary>
201+
/// Removes duplicate components from the provided list and updates KPI data.
202+
/// </summary>
203+
/// <param name="listofComponents">Reference to the component list to deduplicate.</param>
154204
private static void GetDistinctComponentList(ref List<AlpinePackage> listofComponents)
155205
{
156206
int initialCount = listofComponents.Count;
@@ -160,6 +210,12 @@ private static void GetDistinctComponentList(ref List<AlpinePackage> listofCompo
160210
BomCreator.bomKpiData.DuplicateComponents = initialCount - listofComponents.Count;
161211
}
162212

213+
/// <summary>
214+
/// Builds a release external id (purl) for the specified name and version.
215+
/// </summary>
216+
/// <param name="name">Package name.</param>
217+
/// <param name="version">Package version.</param>
218+
/// <returns>Release external id as a PURL string.</returns>
163219
private static string GetReleaseExternalId(string name, string version)
164220
{
165221
version = WebUtility.UrlEncode(version);
@@ -168,6 +224,11 @@ private static string GetReleaseExternalId(string name, string version)
168224
return $"{Dataconstant.PurlCheck()["ALPINE"]}{Dataconstant.ForwardSlash}{name}@{version}?arch=source";
169225
}
170226

227+
/// <summary>
228+
/// Extracts the distro query portion from an Alpine package PURL if present.
229+
/// </summary>
230+
/// <param name="alpinePackage">Alpine package to inspect.</param>
231+
/// <returns>Distro query string including leading token, or empty string if not found.</returns>
171232
private static string GetDistro(AlpinePackage alpinePackage)
172233
{
173234
var distroIndex = alpinePackage.PurlID.LastIndexOf("distro");
@@ -178,6 +239,11 @@ private static string GetDistro(AlpinePackage alpinePackage)
178239
return alpinePackage.PurlID[distroIndex..];
179240
}
180241

242+
/// <summary>
243+
/// Forms a list of CycloneDX Component objects from parsed Alpine packages, adding PURLs and properties.
244+
/// </summary>
245+
/// <param name="listOfComponents">List of parsed Alpine packages.</param>
246+
/// <returns>List of components ready for BOM insertion.</returns>
181247
private static List<Component> FormComponentReleaseExternalID(List<AlpinePackage> listOfComponents)
182248
{
183249
List<Component> listComponentForBOM = new List<Component>();
@@ -198,6 +264,12 @@ private static List<Component> FormComponentReleaseExternalID(List<AlpinePackage
198264
}
199265
return listComponentForBOM;
200266
}
267+
268+
/// <summary>
269+
/// Adds properties to a CycloneDX component based on SPDX information or discovery metadata.
270+
/// </summary>
271+
/// <param name="prop">Parsed Alpine package information.</param>
272+
/// <param name="component">Component to enrich.</param>
201273
private static void AddComponentProperties(AlpinePackage prop, Component component)
202274
{
203275
if (prop.SpdxComponentDetails.SpdxComponent)
@@ -213,6 +285,13 @@ private static void AddComponentProperties(AlpinePackage prop, Component compone
213285
component.Properties.Add(identifierType);
214286
}
215287
}
288+
289+
/// <summary>
290+
/// Sets SPDX related details on an AlpinePackage when the source file is an SPDX file.
291+
/// </summary>
292+
/// <param name="filePath">Source file path.</param>
293+
/// <param name="package">Alpine package to update.</param>
294+
/// <param name="componentInfo">Component information parsed from the BOM.</param>
216295
private static void SetSpdxComponentDetails(string filePath, AlpinePackage package, Component componentInfo)
217296
{
218297
if (filePath.EndsWith(FileConstant.SPDXFileExtension))
@@ -224,5 +303,8 @@ private static void SetSpdxComponentDetails(string filePath, AlpinePackage packa
224303
}
225304

226305
#endregion
306+
307+
#region Events
308+
#endregion
227309
}
228310
}

src/LCT.PackageIdentifier/BomCreator.cs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,46 @@ namespace LCT.PackageIdentifier
3737
/// </summary>
3838
public class BomCreator : IBomCreator
3939
{
40+
#region Fields
4041
static readonly ILog Logger = LoggerFactory.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
4142
public readonly static BomKpiData bomKpiData = new();
4243
ComponentIdentification componentData;
4344
private readonly ICycloneDXBomParser CycloneDXBomParser;
4445
private readonly ISpdxBomParser SpdxBomParser;
45-
public IJFrogService JFrogService { get; set; }
46-
public IBomHelper BomHelper { get; set; }
47-
4846
private readonly IFrameworkPackages _frameworkPackages;
4947
private readonly ICompositionBuilder _compositionBuilder;
5048
private readonly IRuntimeIdentifier _runtimeIdentifier;
5149

5250
public static Jfrog jfrog { get; set; } = new Jfrog();
5351
public static SW360 sw360 { get; set; } = new SW360();
52+
#endregion
53+
54+
#region Properties
55+
public IJFrogService JFrogService { get; set; }
56+
public IBomHelper BomHelper { get; set; }
57+
#endregion
58+
59+
#region Constructors
5460
public BomCreator(ICycloneDXBomParser cycloneDXBomParser, IFrameworkPackages frameworkPackages, ICompositionBuilder compositionBuilder, ISpdxBomParser spdxBomParser, IRuntimeIdentifier runtimeIdentifier)
5561
{
5662
CycloneDXBomParser = cycloneDXBomParser;
57-
_frameworkPackages = frameworkPackages;
63+
_frameworkPackages = frameworkPackages;
5864
_compositionBuilder = compositionBuilder;
5965
SpdxBomParser = spdxBomParser;
6066
_runtimeIdentifier = runtimeIdentifier;
6167
}
68+
#endregion
69+
6270

71+
/// <summary>
72+
/// Asynchronously generates a CycloneDX BOM from configured inputs and writes outputs (BOM, KPI files).
73+
/// </summary>
74+
/// <param name="appSettings">Application settings used for generation.</param>
75+
/// <param name="bomHelper">BOM helper utilities.</param>
76+
/// <param name="fileOperations">File operations helper used to write outputs.</param>
77+
/// <param name="projectReleases">Project releases data used to enrich metadata.</param>
78+
/// <param name="caToolInformation">CA tool information for telemetry/metadata.</param>
79+
/// <returns>Asynchronously completes when generation finishes.</returns>
6380
public async Task GenerateBom(CommonAppSettings appSettings,
6481
IBomHelper bomHelper,
6582
IFileOperations fileOperations,
@@ -124,11 +141,25 @@ public async Task GenerateBom(CommonAppSettings appSettings,
124141
Logger.Debug($"GenerateBom():End");
125142
}
126143

144+
/// <summary>
145+
/// Writes contents to BOM by delegating to CycloneDX writer.
146+
/// </summary>
147+
/// <param name="appSettings">Application settings.</param>
148+
/// <param name="bomKpiData">KPI data structure to update.</param>
149+
/// <param name="listOfComponentsToBom">BOM to write.</param>
150+
/// <param name="defaultProjectName">Default project name used in file naming.</param>
127151
private static void WritecontentsToBOM(CommonAppSettings appSettings, BomKpiData bomKpiData, Bom listOfComponentsToBom, string defaultProjectName)
128152
{
129153
WriteContentToCycloneDxBOM(appSettings, listOfComponentsToBom, ref bomKpiData, defaultProjectName);
130154
}
131155

156+
/// <summary>
157+
/// Writes a CycloneDX BOM file to the output folder, optionally merging with existing BOMs when configured.
158+
/// </summary>
159+
/// <param name="appSettings">Application settings.</param>
160+
/// <param name="listOfComponentsToBom">BOM object containing components and metadata.</param>
161+
/// <param name="bomKpiData">KPI data reference that can be modified.</param>
162+
/// <param name="defaultProjectName">Default project name used for file naming.</param>
132163
private static void WriteContentToCycloneDxBOM(CommonAppSettings appSettings, Bom listOfComponentsToBom, ref BomKpiData bomKpiData, string defaultProjectName)
133164
{
134165
FileOperations fileOperations = new FileOperations();
@@ -154,6 +185,11 @@ private static void WriteContentToCycloneDxBOM(CommonAppSettings appSettings, Bo
154185

155186
}
156187

188+
/// <summary>
189+
/// Asynchronously selects and invokes the appropriate package parser based on project type.
190+
/// </summary>
191+
/// <param name="appSettings">Application settings which include ProjectType.</param>
192+
/// <returns>Asynchronously returns the generated BOM from the selected parser.</returns>
157193
private async Task<Bom> CallPackageParser(CommonAppSettings appSettings)
158194
{
159195
IParser parser;
@@ -194,6 +230,12 @@ private async Task<Bom> CallPackageParser(CommonAppSettings appSettings)
194230
return new Bom();
195231
}
196232

233+
/// <summary>
234+
/// Asynchronously runs component identification and enrichment using the provided parser.
235+
/// </summary>
236+
/// <param name="appSettings">Application settings used by the parser.</param>
237+
/// <param name="parser">Parser that will parse package files and identify components.</param>
238+
/// <returns>Asynchronously returns the composed BOM.</returns>
197239
private async Task<Bom> ComponentIdentification(CommonAppSettings appSettings, IParser parser)
198240
{
199241
ComponentIdentification lstOfComponents;
@@ -246,6 +288,12 @@ private async Task<Bom> ComponentIdentification(CommonAppSettings appSettings, I
246288
bom.Dependencies.AddRange(unSupportedBomList.Dependencies);
247289
return bom;
248290
}
291+
292+
/// <summary>
293+
/// Asynchronously checks connectivity to JFrog using the configured JFrog service.
294+
/// </summary>
295+
/// <param name="appSettings">Application settings containing JFrog configuration.</param>
296+
/// <returns>Asynchronously returns true when connection is successful or JFrog is not configured; otherwise false.</returns>
249297
public async Task<bool> CheckJFrogConnection(CommonAppSettings appSettings)
250298
{
251299
if (appSettings.Jfrog != null)
@@ -276,5 +324,9 @@ public async Task<bool> CheckJFrogConnection(CommonAppSettings appSettings)
276324
return true;
277325

278326
}
327+
328+
329+
#region Events
330+
#endregion
279331
}
280332
}

0 commit comments

Comments
 (0)