Skip to content

Commit

Permalink
Updated for requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ragavareddychalapala committed Jan 6, 2025
1 parent 8b24886 commit 8601964
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 78 deletions.
10 changes: 5 additions & 5 deletions src/AritfactoryUploader.UTest/ArtifactoryUploaderTest.cs
Original file line number Diff line number Diff line change
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>(), component))
jFrogServiceMock.Setup(x => x.GetPackageInfo(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>(),component))
jFrogServiceMock.Setup(x => x.GetPackageInfo(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>(), component))
jFrogServiceMock.Setup(x => x.GetPackageInfo(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>(),component))
jFrogServiceMock.Setup(x => x.GetPackageInfo(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>(), component))
jFrogServiceMock.Setup(x => x.GetPackageInfo(component))
.ThrowsAsync(new InvalidOperationException());
ArtfactoryUploader.jFrogService = jFrogServiceMock.Object;
ArtfactoryUploader.JFrogApiCommInstance = jfrogApicommunicationMock.Object;
Expand Down
8 changes: 4 additions & 4 deletions src/AritfactoryUploader.UTest/PackageUploadHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenPypiRepoExists_Ret
PackageUploadHelper.jFrogService = jFrogServiceMock.Object;

// Act
var result = await PackageUploadHelper.GetSrcRepoDetailsForPyPiOrConanPackages(item);
var result = await PackageUploadHelper.GetSrcRepoDetailsForComponent(item);

// Assert
Assert.IsNotNull(result);
Expand Down Expand Up @@ -487,7 +487,7 @@ public async static Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenConanRepoEx
PackageUploadHelper.jFrogService = jFrogServiceMock.Object;

// Act
var result = await PackageUploadHelper.GetSrcRepoDetailsForPyPiOrConanPackages(item);
var result = await PackageUploadHelper.GetSrcRepoDetailsForComponent(item);

// Assert
Assert.IsNotNull(result);
Expand All @@ -507,7 +507,7 @@ public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenNoRepoExists_Retur
PackageUploadHelper.jFrogService = jFrogServiceMock.Object;

// Act
var result = await PackageUploadHelper.GetSrcRepoDetailsForPyPiOrConanPackages(item);
var result = await PackageUploadHelper.GetSrcRepoDetailsForComponent(item);

// Assert
Assert.IsNull(result);
Expand Down Expand Up @@ -903,7 +903,7 @@ public async Task GetSrcRepoDetailsForPyPiOrConanPackages_WhenNpmRepoExists_Retu


// Act
var result = await PackageUploadHelper.GetSrcRepoDetailsForPyPiOrConanPackages(item);
var result = await PackageUploadHelper.GetSrcRepoDetailsForComponent(item);

// Assert
Assert.IsNotNull(result);
Expand Down
31 changes: 5 additions & 26 deletions src/ArtifactoryUploader/ArtifactoryUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,10 @@ public static void SetConfigurationValues()

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




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


// Handle DEBIAN package name mismatch
Expand All @@ -130,31 +127,13 @@ async Task<AqlResult> TryGetPackageInfo(string srcRepo, string packageName, stri
var lowerPath = component.Path.ToLower();


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

if (packageInfo != null)
{
component.CopyPackageApiUrl = component.CopyPackageApiUrl.ToLower();
}
}

// Retry with wildcard path if still not found
// 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}*", component);

if (packageInfo != null)
{
// Build URLs
string BuildUrl(string apiConstant) =>
$"{component.JfrogApi}{apiConstant}{component.SrcRepoName}/{packageInfo.Path}/{packageInfo.Name}" +
$"?to=/{component.DestRepoName}/{packageInfo.Path}/{packageInfo.Name}";

component.CopyPackageApiUrl = component.DryRun ? $"{BuildUrl(ApiConstant.CopyPackageApi)}&dry=1" : BuildUrl(ApiConstant.CopyPackageApi);
component.MovePackageApiUrl = component.DryRun ? $"{BuildUrl(ApiConstant.MovePackageApi)}&dry=1" : BuildUrl(ApiConstant.MovePackageApi);
}
}
}

