Skip to content

Commit

Permalink
feat(REST): New endpoint split components
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Ali <alimuhammad@siemens.com>
  • Loading branch information
rudra-superrr authored and muhammadali9699 committed Jul 7, 2023
1 parent 9c8b759 commit 7a88723
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -944,29 +944,30 @@ private void mergeAttachments(Component mergeSelection, Component mergeTarget, C
if (mergeTarget.getAttachments() == null) {
mergeTarget.setAttachments(new HashSet<>());
}

Set<String> attachmentIdsSelected = mergeSelection.getAttachments().stream()
.map(Attachment::getAttachmentContentId).collect(Collectors.toSet());
// add new attachments from source
Set<Attachment> attachmentsToAdd = new HashSet<>();
mergeSource.getAttachments().forEach(a -> {
if (attachmentIdsSelected.contains(a.getAttachmentContentId())) {
attachmentsToAdd.add(a);
}
});
// remove moved attachments in source
attachmentsToAdd.forEach(a -> {
mergeTarget.addToAttachments(a);
mergeSource.getAttachments().remove(a);
});
// delete unchosen attachments from target
Set<Attachment> attachmentsToDelete = new HashSet<>();
mergeTarget.getAttachments().forEach(a -> {
if (!attachmentIdsSelected.contains(a.getAttachmentContentId())) {
attachmentsToDelete.add(a);
}
});
mergeTarget.getAttachments().removeAll(attachmentsToDelete);
if (mergeSelection.getAttachments() != null) {
Set<String> attachmentIdsSelected = mergeSelection.getAttachments().stream()
.map(Attachment::getAttachmentContentId).collect(Collectors.toSet());
// add new attachments from source
Set<Attachment> attachmentsToAdd = new HashSet<>();
mergeSource.getAttachments().forEach(a -> {
if (attachmentIdsSelected.contains(a.getAttachmentContentId())) {
attachmentsToAdd.add(a);
}
});
// remove moved attachments in source
attachmentsToAdd.forEach(a -> {
mergeTarget.addToAttachments(a);
mergeSource.getAttachments().remove(a);
});
// delete unchosen attachments from target
Set<Attachment> attachmentsToDelete = new HashSet<>();
mergeTarget.getAttachments().forEach(a -> {
if (!attachmentIdsSelected.contains(a.getAttachmentContentId())) {
attachmentsToDelete.add(a);
}
});
mergeTarget.getAttachments().removeAll(attachmentsToDelete);
}
}

private void transferReleases(Set<String> releaseIds, Component mergeTarget, Component mergeSource) throws SW360Exception {
Expand Down
34 changes: 34 additions & 0 deletions rest/resource-server/src/docs/asciidoc/components.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,40 @@ include::{snippets}/should_document_update_component/http-response.adoc[]
include::{snippets}/should_document_update_component/links.adoc[]


[[resources-components-update]]
==== Merge a component

A `PATCH` request is used to merge two components

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

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

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

===== Links
include::{snippets}/should_document_merge_components/links.adoc[]

[[resources-components-update]]
==== Split a component

A `PATCH` request is used to split two components

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

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

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

===== Links
include::{snippets}/should_document_split_components/links.adoc[]

[[resources-components-delete]]
==== Delete a component

Expand Down
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 @@ -650,3 +650,17 @@ include::{snippets}/should_document_import_sbom/curl-request.adoc[]

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

[[resources-project-getprojectcount]]
==== Get project count of a user

A `GET` request will get project count of a user.

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

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

===== Example response
include::{snippets}/should_document_get_project_count/http-response.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -696,4 +696,36 @@ private ImportBomRequestPreparation handleImportBomRequestPreparation(ImportBomR
}
return importBomRequestPreparationResponse;
}

@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = COMPONENTS_URL + "/mergecomponents", method = RequestMethod.PATCH)
public ResponseEntity<RequestStatus> mergeComponents(
@RequestParam(value = "mergeTargetId", required = true) String mergeTargetId,
@RequestParam(value = "mergeSourceId", required = true) String mergeSourceId,
@RequestBody Component mergeSelection ) throws TException {


User sw360User = restControllerHelper.getSw360UserFromAuthentication();

// perform the real merge, update merge target and delete merge sources
RequestStatus requestStatus = componentService.mergeComponents(mergeTargetId, mergeSourceId, mergeSelection, sw360User);

return new ResponseEntity<>(requestStatus, HttpStatus.OK);
}

