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

feat(rest): listing license clearing info of a project. #2029

Merged
merged 1 commit into from
Aug 2, 2023
Merged
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
14 changes: 14 additions & 0 deletions rest/resource-server/src/docs/asciidoc/projects.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,20 @@ include::{snippets}/should_document_unlink_packages/curl-request.adoc[]
===== Example response
include::{snippets}/should_document_unlink_packages/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 @@ -508,6 +508,16 @@ public void addEmbeddedPackage(HalResource<Package> halResource, Package pkg) {
halResource.addEmbeddedResource("sw360:packages", halPackage);
}

public HalResource<Release> addEmbeddedReleaseLinks(Release release) {
final Release embeddedRelease = convertToEmbeddedLinkedRelease(release);
final HalResource<Release> releaseResource = new HalResource<>(embeddedRelease);
Link releaseLink = linkTo(ReleaseController.class)
.slash("api/releases/" + embeddedRelease.getId()).withSelfRel();
releaseResource.add(releaseLink);

return releaseResource;
}

public void addEmbeddedAttachments(
HalResource halResource,
Set<Attachment> attachments) {
Expand Down Expand Up @@ -749,6 +759,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 addEmbeddedProjectReleases(HalResource halResource, List<EntityModel<Release>> releases) {
halResource.addEmbeddedResource("sw360:release", releases);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep sw360:release if you can

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already sw360:release.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rudra-superrr Oh, sorry I mean sw360:releases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I keep sw360:releases, then nested list will come in response structure.

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I keep sw360:releases, then nested list will come in response structure.

image

@rudra-superrr, I got it, thanks

}

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 @@ -96,6 +96,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 @@ -336,6 +338,29 @@ 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, @RequestParam(value = "transitive", required = true) String transitive, HttpServletRequest request)
throws URISyntaxException, TException {

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

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 HalResource<Release> releaseResource = restControllerHelper.addEmbeddedReleaseLinks(sw360Release);
return releaseResource;
})).collect(Collectors.toList());

HalResource<Project> userHalResource = createHalLicenseClearing(sw360Project, 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 @@ -1157,6 +1182,23 @@ public RepositoryLinksResource process(RepositoryLinksResource resource) {
return resource;
}

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

if (releaseIdToUsage != null) {
restControllerHelper.addEmbeddedProjectReleases(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 @@ -507,6 +507,9 @@ public void before() throws TException, IOException {
release.setClearingState(ClearingState.APPROVED);
release.setExternalIds(Collections.singletonMap("mainline-id-component", "1432"));
release.setPackageIds(linkedPackages);
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 @@ -523,6 +526,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 @@ -1114,6 +1120,35 @@ 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("transitive", "true")
.param("page", "0")
.param("page_entries", "5")
.param("sort", "name,desc")
.accept(MediaTypes.HAL_JSON))
.andExpect(status().isOk())
.andDo(this.documentationHandler.document(
requestParameters(
parameterWithName("transitive").description("Get the transitive releases"),
parameterWithName("page").description("Page of releases"),
parameterWithName("page_entries").description("Amount of releases page"),
parameterWithName("sort").description("Defines order of the releases")
),
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:release").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