return packageInfo;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ArtifactoryUploader/PackageUploadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async static Task<List<ComponentsToArtifactory>> GetComponentsToBeUploade
var packageType = GetPackageType(item);
if (packageType != PackageType.Unknown)
{
AqlResult aqlResult = await GetSrcRepoDetailsForPyPiOrConanPackages(item);
AqlResult aqlResult = await GetSrcRepoDetailsForComponent(item);
ComponentsToArtifactory components = new ComponentsToArtifactory()
{
Name = !string.IsNullOrEmpty(item.Group) ? $"{item.Group}/{item.Name}" : item.Name,
Expand Down Expand Up @@ -955,7 +955,7 @@ private static string GetComponentType(Component item)
return string.Empty;
}

public async static Task<AqlResult> GetSrcRepoDetailsForPyPiOrConanPackages(Component item)
public async static Task<AqlResult> GetSrcRepoDetailsForComponent(Component item)
{
if (item.Purl.Contains("pypi", StringComparison.OrdinalIgnoreCase))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface IJfrogAqlApiCommunication
/// <param name="packageName">repoName</param>
/// <param name="path">repoName</param>
/// <returns>AqlResult</returns>
Task<HttpResponseMessage> GetPackageInfo(string repoName, string packageName = null, string path = null, ComponentsToArtifactory component = null);
Task<HttpResponseMessage> GetPackageInfo(ComponentsToArtifactory component = null);

/// <summary>
/// Checks connectivity with JFrog server
Expand Down
27 changes: 12 additions & 15 deletions src/LCT.APICommunications/JfrogAqlApiCommunication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,12 @@ public async Task<HttpResponseMessage> GetPypiComponentDataByRepo(string repoNam
/// <summary>
/// Gets the package information in the repo, via the name or path
/// </summary>
/// <param name="repoName">repoName</param>
/// <param name="packageName">repoName</param>
/// <param name="path">repoName</param>
/// <returns>AqlResult</returns>
public async Task<HttpResponseMessage> GetPackageInfo(string repoName, string packageName = null, string path = null, ComponentsToArtifactory component = null)

public async Task<HttpResponseMessage> GetPackageInfo(ComponentsToArtifactory component = null)
{
ValidateParameters(packageName, path);
ValidateParameters(component.JfrogPackageName, component.Path);

var aqlQueryToBody = BuildAqlQuery(repoName, packageName, path,component);
var aqlQueryToBody = BuildAqlQuery(component);

string uri = $"{DomainName}{ApiConstant.JfrogArtifactoryApiSearchAql}";
HttpContent httpContent = new StringContent(aqlQueryToBody);
Expand All @@ -133,14 +130,14 @@ private static void ValidateParameters(string packageName, string path)
}
}

private static string BuildAqlQuery(string repoName, string packageName, string path, ComponentsToArtifactory component)
private static string BuildAqlQuery( ComponentsToArtifactory component)
{

if (component.ComponentType.Equals("NPM", StringComparison.InvariantCultureIgnoreCase))
{
var queryList = new List<string>
{
$"\"repo\":{{\"$eq\":\"{repoName}\"}}",
$"\"repo\":{{\"$eq\":\"{component.SrcRepoName}\"}}",
$"\"@npm.name\":{{\"$eq\":\"{component.Name}\"}}",
$"\"@npm.version\":{{\"$eq\":\"{component.Version}\"}}"
};
Expand All @@ -154,7 +151,7 @@ private static string BuildAqlQuery(string repoName, string packageName, string
{
var queryList = new List<string>
{
$"\"repo\":{{\"$eq\":\"{repoName}\"}}",
$"\"repo\":{{\"$eq\":\"{component.SrcRepoName}\"}}",
$"\"@pypi.normalized.name\":{{\"$eq\":\"{component.Name}\"}}",
$"\"@pypi.version\":{{\"$eq\":\"{component.Version}\"}}"
};
Expand All @@ -169,17 +166,17 @@ private static string BuildAqlQuery(string repoName, string packageName, string
{
var queryList = new List<string>()
{
$"\"repo\":{{\"$eq\":\"{repoName}\"}}"
$"\"repo\":{{\"$eq\":\"{component.SrcRepoName}\"}}"
};

if (!string.IsNullOrEmpty(path))
if (!string.IsNullOrEmpty(component.Path))
{
queryList.Add($"\"path\":{{\"$match\":\"{path}\"}}");
queryList.Add($"\"path\":{{\"$match\":\"{component.Path}\"}}");
}

if (!string.IsNullOrEmpty(packageName))
if (!string.IsNullOrEmpty(component.JfrogPackageName))
{
queryList.Add($"\"name\":{{\"$match\":\"{packageName}\"}}");
queryList.Add($"\"name\":{{\"$match\":\"{component.JfrogPackageName}\"}}");
}

StringBuilder query = new();
Expand Down
13 changes: 9 additions & 4 deletions src/LCT.Facade.UTest/JfrogAqlApiCommunicationFacadeUTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,26 @@ public async Task GetInternalComponentDataByRepo_Returnswith_HttpCodeNoContent()
public async Task GetPackageInfoByRepoAndPackageName_GetsPackageInfo_Successfully()
{
// Arrange
ComponentsToArtifactory component= new ComponentsToArtifactory();
ComponentsToArtifactory component = new ComponentsToArtifactory
{
SrcRepoName = "org1-nuget-nuget-remote-cache",
JfrogPackageName = "System.Numerics.Vectors.4.5.0.nupkg",
Path = string.Empty
};


HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);
Mock<IJfrogAqlApiCommunication> mockJfrogAqlApiCommunicationFacade =
new Mock<IJfrogAqlApiCommunication>();
mockJfrogAqlApiCommunicationFacade.
Setup(x => x.GetPackageInfo(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), component)).
Setup(x => x.GetPackageInfo(component)).
ReturnsAsync(httpResponseMessage);

// Act
JfrogAqlApiCommunicationFacade jfrogAqlApiCommunicationFacade =
new JfrogAqlApiCommunicationFacade(mockJfrogAqlApiCommunicationFacade.Object);
HttpResponseMessage actual =
await jfrogAqlApiCommunicationFacade.GetPackageInfo("org1-nuget-nuget-remote-cache", "System.Numerics.Vectors.4.5.0.nupkg",
string.Empty, component);
await jfrogAqlApiCommunicationFacade.GetPackageInfo(component);

//Assert
Assert.That(actual.StatusCode, Is.EqualTo(HttpStatusCode.OK));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface IJfrogAqlApiCommunicationFacade
/// <param name="packageName">repoName</param>
/// <param name="path">repoName</param>
/// <returns>AqlResult</returns>
Task<HttpResponseMessage> GetPackageInfo(string repoName, string packageName, string path, ComponentsToArtifactory component);
Task<HttpResponseMessage> GetPackageInfo(ComponentsToArtifactory component);


/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/LCT.Facade/JfrogAqlApiCommunicationFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public async Task<HttpResponseMessage> GetPypiComponentDataByRepo(string repoNam
/// <param name="packageName">repoName</param>
/// <param name="path">repoName</param>
/// <returns>AqlResult</returns>
public async Task<HttpResponseMessage> GetPackageInfo(string repoName, string packageName, string path, ComponentsToArtifactory component)
public async Task<HttpResponseMessage> GetPackageInfo(ComponentsToArtifactory component)
{
return await m_jfrogAqlApiCommunication.GetPackageInfo(repoName, packageName, path,component);
return await m_jfrogAqlApiCommunication.GetPackageInfo(component);
}
}
}
Loading

0 comments on commit 8601964

Please sign in to comment.