Skip to content

Commit 42d0655

Browse files
committed
fix(rest): Add license information linking for project releases.
Signed-off-by: Nikesh kumar <kumar.nikesh@siemens.com>
1 parent fbea70a commit 42d0655

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java

+73
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
import org.eclipse.sw360.rest.resourceserver.vulnerability.Sw360VulnerabilityService;
102102
import org.eclipse.sw360.rest.resourceserver.vulnerability.VulnerabilityController;
103103
import org.jetbrains.annotations.NotNull;
104+
import org.jose4j.json.internal.json_simple.JSONObject;
104105
import org.springframework.beans.factory.annotation.Autowired;
105106
import org.springframework.boot.json.GsonJsonParser;
106107
import org.springframework.data.domain.Pageable;
@@ -119,6 +120,7 @@
119120
import org.springframework.security.access.prepost.PreAuthorize;
120121
import org.springframework.util.FileCopyUtils;
121122
import org.springframework.web.bind.annotation.PathVariable;
123+
import org.springframework.web.bind.annotation.PostMapping;
122124
import org.springframework.web.bind.annotation.RequestBody;
123125
import org.springframework.web.bind.annotation.RequestMapping;
124126
import org.springframework.web.bind.annotation.RequestMethod;
@@ -192,6 +194,8 @@ public class ProjectController implements RepresentationModelProcessor<Repositor
192194
private static final List<String> enumMainlineStateValues = Stream.of(MainlineState.values())
193195
.map(MainlineState::name)
194196
.collect(Collectors.toList());
197+
private static final ImmutableMap<String, String> RESPONSE_BODY_FOR_MODERATION_REQUEST_WITH_COMMIT = ImmutableMap.<String, String>builder()
198+
.put("message", "Unauthorized user or empty commit message passed.").build();
195199

196200
@NonNull
197201
private final Sw360ProjectService projectService;
@@ -3480,4 +3484,73 @@ public ResponseEntity<?> createDuplicateProjectWithDependencyNetwork(
34803484
return true;
34813485
};
34823486
}
3487+
3488+
@Operation(
3489+
summary = "Add licenses to linked releases of a project.",
3490+
description = "This API adds license information to linked releases of a project by processing the approved CLI attachments for each release. It categorizes releases based on the number of CLI attachments (single, multiple, or none) and updates their main and other licenses accordingly.",
3491+
tags = {"Project"},
3492+
parameters = {
3493+
@Parameter(
3494+
name = "projectId",
3495+
description = "The ID of the project whose linked releases need license updates.",
3496+
required = true,
3497+
example = "12345",
3498+
schema = @Schema(type = "string")
3499+
)
3500+
},
3501+
responses = {
3502+
@ApiResponse(
3503+
responseCode = "200",
3504+
description = "License information successfully added to linked releases.",
3505+
content = @Content(
3506+
mediaType = "application/hal+json",
3507+
schema = @Schema(type = "object", implementation = JSONObject.class),
3508+
examples = @ExampleObject(
3509+
value = "{\n \"one\": [\"Release1\", \"Release2\"],\n \"mul\": [\"Release3\"]\n}"
3510+
)
3511+
)
3512+
),
3513+
@ApiResponse(
3514+
responseCode = "500",
3515+
description = "Error occurred while processing license information for linked releases.",
3516+
content = @Content(
3517+
mediaType = "application/json",
3518+
examples = @ExampleObject(
3519+
value = "{\n \"error\": \"Error adding license info to linked releases.\"\n}"
3520+
)
3521+
)
3522+
)
3523+
}
3524+
)
3525+
@PostMapping(value = PROJECTS_URL + "/{id}/addLinkedRelesesLicenses")
3526+
public ResponseEntity<?> addLicenseToLinkedReleases(
3527+
@Parameter(description = "Project ID", example = "376576")
3528+
@PathVariable("id") String projectId,
3529+
@Parameter(description = "Comment message.")
3530+
@RequestParam(value = "comment", required = false) String comment
3531+
) throws TTransportException, TException {
3532+
try {
3533+
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
3534+
sw360User.setCommentMadeDuringModerationRequest(comment);
3535+
Project project = projectService.getProjectForUserById(projectId, sw360User);
3536+
3537+
if (!restControllerHelper.isWriteActionAllowed(project, sw360User) && comment == null) {
3538+
return new ResponseEntity<>(RESPONSE_BODY_FOR_MODERATION_REQUEST_WITH_COMMIT, HttpStatus.BAD_REQUEST);
3539+
}
3540+
RequestStatus requestStatus = projectService.addLicenseToLinkedReleases(projectId, sw360User);
3541+
3542+
switch (requestStatus) {
3543+
case SENT_TO_MODERATOR:
3544+
return new ResponseEntity<>(RESPONSE_BODY_FOR_MODERATION_REQUEST, HttpStatus.ACCEPTED);
3545+
case SUCCESS:
3546+
return ResponseEntity.ok().body(Map.of("message", "License information successfully added to linked releases."));
3547+
default:
3548+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error adding license info to linked releases.");
3549+
}
3550+
3551+
} catch (Exception e) {
3552+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
3553+
.body("Error adding license info to linked releases: " + e.getMessage());
3554+
}
3555+
}
34833556
}

rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/Sw360ProjectService.java

+97
Original file line numberDiff line numberDiff line change
@@ -1543,4 +1543,101 @@ public List<ReleaseLink> serveLinkedReleasesInDependencyNetworkByIndexPath(Strin
15431543
ProjectService.Iface sw360ProjectClient = getThriftProjectClient();
15441544
return sw360ProjectClient.getReleaseLinksOfProjectNetWorkByIndexPath(projectId, indexPath, sw360User);
15451545
}
1546+
1547+
public RequestStatus addLicenseToLinkedReleases(String projectId, User sw360User)
1548+
throws TTransportException, TException {
1549+
ThriftClients thriftClients = new ThriftClients();
1550+
ProjectService.Iface projectClient = getThriftProjectClient();
1551+
LicenseInfoService.Iface licenseInfoClient = thriftClients.makeLicenseInfoClient();
1552+
ComponentService.Iface componentClient = thriftClients.makeComponentClient();
1553+
JSONObject jsonResult = new JSONObject();
1554+
1555+
try {
1556+
Project project = projectClient.getProjectById(projectId, sw360User);
1557+
if (project == null) {
1558+
throw new IllegalArgumentException("Project with ID " + projectId + " not found.");
1559+
}
1560+
1561+
Set<String> releaseIds = CommonUtils.getNullToEmptyKeyset(project.getReleaseIdToUsage());
1562+
List<Release> releasesWithSingleCLI = new ArrayList<>();
1563+
List<Release> releasesWithMultipleCLI = new ArrayList<>();
1564+
boolean isModerationRequired = false;
1565+
1566+
for (String releaseId : releaseIds) {
1567+
Release release = componentClient.getReleaseById(releaseId, sw360User);
1568+
if (release == null) {
1569+
throw new IllegalArgumentException("Release with ID " + releaseId + " not found.");
1570+
}
1571+
1572+
List<Attachment> approvedCliAttachments = SW360Utils.getApprovedClxAttachmentForRelease(release);
1573+
if (approvedCliAttachments.isEmpty()) {
1574+
approvedCliAttachments = SW360Utils.getClxAttachmentForRelease(release);
1575+
}
1576+
1577+
if (approvedCliAttachments.size() == 1) {
1578+
processSingleAttachment(approvedCliAttachments.get(0), release, licenseInfoClient, sw360User);
1579+
releasesWithSingleCLI.add(release);
1580+
} else {
1581+
if (approvedCliAttachments.size() > 1) {
1582+
releasesWithMultipleCLI.add(release);
1583+
}
1584+
jsonResult.put(SW360Constants.STATUS, SW360Constants.FAILURE);
1585+
isModerationRequired = true;
1586+
}
1587+
1588+
componentClient.updateRelease(release, sw360User);
1589+
}
1590+
1591+
jsonResult.put("releasesWithSingleCLI", releasesWithSingleCLI);
1592+
jsonResult.put("releasesWithMultipleCLI", releasesWithMultipleCLI);
1593+
1594+
if (isModerationRequired) {
1595+
return RequestStatus.SENT_TO_MODERATOR;
1596+
}
1597+
1598+
return RequestStatus.SUCCESS;
1599+
1600+
} catch (Exception e) {
1601+
throw new RuntimeException("Error processing linked releases for project: " + projectId, e);
1602+
}
1603+
}
1604+
1605+
1606+
private void processSingleAttachment(Attachment attachment, Release release,
1607+
LicenseInfoService.Iface licenseInfoClient, User sw360User) throws TTransportException, TException {
1608+
String attachmentName = attachment.getFilename();
1609+
1610+
Set<String> mainLicenses = new HashSet<>();
1611+
Set<String> otherLicenses = new HashSet<>();
1612+
1613+
List<LicenseInfoParsingResult> licenseInfoResults = licenseInfoClient.getLicenseInfoForAttachment(release,
1614+
attachment.getAttachmentContentId(), true, sw360User);
1615+
1616+
if (attachmentName.endsWith(SW360Constants.RDF_FILE_EXTENSION)) {
1617+
licenseInfoResults.forEach(result -> {
1618+
if (result.getLicenseInfo() != null) {
1619+
mainLicenses.addAll(result.getLicenseInfo().getConcludedLicenseIds());
1620+
otherLicenses.addAll(result.getLicenseInfo().getLicenseNamesWithTexts().stream()
1621+
.map(LicenseNameWithText::getLicenseName).collect(Collectors.toSet()));
1622+
}
1623+
});
1624+
otherLicenses.removeAll(mainLicenses);
1625+
} else if (attachmentName.endsWith(SW360Constants.XML_FILE_EXTENSION)) {
1626+
licenseInfoResults.forEach(result -> {
1627+
if (result.getLicenseInfo() != null) {
1628+
result.getLicenseInfo().getLicenseNamesWithTexts().forEach(license -> {
1629+
if (SW360Constants.LICENSE_TYPE_GLOBAL.equals(license.getType())) {
1630+
mainLicenses.add(license.getLicenseName());
1631+
} else {
1632+
otherLicenses.add(license.getLicenseName());
1633+
}
1634+
});
1635+
}
1636+
});
1637+
}
1638+
1639+
release.setMainLicenseIds(mainLicenses);
1640+
release.setOtherLicenseIds(otherLicenses);
1641+
}
1642+
15461643
}

