Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Package Feature :Create Packages in SW360. #256

Open
wants to merge 8 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/LCT.APICommunications.UTest/SW360ApicommunicationUTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,50 @@ public void SW360Apicommunication_UpdateComponent_ReturnsInvalidOperationExcepti
HttpContent httpContent;
var jsonString = JsonConvert.SerializeObject("");
httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

//Arrange
SW360Apicommunication sW360Apicommunication = new SW360Apicommunication(connectionSettings);

//Assert
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.UpdateComponent("", httpContent));
}
[Test]
public void SW360Apicommunication_GetPackages_ReturnsInvalidOperationException()
{
//Act
SW360Apicommunication sW360Apicommunication = new SW360Apicommunication(connectionSettings);

//Assert
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.GetPackages());
}
[Test]
public void SW360Apicommunication_Updatepackage_ReturnsInvalidOperationException()
{
//Arrange
HttpContent httpContent;
var jsonString = JsonConvert.SerializeObject("");
httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

//Arrange
SW360Apicommunication sW360Apicommunication = new SW360Apicommunication(connectionSettings);

//Assert
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.UpdatePackage(httpContent, ""));
}
[Test]
public void SW360Apicommunication_LinkPackagesToProject_ReturnsInvalidOperationException()
{
//Arrange
HttpContent httpContent;
var jsonString = JsonConvert.SerializeObject("");
httpContent = new StringContent(jsonString, Encoding.UTF8, "application/json");

//Arrange
SW360Apicommunication sW360Apicommunication = new SW360Apicommunication(connectionSettings);

//Assert
Assert.ThrowsAsync<InvalidOperationException>(async () => await sW360Apicommunication.LinkPackagesToProject(httpContent, ""));
}

}
}
6 changes: 5 additions & 1 deletion src/LCT.APICommunications/ApiConstant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static class ApiConstant
public const string SOURCE = "SOURCE";
public const string Email = "Email";
public const string Oss = "OSS";
public const string Library = "LIBRARY";
public const string POST = "POST";
public const string Public = "public";
public const string PurlId = "package-url";
Expand All @@ -74,7 +75,10 @@ public static class ApiConstant
public const string InvalidArtifactory = "Invalid artifactory";
public const string PackageNotFound = "Package Not Found";
public const string ArtifactoryRepoName = "ArtifactoryRepoName";
public const string JfrogArtifactoryApiSearchAql = $"/api/search/aql";
public const string JfrogArtifactoryApiSearchAql = $"/api/search/aql";
public const string Sw360PackageApiSuffix = "/resource/api/packages";
public const string LinkPackagesApiSuffix = "/link/packages";
public static readonly List<int> APIRetryIntervals = [5, 10, 30]; // in seconds

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ public interface ISw360ApiCommunication
void DownloadAttachmentUsingWebClient(string attachmentDownloadLink, string fileName);
Task<HttpResponseMessage> GetComponentDetailsByUrl(string componentLink);
Task<HttpResponseMessage> GetAllReleasesWithAllData(int page, int pageEntries);
Task<string> GetPackages();
Task<HttpResponseMessage> CreatePackage(CreatePackage createPackageContent);
Task<HttpResponseMessage> UpdatePackage(HttpContent httpContent, string packageId);
Task<HttpResponseMessage> LinkPackagesToProject(HttpContent httpContent, string sw360ProjectId);
}
}
26 changes: 26 additions & 0 deletions src/LCT.APICommunications/Model/CreatePackage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LCT.APICommunications.Model
{
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class CreatePackage
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("version")]
public string Version { get; set; }

[JsonProperty("componentType")]
public string ComponentType { get; set; }

