Skip to content

Commit

Permalink
Updated code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ragavareddychalapala committed Jan 4, 2025
1 parent 5d05078 commit 8c60d62
Show file tree
Hide file tree
Showing 16 changed files with 230 additions and 119 deletions.
12 changes: 6 additions & 6 deletions src/AritfactoryUploader.UTest/ArtifactoryUploaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task UploadPackageToRepo_InputEmptyCreds_ReturnsPackgeNotFound()
JfrogApi = "https://abc.jfrog.io/artifactory",
SrcRepoName = "org1-pythonhosted-pypi-remote-cache",
SrcRepoPathWithFullName = "org1-pythonhosted-pypi-remote-cache/6c/dd/a834df6482147d48e225a49515aabc28974ad5a4ca3215c18a882565b028/html5lib-1.1-py2.py3-none-any.whl",
PypiCompName = "html5lib-1.1-py2.py3-none-any.whl",
PypiOrNpmCompName = "html5lib-1.1-py2.py3-none-any.whl",
DestRepoName = "pypi-test",
ApiKey = "",
Email = "",
Expand Down Expand Up @@ -111,7 +111,7 @@ public async Task UploadPackageToRepo_WhenPackageInfoIsNull_ReturnsNotFoundRespo
var timeout = 10000;
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), component))
.ReturnsAsync((AqlResult)null);
ArtfactoryUploader.jFrogService = jFrogServiceMock.Object;
// Act
Expand Down Expand Up @@ -140,7 +140,7 @@ public async Task UploadPackageToRepo_WhenPackageTypeIsClearedThirdPartyOrDevelo
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
var jfrogApicommunicationMock = new Mock<IJFrogApiCommunication>();
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),component))
.ReturnsAsync(new AqlResult());
jfrogApicommunicationMock.Setup(x => x.CopyFromRemoteRepo(It.IsAny<ComponentsToArtifactory>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));
Expand Down Expand Up @@ -171,7 +171,7 @@ public async Task UploadPackageToRepo_WhenPackageTypeIsInternal_CallsMoveFromRep
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
var jfrogApicommunicationMock = new Mock<IJFrogApiCommunication>();
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), component))
.ReturnsAsync(new AqlResult());
jfrogApicommunicationMock.Setup(x => x.MoveFromRepo(It.IsAny<ComponentsToArtifactory>()))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));
Expand Down Expand Up @@ -227,7 +227,7 @@ public async Task UploadPackageToRepo_WhenHttpRequestExceptionOccurs_ReturnsErro
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
var jfrogApicommunicationMock = new Mock<IJFrogApiCommunication>();
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),component))
.ThrowsAsync(new HttpRequestException());
ArtfactoryUploader.jFrogService = jFrogServiceMock.Object;
ArtfactoryUploader.JFrogApiCommInstance = jfrogApicommunicationMock.Object;
Expand Down Expand Up @@ -257,7 +257,7 @@ public async Task UploadPackageToRepo_WhenInvalidOperationExceptionOccurs_Return
var displayPackagesInfo = new DisplayPackagesInfo();
var jFrogServiceMock = new Mock<IJFrogService>();
var jfrogApicommunicationMock = new Mock<IJFrogApiCommunication>();
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
jFrogServiceMock.Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), component))
.ThrowsAsync(new InvalidOperationException());
ArtfactoryUploader.jFrogService = jFrogServiceMock.Object;
ArtfactoryUploader.JFrogApiCommInstance = jfrogApicommunicationMock.Object;
Expand Down
23 changes: 12 additions & 11 deletions src/AritfactoryUploader.UTest/PackageUploadHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,31 +406,31 @@ public void GetUploadPackageDetails_CoversAllScenarios()
public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenPypiRepoExists_ReturnsArtifactoryRepoName()
{
// Arrange
Property prop1 = new Property
Property repoNameProperty = new Property
{
Name = Dataconstant.Cdx_ArtifactoryRepoName,
Value = "Reponame"
};
List<Property> properties = new List<Property>() { prop1 };
List<Property> properties = new List<Property>() { repoNameProperty };
var item = new Component
{
Purl = "pypi://example-package",
Properties = properties,
Name = "pypi component",
Version = "1.0.0"
};
AqlProperty property1 = new AqlProperty
AqlProperty pypiNameProperty = new AqlProperty
{
key = "pypi.normalized.name",
value = "pypi component"
};

AqlProperty property2 = new AqlProperty
AqlProperty pypiVersionProperty = new AqlProperty
{
key = "pypi.version",
value = "1.0.0"
};
List<AqlProperty> propertys = new List<AqlProperty> { property1, property2 };
List<AqlProperty> propertys = new List<AqlProperty> { pypiNameProperty, pypiVersionProperty };
//GetInternalComponentDataByRepo
var aqlResultList = new List<AqlResult>
{
Expand All @@ -443,7 +443,9 @@ public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenPypiRepoExists_Ret
}
};
var jFrogServiceMock = new Mock<IJFrogService>();

