Skip to content

Commit 09ade65

Browse files
Updated changes for distro and clone repository
1 parent 2aa007d commit 09ade65

File tree

10 files changed

+90
-24
lines changed

10 files changed

+90
-24
lines changed

src/LCT.PackageIdentifier/AlpineProcesser.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,27 @@ private static string GetReleaseExternalId(string name, string version)
168168
return $"{Dataconstant.PurlCheck()["ALPINE"]}{Dataconstant.ForwardSlash}{name}@{version}?arch=source";
169169
}
170170

171+
private static string GetDistro(AlpinePackage alpinePackage)
172+
{
173+
var distro = alpinePackage.PurlID;
174+
distro = distro.Substring(distro.LastIndexOf("distro"));
175+
return distro;
176+
}
177+
171178
private static List<Component> FormComponentReleaseExternalID(List<AlpinePackage> listOfComponents)
172179
{
173180
List<Component> listComponentForBOM = new List<Component>();
174181

175182
foreach (var prop in listOfComponents)
176183
{
184+
var distro = GetDistro(prop);
177185
Component component = new Component
178186
{
179187
Name = prop.Name,
180188
Version = prop.Version,
181189
Purl = GetReleaseExternalId(prop.Name, prop.Version)
182190
};
183-
component.BomRef = component.Purl;
191+
component.BomRef = $"{Dataconstant.PurlCheck()["ALPINE"]}{Dataconstant.ForwardSlash}{prop.Name}@{prop.Version}?{distro}";
184192
Property identifierType = new() { Name = Dataconstant.Cdx_IdentifierType, Value = Dataconstant.Discovered };
185193
component.Properties = new List<Property> { identifierType };
186194
listComponentForBOM.Add(component);

src/LCT.PackageIdentifier/Program.cs

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using LCT.APICommunications.Interfaces;
2525
using LCT.APICommunications;
2626
using LCT.APICommunications.Model;
27+
using System.Drawing;
2728

2829
namespace LCT.PackageIdentifier
2930
{
@@ -53,6 +54,10 @@ static async Task Main(string[] args)
5354
Logger.Logger.Log(null, Level.Notice, $"\n====================<<<<< Package Identifier >>>>>====================", null);
5455
Logger.Logger.Log(null, Level.Notice, $"\nStart of Package Identifier execution: {DateTime.Now}", null);
5556

57+
if (appSettings.ProjectType.ToUpperInvariant() == "ALPINE")
58+
{
59+
Logger.Warn($"\nPlease note that the Alpine feature is currently in preview state. This means it's available for testing and evaluation purposes. While functional, it may not yet include all planned features and could encounter occasional issues. Your feedback during this preview phase is appreciated as we work towards its official release. Thank you for exploring Alpine with us.");
60+
}
5661

5762
if (appSettings.IsTestMode)
5863
Logger.Logger.Log(null, Level.Alert, $"Package Identifier is running in TEST mode \n", null);

src/LCT.SW360PackageCreator.UTest/AlpinePackageDownloaderTest.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ public async Task DownloadSourceForAlpine_ProvidedSourceURL_ReturnsDownloadPath(
2020
{
2121
Name = "apk-tools",
2222
Version = "2.14.0-r2",
23-
SourceUrl = "https://gitlab.alpinelinux.org/alpine/apk-tools/-/archive/v2.14.0/apk-tools-v2.14.0.tar.gz",
23+
SourceUrl = "https://gitlab.alpinelinux.org/alpine/apk-tools/-/archive/v2.12.9/apk-tools-v2.12.9.tar.gz",
2424
AlpineSource= "https://gitlab.alpinelinux.org/alpine/apk-tools/-/archive/v$pkgver/apk-tools-v$pkgver.tar.gz\\n\\tfix-recursive-solve-1.patch\\n\\tfix-recursive-solve-2.patch\\n\\t_apk\\n\\t"
2525

2626
};
27-
var localPathforDownload = $"{Directory.GetParent(Directory.GetCurrentDirectory())}/ClearingTool/DownloadedFiles/";
27+
var localPathforDownload = $"{Directory.GetParent(Directory.GetCurrentDirectory())}\\ClearingTool\\DownloadedFiles\\";
2828
var alpinePackageDownloader = new AlpinePackageDownloader();
2929

3030
//Act
@@ -40,13 +40,13 @@ public async Task DownloadSourceForAlpine_ProvidedInValidSourceURL_ReturnsNull()
4040
//Sending Incorrect SourceURL
4141
var lstComparisonBomData = new ComparisonBomData()
4242
{
43-
Name = "abi-compliance-checker",
44-
Version = "2.14.0-r2",
45-
SourceUrl = "https://github.com/lvc/abi-compliance-checker/archive/$pkgver.tar.gz",
46-
AlpineSource=""
43+
Name = "ca-certificates",
44+
Version = "20220614-r0",
45+
SourceUrl = "https://gitlab.alpinelinux.org/alpine/ca-certificates/-/archive/ca-certificates-20230506.tar.bz2",
46+
AlpineSource =""
4747

4848
};
49-
var localPathforDownload = $"{Directory.GetParent(Directory.GetCurrentDirectory())}/ClearingTool/DownloadedFiles/";
49+
var localPathforDownload = $"{Directory.GetParent(Directory.GetCurrentDirectory())}\\ClearingTool\\DownloadedFiles\\";
5050
var alpinePackageDownloader = new AlpinePackageDownloader();
5151

5252
//Act

src/LCT.SW360PackageCreator.UTest/UrlHelperTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ public async Task GetSourceUrlForPythonPackage_ProvidedInvalidPackageDetails_Ret
131131
Assert.That(string.IsNullOrEmpty(sourceUrl));
132132
}
133133

134-
[TestCase("apk-tools", "2.14.0-r2")]
135-
public async Task GetSourceUrlForAlpinePackage_ProvidedPackageDetails_ReturnsValidSourceURL(string componentName, string version)
134+
[TestCase("apk-tools", "2.12.9-r3", "pkg:apk/alpine/[email protected]?distro=alpine-3.16.2")]
135+
public async Task GetSourceUrlForAlpinePackage_ProvidedPackageDetails_ReturnsValidSourceURL(string componentName, string version,string bomref)
136136
{
137137
// Arrange
138138
IUrlHelper urlHelper = new UrlHelper();
139139

140140
// Act
141-
var sourceUrlDetails = await urlHelper.GetSourceUrlForAlpinePackage(componentName, version);
141+
var sourceUrlDetails = await urlHelper.GetSourceUrlForAlpinePackage(componentName, version,bomref);
142142

143143
// Assert
144144
Assert.That(sourceUrlDetails.SourceUrl.Contains("apk-tools"));

src/LCT.SW360PackageCreator/AlpinePackageDownloader.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private static string ApplyPatchFilesToSourceCode(string downloadPath, string so
124124
}
125125
};
126126
process.Start();
127+
process.WaitForExit();
127128
}
128129

129130
foreach (var fileNames in buildFilesList)
@@ -220,8 +221,8 @@ public static string PackageFolderGzip(string localPathforDownload, ComparisonBo
220221
if (Directory.GetDirectories(localPathforDownload).Length != 0)
221222
{
222223

223-
var tempFolder = Directory.CreateDirectory($"{Directory.GetParent(Directory.GetCurrentDirectory())}/ClearingTool/DownloadedFiles/SourceCodeZipped/{component.Name}/--{DateTime.Now.ToString("yyyyMMddHHmmss")}{Dataconstant.ForwardSlash}");
224-
tarArchivePath = tempFolder + component.Name + ".tar.gz";
224+
var tempFolder = Directory.CreateDirectory($"{Directory.GetParent(Directory.GetCurrentDirectory())}\\ClearingTool\\DownloadedFiles\\SourceCodeZipped\\{component.Name}\\--{DateTime.Now.ToString("yyyyMMddHHmmss")}\\");
225+
tarArchivePath = tempFolder + (component.Name + "_" + component.Version) +".tar.gz";
225226
var InputDirectory = localPathforDownload;
226227
var OutputFilename = tarArchivePath;
227228
using Stream zipStream = new FileStream(System.IO.Path.GetFullPath(OutputFilename), FileMode.Create, FileAccess.Write);

src/LCT.SW360PackageCreator/ComponentCreator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private async Task<List<Components>> GetListOfBomData(List<Component> components
8484
componentsData.ComponentExternalId = item.Purl.Substring(0, item.Purl.IndexOf('@'));
8585
componentsData.ReleaseExternalId = item.Purl;
8686

87-
Components component = await GetSourceUrl(componentsData.Name, componentsData.Version, componentsData.ProjectType);
87+
Components component = await GetSourceUrl(componentsData.Name, componentsData.Version, componentsData.ProjectType, item.BomRef);
8888
componentsData.SourceUrl = component.SourceUrl;
8989

9090
if (componentsData.ProjectType.ToUpperInvariant() == "ALPINE")
@@ -179,7 +179,7 @@ private static string GetPackageName(Component item)
179179
}
180180
}
181181

182-
private static async Task<Components> GetSourceUrl(string name, string version, string projectType)
182+
private static async Task<Components> GetSourceUrl(string name, string version, string projectType, string bomRef)
183183
{
184184
Components componentsData = new Components();
185185
switch (projectType.ToUpperInvariant())
@@ -202,7 +202,7 @@ private static async Task<Components> GetSourceUrl(string name, string version,
202202
componentsData.SourceUrl = await UrlHelper.Instance.GetSourceUrlForConanPackage(name, version);
203203
break;
204204
case "ALPINE":
205-
Components alpComponentData = await UrlHelper.Instance.GetSourceUrlForAlpinePackage(name, version);
205+
Components alpComponentData = await UrlHelper.Instance.GetSourceUrlForAlpinePackage(name, version,bomRef);
206206
componentsData = alpComponentData;
207207
componentsData.ProjectType = projectType;
208208
break;

src/LCT.SW360PackageCreator/Interfaces/IURLHelper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public interface IUrlHelper
6262
/// </summary>
6363
/// <param name="componentName"></param>
6464
/// <param name="componenVersion"></param>
65-
/// <param name="componentsData"></param>
65+
/// <param name="bomRef"></param>
6666
/// <returns>string</returns>
67-
Task<Components> GetSourceUrlForAlpinePackage(string componentName, string componenVersion);
67+
Task<Components> GetSourceUrlForAlpinePackage(string componentName, string componenVersion, string bomRef);
6868
}
6969
}

src/LCT.SW360PackageCreator/Program.cs

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using LCT.SW360PackageCreator.Interfaces;
1616
using log4net;
1717
using log4net.Core;
18+
using NuGet.Protocol.Plugins;
1819
using System;
1920
using System.Collections.Generic;
2021
using System.Diagnostics;
@@ -54,6 +55,10 @@ static async Task Main(string[] args)
5455

5556
Logger.Logger.Log(null, Level.Notice, $"\n====================<<<<< Package creator >>>>>====================", null);
5657
Logger.Logger.Log(null, Level.Notice, $"\nStart of Package creator execution : {DateTime.Now}", null);
58+
if (appSettings.ProjectType.ToUpperInvariant() == "ALPINE")
59+
{
60+
Logger.Warn($"\nPlease note that the Alpine feature is currently in preview state. This means it's available for testing and evaluation purposes. While functional, it may not yet include all planned features and could encounter occasional issues. Your feedback during this preview phase is appreciated as we work towards its official release. Thank you for exploring Alpine with us.");
61+
}
5762

5863
if (appSettings.IsTestMode)
5964
Logger.Logger.Log(null, Level.Alert, $"Package creator is running in TEST mode \n", null);

src/LCT.SW360PackageCreator/URLHelper.cs

+52-5
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,25 @@ public class UrlHelper : IUrlHelper, IDisposable
5959
/// </summary>
6060
/// <param name="componentName"></param>
6161
/// <param name="componenVersion"></param>
62+
/// <param name="bomRef"></param>
6263
/// <returns>Components</returns>
63-
public async Task<Components> GetSourceUrlForAlpinePackage(string componentName, string componenVersion)
64+
public async Task<Components> GetSourceUrlForAlpinePackage(string componentName, string componenVersion, string bomRef)
6465
{
6566
Components componentsData = new Components();
6667
try
6768
{
6869
string localPathforSourceRepo = GetDownloadPathForAlpineRepo();
6970
string fullPath = Path.Combine(localPathforSourceRepo, "aports");
71+
var alpineDistro = GetAlpineDistro(bomRef);
7072
if (!Directory.Exists(fullPath))
7173
{
7274
//Clone AlpineRepository
73-
CloneSource(localPathforSourceRepo);
75+
CloneSource(localPathforSourceRepo, alpineDistro, fullPath);
76+
}
77+
else
78+
{
79+
//Checkout stable branch
80+
CheckoutDistro(alpineDistro, fullPath);
7481
}
7582

7683
AlpinePackage alpinePackSourceDetails = await GetAlpineSourceUrl(componentName, componenVersion, localPathforSourceRepo);
@@ -88,6 +95,15 @@ public async Task<Components> GetSourceUrlForAlpinePackage(string componentName,
8895
}
8996
return componentsData;
9097
}
98+
public static string GetAlpineDistro(string bomRef)
99+
{
100+
101+
string[] getDistro = bomRef.Split("distro");
102+
string[] getDestroVersion = getDistro[1].Split("-");
103+
var output = getDestroVersion[1].Substring(0, getDestroVersion[1].Length - 2);
104+
var distro = output + "-stable";
105+
return distro;
106+
}
91107

92108
private static Task<AlpinePackage> GetAlpineSourceUrl(string name, string version, string localPathforSourceRepo)
93109
{
@@ -154,9 +170,11 @@ public static string GetSourceUrlForAlpine(string pkgFilePath, string sourceData
154170
try
155171
{
156172
var pkgVersionLine = File.ReadLines(pkgFilePath).FirstOrDefault(x => x.StartsWith("pkgver"));
173+
var pkgNameLine = File.ReadLines(pkgFilePath).FirstOrDefault(x => x.StartsWith("pkgname"));
157174
var _commitLine = File.ReadLines(pkgFilePath).FirstOrDefault(x => x.StartsWith("_commit"));
158175
var _tzcodeverLine = File.ReadLines(pkgFilePath).FirstOrDefault(x => x.StartsWith("_tzcodever"));
159176
string pkgVersion = string.Empty;
177+
string pkgName = string.Empty;
160178
string _commitValue = string.Empty;
161179
string _tzcodever = string.Empty;
162180

@@ -165,10 +183,19 @@ public static string GetSourceUrlForAlpine(string pkgFilePath, string sourceData
165183
string[] pkgVersionValue = Regex.Split(pkgVersionLine, @"\=");
166184
pkgVersion = pkgVersionValue[1];
167185
}
186+
if (pkgNameLine != null)
187+
{
188+
string[] pkgNameData = Regex.Split(pkgNameLine, @"\=");
189+
pkgName = pkgNameData[1];
190+
}
168191
if (_tzcodeverLine != null)
169192
{
170193
string[] _tzcodeverValue = Regex.Split(_tzcodeverLine, @"\=");
171194
_tzcodever = _tzcodeverValue[1];
195+
if (_tzcodeverLine.Contains("pkgver"))
196+
{
197+
_tzcodever = pkgVersion;
198+
}
172199
}
173200
if (_commitLine != null)
174201
{
@@ -179,7 +206,7 @@ public static string GetSourceUrlForAlpine(string pkgFilePath, string sourceData
179206
{
180207
Match url = Regex.Match(sourceData, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=$]*)?");
181208
string finalUrl = url.ToString();
182-
sourceUrl = finalUrl.Replace("$pkgver", pkgVersion).Replace("$_commit", _commitValue).Replace("$_tzcodever", _tzcodever);
209+
sourceUrl = finalUrl.Replace("$pkgver", pkgVersion).Replace("$pkgname", pkgName).Replace("$_commit", _commitValue).Replace("$_tzcodever", _tzcodever);
183210
if (pkgVersion == null && _commitValue == null && _tzcodever == null)
184211
{
185212
sourceUrl = string.Empty;
@@ -203,7 +230,7 @@ public static string GetDownloadPathForAlpineRepo()
203230
string localPathforSourceRepo = string.Empty;
204231
try
205232
{
206-
localPathforSourceRepo = $"{Directory.GetParent(Directory.GetCurrentDirectory())}/ClearingTool/DownloadedFiles/";
233+
localPathforSourceRepo = $"{Directory.GetParent(Directory.GetCurrentDirectory())}\\ClearingTool\\DownloadedFiles\\";
207234
}
208235
catch (IOException ex)
209236
{
@@ -227,7 +254,7 @@ private static string GetComponentExternalIdForAlpine(string name)
227254
return $"{Dataconstant.PurlCheck()["ALPINE"]}{Dataconstant.ForwardSlash}{name}?arch=source";
228255
}
229256

230-
private static void CloneSource(string localPathforSourceRepo)
257+
private static void CloneSource(string localPathforSourceRepo, string alpineDistro, string fullPath)
231258
{
232259
List<string> gitCommands = GetGitCloneCommands();
233260
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -263,7 +290,27 @@ private static void CloneSource(string localPathforSourceRepo)
263290
process.Start();
264291
process.WaitForExit();
265292
}
293+
if (Directory.Exists(fullPath))
294+
{
295+
CheckoutDistro(alpineDistro, fullPath);
296+
}
297+
298+
}
266299

300+
private static void CheckoutDistro(string alpineDistro, string fullPath)
301+
{
302+
var process = new Process
303+
{
304+
StartInfo = new ProcessStartInfo()
305+
{
306+
CreateNoWindow = true,
307+
FileName = "git",
308+
Arguments = $"checkout" + " " + alpineDistro,
309+
WorkingDirectory = fullPath,
310+
}
311+
};
312+
process.Start();
313+
process.WaitForExit();
267314
}
268315

269316
private static List<string> GetGitCloneCommands()

src/SW360IntegrationTest/Alpine/ComponentCreatorInitialAlpine.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public async Task ReleaseCreation__AfterSuccessfulExeRun_ReturnsClearingStateAsN
140140
new AuthenticationHeaderValue(testParameters.SW360AuthTokenType, testParameters.SW360AuthTokenValue);
141141
string expectedname = "apk-tools";
142142
string expectedversion = "2.12.9-r3";
143-
string expecteddownloadurl = "https://gitlab.alpinelinux.org/alpine/apk-tools/-/archive/v2.14.0/apk-tools-v2.14.0.tar.gz";
143+
string expecteddownloadurl = "https://gitlab.alpinelinux.org/alpine/apk-tools/-/archive/v2.12.9/apk-tools-v2.12.9.tar.gz";
144144
string expectedexternalid = "pkg:apk/alpine/[email protected]?arch=source";
145145
//url formation for retrieving component details
146146
string url = TestConstant.Sw360ReleaseApi + TestConstant.componentNameUrl + "apk-tools";

0 commit comments

Comments
 (0)