rest/resource-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ProjectSpecTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
import org.eclipse.sw360.rest.resourceserver.user.Sw360UserService;
7474
import org.eclipse.sw360.rest.resourceserver.vulnerability.Sw360VulnerabilityService;
7575
import org.hamcrest.Matchers;
76+
import org.jose4j.json.internal.json_simple.JSONArray;
77+
import org.jose4j.json.internal.json_simple.JSONObject;
7678
import org.junit.Before;
7779
import org.junit.Test;
7880
import org.junit.runner.RunWith;
@@ -3250,4 +3252,17 @@ public void should_document_get_projects_by_advance_search() throws Exception {
32503252
fieldWithPath("page.number").description("Number of the current page")
32513253
)));
32523254
}
3255+
3256+
@Test
3257+
public void should_add_license_to_linked_releases() throws Exception {
3258+
String projectId = "1234567";
3259+
String comment = "This is a test comment";
3260+
when(projectServiceMock.addLicenseToLinkedReleases(eq(projectId), any(User.class))).thenReturn(RequestStatus.SUCCESS);
3261+
3262+
MockHttpServletRequestBuilder requestBuilder = post("/api/projects/" + projectId + "/addLinkedRelesesLicenses")
3263+
.contentType(MediaType.APPLICATION_JSON)
3264+
.param("comment", comment)
3265+
.header("Authorization", TestHelper.generateAuthHeader(testUserId, testUserPassword));
3266+
this.mockMvc.perform(requestBuilder).andExpect(status().isOk()).andDo(this.documentationHandler.document());
3267+
}
32533268
}

0 commit comments

Comments
 (0)