jFrogServiceMock.Setup(x => x.GetPypiComponentDataByRepo(It.IsAny<string>())).ReturnsAsync(aqlResultList);

PackageUploadHelper.jFrogService = jFrogServiceMock.Object;

// Act
Expand All @@ -454,16 +456,15 @@ public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenPypiRepoExists_Ret
Assert.AreEqual("pypi-repo", result.Repo);
Assert.AreEqual("path/to/package", result.Path);
}

public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenConanRepoExists_ReturnsArtifactoryRepoName()
{
// Arrange
Property prop1 = new Property
Property reponameProperty = new Property
{
Name = Dataconstant.Cdx_ArtifactoryRepoName,
Value = "Reponame"
};
List<Property> properties = new List<Property>() { prop1 };
List<Property> properties = new List<Property>() { reponameProperty };
var item = new Component
{
Purl = "conan://example-package",
Expand Down Expand Up @@ -756,7 +757,7 @@ public async Task GetJfrogRepoInfoForAllTypePackages_GivenDestRepoNames_ReturnsA
}

[Test]
[TestCase("NPM", "source-repo/package-name/-/package-name-1.0.0.tgz?to=/destination-repo/package-name/-/package-name-1.0.0.tgz")]
[TestCase("NPM", "?to=/destination-repo//")]
[TestCase("NUGET", "source-repo/package-name.1.0.0.nupkg?to=/destination-repo/package-name.1.0.0.nupkg")]
[TestCase("MAVEN", "source-repo/package-name/1.0.0?to=/destination-repo/package-name/1.0.0")]
[TestCase("CONAN", "source-repo/?to=/destination-repo/")]
Expand Down Expand Up @@ -810,7 +811,7 @@ public void GetCopyURL_GivenInvalidComponentType_ReturnsEmptyString()


[Test]
[TestCase("NPM", "source-repo/package-name/-/package-name-1.0.0.tgz?to=/destination-repo/package-name/-/package-name-1.0.0.tgz")]
[TestCase("NPM", "?to=/destination-repo//")]
[TestCase("NUGET", "source-repo/package-name.1.0.0.nupkg?to=/destination-repo/package-name.1.0.0.nupkg")]
[TestCase("MAVEN", "source-repo/package-name/1.0.0?to=/destination-repo/package-name/1.0.0")]
[TestCase("CONAN", "source-repo/?to=/destination-repo/")]
Expand Down Expand Up @@ -860,6 +861,6 @@ public void GetMoveURL_GivenInvalidComponentType_ReturnsEmptyString()

// Assert
Assert.AreEqual(string.Empty, result);
}
}
}
}
15 changes: 10 additions & 5 deletions src/ArtifactoryUploader/ArtifactoryUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,14 @@ public static void SetConfigurationValues()

