Skip to content

Commit

Permalink
feat(rest): listing license clearing info of a project.
Browse files Browse the repository at this point in the history
Signed-off-by: rudra-superrr <rudra.chopra@siemens.com>
  • Loading branch information
rudra-superrr committed Jul 10, 2023
1 parent 0e75910 commit a115c7f
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
16 changes: 16 additions & 0 deletions rest/resource-server/src/docs/asciidoc/projects.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,22 @@ include::{snippets}/should_document_update_project_release_relationship/curl-req
===== Example response
include::{snippets}/should_document_update_project_release_relationship/http-response.adoc[]


[[resources-project-get-project-license-clearing-information]]
==== Listing license clearing information

A `GET` request will get all releases of license clearing

===== Response structure
include::{snippets}/should_document_get_license_clearing/response-fields.adoc[]

===== Example request
include::{snippets}/should_document_get_license_clearing/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_get_license_clearing/http-response.adoc[]


[[resources-project-get-download-licenseinfo]]
==== Download License Info

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,24 @@ public License convertToEmbeddedLicense(String licenseId) {
return embeddedLicense;
}

public Release convertToEmbeddedLinkedRelease(Release release) {
Release embeddedRelease = new Release();
embeddedRelease.setId(release.getId());
embeddedRelease.setMainlineState(release.getMainlineState());
embeddedRelease.setClearingState(release.getClearingState());
embeddedRelease.setMainLicenseIds(release.getMainLicenseIds());
embeddedRelease.setOtherLicenseIds(release.getOtherLicenseIds());
embeddedRelease.setName(release.getName());
embeddedRelease.setComponentType(release.getComponentType());
embeddedRelease.setVersion(release.getVersion());
embeddedRelease.setType(null);
return embeddedRelease;
}

public void addEmbeddedReleasess(HalResource halResource, List<EntityModel<Release>> releases) {
halResource.addEmbeddedResource("sw360:releases", releases);
}

public User convertToEmbeddedUser(User user) {
User embeddedUser = new User();
embeddedUser.setId(user.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.hateoas.Link;
import org.eclipse.sw360.rest.resourceserver.release.ReleaseController;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -329,6 +331,34 @@ public ResponseEntity<CollectionModel<EntityModel<Project>>> getProjectsFiltered
mapOfProjects, true, sw360Projects, false);
}

@RequestMapping(value = PROJECTS_URL + "/{id}/licenseClearing", method = RequestMethod.GET)
public ResponseEntity licenseClearing(@PathVariable("id") String id, HttpServletRequest request)
throws URISyntaxException, TException {

final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
Project sw360Project = projectService.getProjectForUserById(id, sw360User);
String transitive = "true";

final Set<String> releaseIds = projectService.getReleaseIds(id, sw360User, transitive);
List<Release> releases = releaseIds.stream().map(relId -> wrapTException(() -> {
final Release sw360Release = releaseService.getReleaseForUserById(relId, sw360User);
releaseService.setComponentDependentFieldsInRelease(sw360Release, sw360User);
return sw360Release;
})).collect(Collectors.toList());

List<EntityModel<Release>> releaseList = releases.stream().map(sw360Release -> wrapTException(() -> {
final Release embeddedRelease = restControllerHelper.convertToEmbeddedLinkedRelease(sw360Release);
final HalResource<Release> releaseResource = new HalResource<>(embeddedRelease);
Link releaseLink = linkTo(ReleaseController.class)
.slash("api/releases/" + embeddedRelease.getId()).withSelfRel();
releaseResource.add(releaseLink);
return releaseResource;
})).collect(Collectors.toList());

HalResource<Project> userHalResource = createHalLicenseClearing(sw360Project, sw360User, releaseList);
return new ResponseEntity<>(userHalResource, HttpStatus.OK);
}