@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = COMPONENTS_URL + "/splitComponents", method = RequestMethod.PATCH)
public ResponseEntity<RequestStatus> splitComponents(
@RequestBody Map<String, Component> componentMap) throws TException {

User sw360User = restControllerHelper.getSw360UserFromAuthentication();

Component srcComponent = componentMap.get("srcComponent");
Component targetComponent = componentMap.get("targetComponent");

// perform the real merge, update merge target and delete merge source
RequestStatus requestStatus = componentService.splitComponents(srcComponent, targetComponent, sw360User);

return new ResponseEntity<>(requestStatus, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,37 @@ public ImportBomRequestPreparation prepareImportSBOM(User user, String attachmen
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.prepareImportBom(user, attachmentContentId);
}

public RequestStatus mergeComponents(String componentTargetId, String componentSourceId, Component componentSelection, User user) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
RequestStatus requestStatus;
requestStatus = sw360ComponentClient.mergeComponents(componentTargetId, componentSourceId, componentSelection, user);

if (requestStatus == RequestStatus.IN_USE) {
throw new HttpMessageNotReadableException("Component already in use.");
} else if (requestStatus == RequestStatus.FAILURE) {
throw new HttpMessageNotReadableException("Cannot merge these components");
} else if (requestStatus == RequestStatus.ACCESS_DENIED) {
throw new RuntimeException("Access denied");
}

return requestStatus;
}


public RequestStatus splitComponents(Component srcComponent, Component targetComponent, User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
RequestStatus requestStatus;
requestStatus = sw360ComponentClient.splitComponent(srcComponent, targetComponent, sw360User);

if (requestStatus == RequestStatus.IN_USE) {
throw new HttpMessageNotReadableException("Component already in use.");
} else if (requestStatus == RequestStatus.FAILURE) {
throw new HttpMessageNotReadableException("Cannot split these components");
} else if (requestStatus == RequestStatus.ACCESS_DENIED) {
throw new RuntimeException("Access denied...!");
}

return requestStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ static abstract class EmbeddedProjectMixin extends ProjectMixin {
abstract public boolean isEnableVulnerabilitiesDisplay();

@Override
@JsonIgnore
abstract public ProjectState getState();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,11 @@ public Project convertToEmbeddedProject(Project project) {
Project embeddedProject = new EmbeddedProject();
embeddedProject.setName(project.getName());
embeddedProject.setId(project.getId());
embeddedProject.setDescription(project.getDescription());
embeddedProject.setProjectResponsible(project.getProjectResponsible());
embeddedProject.setProjectType(project.getProjectType());
embeddedProject.setState(project.getState());
embeddedProject.setClearingState(project.getClearingState());
embeddedProject.setVersion(project.getVersion());
embeddedProject.setVisbility(project.getVisbility());
embeddedProject.setType(null);
Expand All @@ -521,6 +525,7 @@ public Component convertToEmbeddedComponent(Component component) {
Component embeddedComponent = new Component();
embeddedComponent.setId(component.getId());
embeddedComponent.setName(component.getName());
embeddedComponent.setDescription(component.getDescription());
embeddedComponent.setComponentType(component.getComponentType());
embeddedComponent.setVisbility(component.getVisbility());
embeddedComponent.setMainLicenseIds(component.getMainLicenseIds());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gson.JsonObject;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
Expand All @@ -39,7 +41,9 @@
import org.eclipse.sw360.datahandler.thrift.ReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
import org.eclipse.sw360.datahandler.thrift.RequestSummary;
import org.eclipse.sw360.datahandler.thrift.SW360Exception;
import org.eclipse.sw360.datahandler.thrift.Source;
import org.eclipse.sw360.datahandler.thrift.ThriftClients;
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent;
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentType;
Expand All @@ -58,6 +62,7 @@
import org.eclipse.sw360.datahandler.thrift.projects.ProjectClearingState;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectLink;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectProjectRelationship;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectService;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.vendors.Vendor;
import org.eclipse.sw360.datahandler.thrift.vulnerabilities.ProjectVulnerabilityRating;
Expand Down Expand Up @@ -1209,4 +1214,17 @@ public static TSerializer getJsonSerializer() {
}
return null;
}

@RequestMapping(value = PROJECTS_URL + "/projectcount", method = RequestMethod.GET)
public void getUserProjectCount(HttpServletResponse response) throws TException {
User sw360User = restControllerHelper.getSw360UserFromAuthentication();
try {
JsonObject resultJson = new JsonObject();
resultJson.addProperty("status", "success");
resultJson.addProperty("count", projectService.getMyAccessibleProjectCounts(sw360User));
response.getWriter().write(resultJson.toString());
}catch (IOException e) {
throw new SW360Exception(e.getMessage());
}
}
}
Loading

0 comments on commit 7a88723

Please sign in to comment.