private static async Task<AqlResult> GetPackageInfoWithRetry(IJFrogService jFrogService, ComponentsToArtifactory component)
{
async Task<AqlResult> TryGetPackageInfo(string srcRepo, string packageName, string path)
=> await jFrogService.GetPackageInfo(srcRepo, packageName, path);
async Task<AqlResult> TryGetPackageInfo(string srcRepo, string packageName, string path, ComponentsToArtifactory component)
=> await jFrogService.GetPackageInfo(srcRepo, packageName, path, component);

var packageInfo = await TryGetPackageInfo(component.SrcRepoName, component.JfrogPackageName, component.Path);



var packageInfo = await TryGetPackageInfo(component.SrcRepoName, component.JfrogPackageName, component.Path, component);


// Handle DEBIAN package name mismatch
if (component.ComponentType == "DEBIAN" && packageInfo?.Name != component.JfrogPackageName)
Expand All @@ -124,8 +128,9 @@ async Task<AqlResult> TryGetPackageInfo(string srcRepo, string packageName, stri
var lowerSrcRepo = component.SrcRepoName.ToLower();
var lowerPackageName = component.JfrogPackageName.ToLower();
var lowerPath = component.Path.ToLower();


packageInfo = await TryGetPackageInfo(lowerSrcRepo, lowerPackageName, lowerPath);
packageInfo = await TryGetPackageInfo(lowerSrcRepo, lowerPackageName, lowerPath, component);

if (packageInfo != null)
{
Expand All @@ -137,7 +142,7 @@ async Task<AqlResult> TryGetPackageInfo(string srcRepo, string packageName, stri
// ToDo - A better way would need to be thought of in the future.
if (packageInfo == null)
{
packageInfo = await TryGetPackageInfo(component.SrcRepoName, component.JfrogPackageName, $"{component.Path}*");
packageInfo = await TryGetPackageInfo(component.SrcRepoName, component.JfrogPackageName, $"{component.Path}*", component);

if (packageInfo != null)
{
Expand Down
83 changes: 70 additions & 13 deletions src/ArtifactoryUploader/PackageUploadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ public async static Task<List<ComponentsToArtifactory>> GetComponentsToBeUploade
if (aqlResult != null)
{
components.SrcRepoPathWithFullName = aqlResult.Repo + "/" + aqlResult.Path + "/" + aqlResult.Name;
components.PypiCompName = aqlResult.Name;
components.PypiOrNpmCompName = aqlResult.Name;
}
else
{
components.SrcRepoPathWithFullName = string.Empty;
components.PypiCompName = string.Empty;
components.PypiOrNpmCompName = string.Empty;
}

components.Path = GetPackagePath(components, aqlResult);
Expand Down Expand Up @@ -730,8 +730,11 @@ public static string GetCopyURL(ComponentsToArtifactory component)
string url = string.Empty;
if (component.ComponentType == "NPM")
{
url = $"{component.JfrogApi}{ApiConstant.CopyPackageApi}{component.SrcRepoName}/{component.Name}/-/{component.PackageName}-{component.Version}" +
$"{ApiConstant.NpmExtension}?to=/{component.DestRepoName}/{component.Name}/-/{component.PackageName}-{component.Version}{ApiConstant.NpmExtension}";
url = $"{component.JfrogApi}{ApiConstant.CopyPackageApi}{component.SrcRepoPathWithFullName}" +
$"?to=/{component.DestRepoName}/{component.Path}/{component.PypiOrNpmCompName}";

// url = $"{component.JfrogApi}{ApiConstant.CopyPackageApi}{component.SrcRepoName}/{component.Name}/-/{component.PackageName}-{component.Version}" +
//$"{ApiConstant.NpmExtension}?to=/{component.DestRepoName}/{component.Name}/-/{component.PackageName}-{component.Version}{ApiConstant.NpmExtension}";
}
else if (component.ComponentType == "NUGET")
{
Expand All @@ -746,7 +749,7 @@ public static string GetCopyURL(ComponentsToArtifactory component)
else if (component.ComponentType == "PYTHON")
{
url = $"{component.JfrogApi}{ApiConstant.CopyPackageApi}{component.SrcRepoPathWithFullName}" +
$"?to=/{component.DestRepoName}/{component.PypiCompName}";
$"?to=/{component.DestRepoName}/{component.PypiOrNpmCompName}";
}
else if (component.ComponentType == "CONAN")
{
Expand All @@ -772,8 +775,11 @@ public static string GetMoveURL(ComponentsToArtifactory component)
string url = string.Empty;
if (component.ComponentType == "NPM")
{
url = $"{component.JfrogApi}{ApiConstant.MovePackageApi}{component.SrcRepoName}/{component.Name}/-/{component.PackageName}-{component.Version}" +
$"{ApiConstant.NpmExtension}?to=/{component.DestRepoName}/{component.Name}/-/{component.PackageName}-{component.Version}{ApiConstant.NpmExtension}";
url = $"{component.JfrogApi}{ApiConstant.MovePackageApi}{component.SrcRepoPathWithFullName}" +
$"?to=/{component.DestRepoName}/{component.Path}/{component.PypiOrNpmCompName}";

// url = $"{component.JfrogApi}{ApiConstant.MovePackageApi}{component.SrcRepoName}/{component.Name}/-/{component.PackageName}-{component.Version}" +
//$"{ApiConstant.NpmExtension}?to=/{component.DestRepoName}/{component.Name}/-/{component.PackageName}-{component.Version}{ApiConstant.NpmExtension}";
}
else if (component.ComponentType == "NUGET")
{
Expand All @@ -788,7 +794,7 @@ public static string GetMoveURL(ComponentsToArtifactory component)
else if (component.ComponentType == "PYTHON")
{
url = $"{component.JfrogApi}{ApiConstant.MovePackageApi}{component.SrcRepoPathWithFullName}" +
$"?to=/{component.DestRepoName}/{component.PypiCompName}";
$"?to=/{component.DestRepoName}/{component.PypiOrNpmCompName}";
}
else if (component.ComponentType == "CONAN")
{
Expand All @@ -814,7 +820,15 @@ private static string GetPackagePath(ComponentsToArtifactory component, AqlResul
switch (component.ComponentType)
{
case "NPM":
return $"{component.Name}/-";
if(aqlResult != null)
{
return $"{aqlResult.Path}";
}
else
{
return $"{component.Name}/-";
}


case "CONAN" when aqlResult != null:
string path = aqlResult.Path;
Expand Down Expand Up @@ -847,7 +861,7 @@ private static string GetJfrogPackageName(ComponentsToArtifactory component)
switch (component.ComponentType)
{
case "NPM":
packageName = $"{component.PackageName}-{component.Version}{ApiConstant.NpmExtension}";
packageName = component.PypiOrNpmCompName;
break;

case "NUGET":
Expand All @@ -859,7 +873,7 @@ private static string GetJfrogPackageName(ComponentsToArtifactory component)
break;

case "PYTHON":
packageName = component.PypiCompName;
packageName = component.PypiOrNpmCompName;
break;

default:
Expand Down Expand Up @@ -966,6 +980,15 @@ public async static Task<AqlResult> GetSrcRepoDetailsForPyPiOrConanPackages(Comp
return GetArtifactoryRepoNameForConan(aqlConanResultList, item);
}
}
else if (item.Purl.Contains("npm", StringComparison.OrdinalIgnoreCase))
{
var aqlNpmResultList = await GetNpmListOfComponentsFromRepo(new string[] { item.Properties.Find(x => x.Name == Dataconstant.Cdx_ArtifactoryRepoName)?.Value }, jFrogService);

if (aqlNpmResultList.Count > 0)
{
return GetNpmArtifactoryRepoName(aqlNpmResultList, item);
}
}

return null;
}
Expand Down Expand Up @@ -1180,13 +1203,36 @@ public static async Task<List<AqlResult>> GetPypiListOfComponentsFromRepo(string
return aqlResultList;
}

public static async Task<List<AqlResult>> GetNpmListOfComponentsFromRepo(string[] repoList, IJFrogService jFrogService)
{
if (repoList != null && repoList.Length > 0)
{
foreach (var repo in repoList)
{
var componentRepoData = await jFrogService.GetNpmComponentDataByRepo(repo) ?? new List<AqlResult>();
aqlResultList.AddRange(componentRepoData);
}
}

return aqlResultList;
}

private static AqlResult GetArtifactoryRepoName(List<AqlResult> aqlResultList, Component component)
{
AqlResult repoName = aqlResultList.Find(x => x.properties.Any(p => p.key == "pypi.normalized.name" && p.value == component.Name) && x.properties.Any(p => p.key == "pypi.version" && p.value == component.Version));
string jfrogpackageName = GetFullNameOfComponent(component);
AqlResult repoName = aqlResultList.Find(x => x.properties.Any(p => p.key == "pypi.normalized.name" && p.value == jfrogpackageName) && x.properties.Any(p => p.key == "pypi.version" && p.value == component.Version));

return repoName;
}


private static AqlResult GetNpmArtifactoryRepoName(List<AqlResult> aqlResultList, Component component)
{
string jfrogpackageName = GetFullNameOfComponent(component);
AqlResult repoName = aqlResultList.Find(x => x.properties.Any(p => p.key == "npm.name" && p.value == jfrogpackageName) && x.properties.Any(p => p.key == "npm.version" && p.value == component.Version));

return repoName;
}


private static AqlResult GetArtifactoryRepoNameForConan(List<AqlResult> aqlResultList, Component component)
{
Expand All @@ -1198,6 +1244,17 @@ private static AqlResult GetArtifactoryRepoNameForConan(List<AqlResult> aqlResul
return repoName;
}

private static string GetFullNameOfComponent(Component item)
{
if (!string.IsNullOrEmpty(item.Group))
{
return $"{item.Group}/{item.Name}";
}
else
{
return item.Name;
}
}
public static void UpdateBomArtifactoryRepoUrl(ref Bom bom, List<ComponentsToArtifactory> componentsUploaded)
{
foreach (var component in componentsUploaded)
Expand Down
Loading

0 comments on commit 8c60d62

Please sign in to comment.