@RequestMapping(value = PROJECTS_URL + "/{id}", method = RequestMethod.GET)
public ResponseEntity<EntityModel<Project>> getProject(
@PathVariable("id") String id) throws TException {
Expand Down Expand Up @@ -1122,6 +1152,27 @@ public RepositoryLinksResource process(RepositoryLinksResource resource) {
return resource;
}

private HalResource<Project> createHalLicenseClearing(Project sw360Project, User sw360User, List<EntityModel<Release>> releases)
throws TException {
Project sw360 = new Project();
Map<String, ProjectReleaseRelationship> releaseIdToUsage = sw360Project.getReleaseIdToUsage();
sw360.setReleaseIdToUsage(sw360Project.getReleaseIdToUsage());
sw360.setLinkedProjects(sw360Project.getLinkedProjects());
sw360.unsetState();
sw360.unsetEnableSvm();
sw360.unsetProjectType();
sw360.unsetVisbility();
sw360.unsetSecurityResponsibles();
sw360.unsetEnableVulnerabilitiesDisplay();
sw360.unsetConsiderReleasesFromExternalList();
HalResource<Project> halProject = new HalResource<>(sw360);

if (releaseIdToUsage != null) {
restControllerHelper.addEmbeddedReleasess(halProject, releases);
}
return halProject;
}

private HalResource<Project> createHalProject(Project sw360Project, User sw360User) throws TException {
HalResource<Project> halProject = new HalResource<>(sw360Project);
User projectCreator = restControllerHelper.getUserByEmail(sw360Project.getCreatedBy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ public void before() throws TException, IOException {
release.setComponentId("12356115");
release.setClearingState(ClearingState.APPROVED);
release.setExternalIds(Collections.singletonMap("mainline-id-component", "1432"));
release.setMainlineState(MainlineState.MAINLINE);
release.setMainLicenseIds(new HashSet<>(Arrays.asList("GPL-2.0-or-later", "Apache-2.0")));
release.setOtherLicenseIds(new HashSet<>(Arrays.asList("LGPL-2.0")));

Release release2 = new Release();
release2.setId("5578999");
Expand All @@ -429,6 +432,9 @@ public void before() throws TException, IOException {
release2.setClearingState(ClearingState.APPROVED);
release2.setExternalIds(Collections.singletonMap("mainline-id-component", "1771"));
release2.setComponentType(ComponentType.COTS);
release2.setMainlineState(MainlineState.MAINLINE);
release2.setMainLicenseIds(new HashSet<>(Arrays.asList("Apache-2.0")));
release2.setOtherLicenseIds(new HashSet<>(Arrays.asList("GPL-2.0")));

Release rel = new Release();
Map<String, String> releaseExternalIds = new HashMap<>();
Expand Down Expand Up @@ -1010,6 +1016,28 @@ public void should_document_get_projects_by_externalIds() throws Exception {
)));
}

@Test
public void should_document_get_license_clearing() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(get("/api/projects/" + project.getId() + "/licenseClearing")
.header("Authorization", "Bearer " + accessToken)
.param("page", "0")
.param("page_entries", "5")
.param("sort", "name,desc")
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk())
.andDo(this.documentationHandler.document(
responseFields(
fieldWithPath("enableSvm").description("Security vulnerability monitoring flag"),
fieldWithPath("considerReleasesFromExternalList").description("Consider list of releases from existing external list"),
fieldWithPath("enableVulnerabilitiesDisplay").description("Displaying vulnerabilities flag."),
subsectionWithPath("linkedReleases").description("The relationship between linked releases of the project"),
subsectionWithPath("linkedProjects").description("The `linked project id` - metadata of linked projects (`enableSvm` - whether linked projects will be part of SVM, `projectRelationship` - relationship between linked project and the project. Possible values: " + Arrays.asList(ProjectRelationship.values())),
subsectionWithPath("_embedded.sw360:releases").description("An array of linked releases"),
subsectionWithPath("_links").description("<<resources-index-links,Links>> to other resources")
)));
}

@Test
public void should_document_get_linked_projects() throws Exception {
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
Expand Down

0 comments on commit a115c7f

Please sign in to comment.