Skip to content

feat: populate tree [IDE-712] #218

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ public interface ProductConstants {

String DIAGNOSTIC_SOURCE_SNYK_OSS = "Snyk Open Source";
String DIAGNOSTIC_SOURCE_SNYK_CODE = "Snyk Code";
String DIAGNOSTIC_SOURCE_SNYK_IAC = "Snyk Infrastructure";
String DIAGNOSTIC_SOURCE_SNYK_IAC = "Snyk IaC";

String DISPLAYED_OSS = "Snyk Open Source";
String DISPLAYED_CODE_SECURITY = "Code Security";
String DISPLAYED_CODE_QUALITY = "Code Quality";
String DISPLAYED_IAC = "Configuration";

String SEVERITY_CRITICAL = "critical";
String SEVERITY_HIGH = "high";
String SEVERITY_MEDIUM = "medium";
String SEVERITY_LOW = "low";

Map<String, String> LSP_SOURCE_TO_SCAN_PARAMS = Map.of(DIAGNOSTIC_SOURCE_SNYK_CODE, SCAN_PARAMS_CODE,
DIAGNOSTIC_SOURCE_SNYK_IAC, SCAN_PARAMS_IAC, DIAGNOSTIC_SOURCE_SNYK_OSS, SCAN_PARAMS_OSS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Path;
import java.util.Base64;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.osgi.framework.Bundle;

public class ResourceUtils {
Expand All @@ -15,7 +16,7 @@ public ResourceUtils() {
}

public static String getBase64Image(Bundle bundle, String icon) {
URL imageUrl = FileLocator.find(bundle, new Path("icons/" + icon), null);
URL imageUrl = FileLocator.find(bundle, new org.eclipse.core.runtime.Path("icons/" + icon), null);

byte[] imageData = getImageDataFromUrl(imageUrl);

Expand All @@ -42,4 +43,7 @@ private static byte[] getImageDataFromUrl(URL imageUrl) {
}
}

public static Path getFullPath(IResource resource) {
return resource.getLocation().toPath().toAbsolutePath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ public class SnykIcons {
public static final ImageDescriptor SEVERITY_LOW = Activator.getImageDescriptor("/icons/severity-low.png");

public static final ImageDescriptor ENABLED = Activator.getImageDescriptor("/icons/enabled.png");
public static final ImageDescriptor DISABLED = Activator.getImageDescriptor("/icons/transparent.png");
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,12 @@ public void reset() {
this.value = null;
this.imageDescriptor = null;
}


/**
* Provides the details to be displayed in the details view
* @return html details
*/
public String getDetails() {
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package io.snyk.eclipse.plugin.views.snyktoolview;

import static io.snyk.eclipse.plugin.domain.ProductConstants.DISPLAYED_CODE_QUALITY;
import static io.snyk.eclipse.plugin.domain.ProductConstants.DISPLAYED_CODE_SECURITY;
import static io.snyk.eclipse.plugin.domain.ProductConstants.DISPLAYED_IAC;
import static io.snyk.eclipse.plugin.domain.ProductConstants.DISPLAYED_OSS;

import java.nio.file.Path;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.model.WorkbenchLabelProvider;

import io.snyk.eclipse.plugin.domain.ProductConstants;

public class ContentRootNode extends BaseTreeNode {
private Path path;
private String name;

public ContentRootNode(String name, Path value) {
super(value);
reset();
this.name = name;
this.setPath(value);
}

@Override
public ImageDescriptor getImageDescriptor() {
WorkbenchLabelProvider labelProvider = new WorkbenchLabelProvider();
try {
var object = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
Image image = labelProvider.getImage(object);
if (image == null)
return null;

return ImageDescriptor.createFromImage(image);
} finally {
labelProvider.dispose();
}
}

public ProductTreeNode getProductNode(String product) {
if (product == null) {
return null;
}
switch (product) {
case ProductConstants.DISPLAYED_OSS:
return (ProductTreeNode) this.getChildren()[0];
case ProductConstants.DISPLAYED_CODE_SECURITY:
return (ProductTreeNode) this.getChildren()[1];
case ProductConstants.DISPLAYED_CODE_QUALITY:
return (ProductTreeNode) this.getChildren()[2];
case ProductConstants.DISPLAYED_IAC:
return (ProductTreeNode) this.getChildren()[3];
}
return null;
}

@Override
public void reset() {
var ossRootNode = new ProductTreeNode(DISPLAYED_OSS);
ossRootNode.setParent(this);

var codeSecurityRootNode = new ProductTreeNode(DISPLAYED_CODE_SECURITY);
codeSecurityRootNode.setParent(this);

var codeQualityRootNode = new ProductTreeNode(DISPLAYED_CODE_QUALITY);
codeQualityRootNode.setParent(this);

var iacRootNode = new ProductTreeNode(DISPLAYED_IAC);
iacRootNode.setParent(this);

ProductTreeNode[] productNodes = new ProductTreeNode[] { ossRootNode, codeSecurityRootNode, codeQualityRootNode, iacRootNode, };
this.setChildren(productNodes);
}

@Override
public String getText() {
return name;
}

public Path getPath() {
return path;
}

public void setPath(Path path) {
this.path = path;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.snyk.eclipse.plugin.views.snyktoolview;

import io.snyk.languageserver.protocolextension.FileTreeNode;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreeViewer;


/**
* This interface captures the externally used methods with the tool window.
* Having it, should allow for easier testing of the business logic apart from
Expand All @@ -16,7 +19,7 @@ public interface ISnykToolView {

String NODE_TEXT_SCANNING = "Scanning...";
String NODE_TEXT_NO_ISSUES_FOUND = "No issues found";
String NODE_TEXT_EROR = "An error occurred";
String NODE_TEXT_ERROR = "An error occurred";

/**
* Updates the text of the given node
Expand All @@ -26,50 +29,51 @@ public interface ISnykToolView {
*/
abstract void setNodeText(BaseTreeNode node, String text);

/**
* Sets the icon of the given node
*
* @param icon
*/
abstract void setNodeIcon(ImageDescriptor icon);

/**
* Adds an issue node to the parent (usually a file node)
*
* @param parent
* @param toBeAdded
*/
abstract void addIssueNode(BaseTreeNode parent, BaseTreeNode toBeAdded);
abstract void addIssueNode(FileTreeNode parent, IssueTreeNode toBeAdded);

/**
* Adds a file node (usually below the product node)
*
* @param parent
* @param toBeAdded
*/
abstract void addFileNode(BaseTreeNode parent, BaseTreeNode toBeAdded);
abstract void addFileNode(ProductTreeNode parent, FileTreeNode toBeAdded);

/**
* Adds an info node (usually below the product node)
*
* @param parent
* @param toBeAdded
*/
abstract void addInfoNode(BaseTreeNode parent, BaseTreeNode toBeAdded);
abstract void addInfoNode(ProductTreeNode parent, InfoTreeNode toBeAdded);

/**
* Returns the product node
*
* @param product the product. ProductConstants#DISPLAY_*
* @param product the product. ProductConstants#DISPLAY_
* @param folderPath TODO*
* @return
*/
abstract BaseTreeNode getProductNode(String product);
abstract ProductTreeNode getProductNode(String product, String folderPath);

/**
* Resets a product node
*/
abstract void resetNode(BaseTreeNode node);

/**
* Remove all info nodes from the product tree node
*
* @param node
*/
abstract void removeInfoNodes(ProductTreeNode node);

/**
* Refreshes the tree display
*/
Expand All @@ -94,5 +98,4 @@ static String getPlural(long count) {
}

abstract TreeViewer getTreeViewer();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.snyk.eclipse.plugin.views.snyktoolview;

public class InfoTreeNode extends BaseTreeNode {

public InfoTreeNode(Object value) {
super(value);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.snyk.eclipse.plugin.views.snyktoolview;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.model.WorkbenchLabelProvider;

import io.snyk.eclipse.plugin.domain.ProductConstants;
import io.snyk.eclipse.plugin.utils.SnykIcons;
import io.snyk.languageserver.protocolextension.messageObjects.scanResults.Issue;

public class IssueTreeNode extends BaseTreeNode {
private Issue issue;

public IssueTreeNode(Issue issue) {
super(issue);
this.setIssue(issue);
}

@Override
public String getText() {
return getIssue().getDisplayTitle();
}

@Override
public String getDetails() {
return getIssue().additionalData().customUIContent();
}

@Override
public ImageDescriptor getImageDescriptor() {
switch(getIssue().severity()) {
case ProductConstants.SEVERITY_CRITICAL:
return SnykIcons.SEVERITY_CRITICAL;
case ProductConstants.SEVERITY_HIGH:
return SnykIcons.SEVERITY_HIGH;
case ProductConstants.SEVERITY_MEDIUM:
return SnykIcons.SEVERITY_MEDIUM;
case ProductConstants.SEVERITY_LOW:
return SnykIcons.SEVERITY_LOW;
default:
return super.getImageDescriptor();
}
}

public Issue getIssue() {
return issue;
}

public void setIssue(Issue issue) {
this.issue = issue;
}


}
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package io.snyk.eclipse.plugin.views.snyktoolview;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreeNode;

import io.snyk.eclipse.plugin.Activator;
import io.snyk.eclipse.plugin.domain.ProductConstants;
import io.snyk.eclipse.plugin.utils.SnykIcons;

public class ProductTreeNode extends BaseTreeNode {

private String product;

public ProductTreeNode(Object value) {
public ProductTreeNode(String value) {
super(value);
this.setProduct(value.toString());
this.setProduct(value);

switch (value.toString()) {
switch (value) {
case ProductConstants.DISPLAYED_OSS:
setImageDescriptor(SnykIcons.OSS);
break;
Expand All @@ -32,6 +40,24 @@ public void setText(String text) {
this.setValue(text);
}

public void removeInfoNodes() {
var childs = getChildren();
if (childs == null || childs.length == 0) {
return;
}

var list = new ArrayList<TreeNode>(Arrays.asList(getChildren()));

var newList = new HashSet<BaseTreeNode>();
for (TreeNode node : list) {
if (!(node instanceof InfoTreeNode) && node instanceof BaseTreeNode) {
newList.add((BaseTreeNode) node);
}
}

setChildren(newList.toArray(new BaseTreeNode[0]));
}

@Override
public void setValue(Object value) {
if (!(value instanceof String))
Expand Down
Loading