[JsonProperty("packageType")]
public string PackageType { get; set; }
[JsonProperty("purl")]
public string Purl { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/LCT.APICommunications/Model/PackageCreateStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LCT.APICommunications.Model
{
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class PackageCreateStatus
{
public bool IsCreated { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/LCT.APICommunications/Model/PackageEmbedded.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LCT.APICommunications.Model
{
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class PackageEmbedded
{
[JsonProperty("sw360:packages")]
public IList<Sw360Packages> Sw360packages { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/LCT.APICommunications/Model/PackageLinked.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LCT.APICommunications.Model
{
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class PackageLinked
{
public string PackageName { get; set; } = string.Empty;

public string Version { get; set; } = string.Empty;

public string PackageId { get; set; } = string.Empty;
public string Packagelink { get; set; } = string.Empty;
}
}
14 changes: 14 additions & 0 deletions src/LCT.APICommunications/Model/PackageUpdateStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LCT.APICommunications.Model
{
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class PackageUpdateStatus
{
public bool IsUpdated { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/LCT.APICommunications/Model/PackagesModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LCT.APICommunications.Model
{
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class PackagesModel
{
[JsonProperty("_embedded")]
public PackageEmbedded Embedded { get; set; }
}
}
24 changes: 24 additions & 0 deletions src/LCT.APICommunications/Model/Sw360Packages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LCT.APICommunications.Model
{
public class Sw360Packages
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("version")]
public string Version { get; set; }

[JsonProperty("_links")]
public Links Links { get; set; }

[JsonProperty("purl")]
public string Purl { get; set; }
}
}
28 changes: 28 additions & 0 deletions src/LCT.APICommunications/Model/UpdatePackage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LCT.APICommunications.Model
{
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class UpdatePackage
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("version")]
public string Version { get; set; }

[JsonProperty("componentType")]
public string ComponentType { get; set; }

[JsonProperty("packageType")]
public string PackageType { get; set; }
[JsonProperty("purl")]
public string Purl { get; set; }
[JsonProperty("releaseId")]
public string ReleaseId { get; set; }
}
}
27 changes: 25 additions & 2 deletions src/LCT.APICommunications/SW360Apicommunication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class SW360Apicommunication : ISw360ApiCommunication
private readonly string sw360ReleaseByExternalId;
private readonly string sw360ComponentByExternalId;
private readonly string sw360UsersApi;
private readonly string sw360PackageApi;
private readonly int timeOut;
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private static IEnvironmentHelper environmentHelper = new EnvironmentHelper();
Expand All @@ -63,9 +64,14 @@ public SW360Apicommunication(SW360ConnectionSettings sw360ConnectionSettings)
sw360ComponentByExternalId = $"{sw360ConnectionSettings.SW360URL}{ApiConstant.Sw360ComponentByExternalId}";
sw360ReleaseByExternalId = $"{sw360ConnectionSettings.SW360URL}{ApiConstant.Sw360ReleaseByExternalId}";
sw360UsersApi = $"{sw360ConnectionSettings.SW360URL}{ApiConstant.Sw360UsersSuffix}";
sw360PackageApi = $"{sw360ConnectionSettings.SW360URL}{ApiConstant.Sw360PackageApiSuffix}";
}


public async Task<string> GetPackages()
{
HttpClient httpClient = GetHttpClient();
return await httpClient.GetStringAsync(sw360PackageApi);
}
public async Task<string> GetProjects()
{
HttpClient httpClient = GetHttpClient();
Expand Down Expand Up @@ -331,7 +337,24 @@ public async Task<HttpResponseMessage> GetAllReleasesWithAllData(int page, int p
{
HttpClient httpClient = GetHttpClient();
string url = $"{sw360ReleaseApi}?page={page}&allDetails=true&page_entries={pageEntries}";
return await httpClient.GetAsync(url);
return await httpClient.GetAsync(url);
}
public async Task<HttpResponseMessage> CreatePackage(CreatePackage createPackageContent)
{
HttpClient httpClient = GetHttpClient();
return await httpClient.PostAsJsonAsync(sw360PackageApi, createPackageContent);
}
public async Task<HttpResponseMessage> UpdatePackage( HttpContent httpContent, string packageId)
{
HttpClient httpClient = GetHttpClient();
string packageApi = $"{sw360PackageApi}/{packageId}";
return await httpClient.PatchAsync(packageApi, httpContent);
}
public async Task<HttpResponseMessage> LinkPackagesToProject(HttpContent httpContent, string sw360ProjectId)
{
HttpClient httpClient = GetHttpClient();
string url = $"{sw360ProjectsApi}/{sw360ProjectId}/{ApiConstant.LinkPackagesApiSuffix}";
return await httpClient.PatchAsync(url, httpContent);
}
#endregion

Expand Down
6 changes: 6 additions & 0 deletions src/LCT.Common/Model/ComparisonBomData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ public class ComparisonBomData
public string FossologyLink { get; set; }
public string ReleaseID { get; set; }
public string AlpineSource { get; set; }
public string Purl { get; set; }
public string PackageLink { get; set; }
public string PackageStatus { get; set; }
public string IsPackageCreated { get; set; }
public string PackageName { get; set; }
public string PackageId { get; set; }
}
}
7 changes: 6 additions & 1 deletion src/LCT.Common/Model/Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public class Components
public string IsDev { get; set; }
[JsonIgnore]
public string ExcludeComponent { get; set; }

[JsonProperty("purl")]
public string Purl { get; set; }
public string PackageLink { get; set; }
[JsonIgnore]
public string PackageStatus { get; set; }
public string PackageName { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/LCT.Facade/Interfaces/ISW360ApicommunicationFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// SPDX-License-Identifier: MIT
// --------------------------------------------------------------------------------------------------------------------

using LCT.APICommunications;
using LCT.APICommunications.Model;
using System.Net.Http;
using System.Threading.Tasks;
Expand Down Expand Up @@ -44,5 +45,9 @@ public interface ISW360ApicommunicationFacade
Task<HttpResponseMessage> GetReleaseByExternalId(string purlId,string externalIdKey = "");
Task<HttpResponseMessage> GetComponentByExternalId(string purlId, string externalIdKey = "");
Task<HttpResponseMessage> GetAllReleasesWithAllData(int page, int pageEntries);
Task<string> GetPackages();
Task<HttpResponseMessage> CreatePackage(CreatePackage createComponentContent);
Task<HttpResponseMessage> UpdatePackage(HttpContent httpContent, string packageId);
Task<HttpResponseMessage> LinkPackagesToProject(HttpContent httpContent, string sw360ProjectId);
}
}
31 changes: 30 additions & 1 deletion src/LCT.Facade/SW360ApicommunicationFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public SW360ApicommunicationFacade(ISw360ApiCommunication sw360ApiCommunication,
m_sw360ApiCommunication = sw360ApiCommunication;
m_TestMode = testMode;
}

public Task<string> GetPackages()
{
return m_sw360ApiCommunication.GetPackages();
}
public Task<string> GetProjects()
{
return m_sw360ApiCommunication.GetProjects();
Expand Down Expand Up @@ -97,7 +100,15 @@ public async Task<HttpResponseMessage> LinkReleasesToProject(HttpContent httpCon

return await m_sw360ApiCommunication.LinkReleasesToProject(httpContent, sw360ProjectId);
}
public async Task<HttpResponseMessage> LinkPackagesToProject(HttpContent httpContent, string sw360ProjectId)
{
if (m_TestMode)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}

return await m_sw360ApiCommunication.LinkPackagesToProject(httpContent, sw360ProjectId);
}
public async Task<HttpResponseMessage> CreateComponent(CreateComponent createComponentContent)
{
if (m_TestMode)
Expand Down Expand Up @@ -199,5 +210,23 @@ public Task<HttpResponseMessage> GetAllReleasesWithAllData(int page, int pageEnt
{
return m_sw360ApiCommunication.GetAllReleasesWithAllData(page, pageEntries);
}
public async Task<HttpResponseMessage> CreatePackage(CreatePackage createPackageContent)
{
if (m_TestMode)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}

return await m_sw360ApiCommunication.CreatePackage(createPackageContent);
}
public async Task<HttpResponseMessage> UpdatePackage(HttpContent httpContent,string packageId)
{
if (m_TestMode)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}

return await m_sw360ApiCommunication.UpdatePackage(httpContent,packageId);
}
}
}
Loading
Loading