Skip to content

Commit

Permalink
fixed for npm/yarn maven gradle and go
Browse files Browse the repository at this point in the history
(pyton is not implemented)
  • Loading branch information
eyalk007 committed Sep 17, 2024
1 parent 0888199 commit 25f7f44
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.idea.maven.execution.SoutMavenConsole;

import javax.swing.tree.TreeNode;
import java.util.*;
Expand Down Expand Up @@ -180,6 +181,7 @@ boolean isShowInspection(PsiElement element) {
return false; // File is not a package descriptor file
}


ScannerBase scanner = getScanner(project, editorFile.getParent().getPath());
if (scanner == null) {
return false; // Scan manager for this project not yet created
Expand Down Expand Up @@ -234,7 +236,7 @@ private List<DependencyNode> getMatchDependencies(DescriptorFileTreeNode file, S
boolean isNodeMatch(DependencyNode node, String componentName) {
String artifactID = node.getComponentIdWithoutPrefix();
ImpactTree impactTree = node.getImpactTree();
return StringUtils.equals(artifactID, componentName) || impactTree.contains(componentName);
return StringUtils.equals(extractArtifactIdWithoutVersion(artifactID), componentName) || impactTree.contains(componentName+":");
}

abstract UpgradeVersion getUpgradeVersion(String componentName, String fixVersion, Collection<String> issues, String descriptorPath);
Expand Down Expand Up @@ -296,4 +298,15 @@ protected static String convertFixVersionStringToMinFixVersion(String fixVersion
fixVersion = StringUtils.strip(fixVersion, "]");
return fixVersion;
}
}


private String extractArtifactIdWithoutVersion(String artifact) {
int versionIndex = artifact.lastIndexOf(':');

if (versionIndex != -1) {
return artifact.substring(0, versionIndex);
} else {
return artifact;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public GradleGroovyInspection() {
* @return the value of the literal
*/
public static String getLiteralValue(GrLiteral literal) {
return Objects.toString((literal).getValue(), "");
String artifact = Objects.toString((literal).getValue(), "");
int versionIndex = artifact.lastIndexOf(':');
if (versionIndex == -1) {
return artifact;
}
return artifact.substring(0, versionIndex);
}

public static boolean isNamedArgumentComponent(PsiElement element) {
Expand Down Expand Up @@ -113,13 +118,12 @@ List<GroovyPsiElement> parseComponentElements(GrArgumentList element) {

@Override
String createComponentName(PsiElement element) {
//element.getText()
if (isNamedArgumentComponent(element)) {
// implementation group: 'j', name: 'k', version: 'l'
return String.join(":",
extractExpression(element, GRADLE_GROUP_KEY),
extractExpression(element, GRADLE_NAME_KEY),
extractExpression(element, GRADLE_VERSION_KEY)
);
extractExpression(element, GRADLE_NAME_KEY));
}
if (element instanceof GrLiteral) {
// implementation 'g:h:i'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ String createComponentName(PsiElement element) {
return extractArgument(argumentList.get(0));
}
if (argumentList.size() >= 3) {
// "commons-collections", "commons-collections", "3.2.2"
// "commons-collections", "commons-collections"
return String.join(":",
extractArgument(argumentList.get(0)),
extractArgument(argumentList.get(1)),
extractArgument(argumentList.get(2)));
extractArgument(argumentList.get(1))
);
}
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
import com.jfrog.ide.idea.scan.ScannerBase;
import com.jfrog.ide.idea.utils.Descriptor;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.maven.dom.model.MavenDomArtifactCoordinates;
import org.jetbrains.idea.maven.project.MavenProject;

import java.util.Collection;

/**
* @author yahavi
*/
Expand Down Expand Up @@ -92,8 +94,7 @@ String createComponentName(PsiElement element) {
}
DomElement domElement = DomManager.getDomManager(element.getProject()).getDomElement((XmlTag) element);
if (domElement instanceof MavenDomArtifactCoordinates) {
String version = ((MavenDomArtifactCoordinates) domElement).getVersion().getStringValue();
return String.join(":", groupId.getValue().getText(), artifactId.getValue().getText(), version);
return String.join(":", groupId.getValue().getText(), artifactId.getValue().getText());
}
return null;
}
Expand All @@ -102,4 +103,6 @@ String createComponentName(PsiElement element) {
UpgradeVersion getUpgradeVersion(String componentName, String fixVersion, Collection<String> issue, String descriptorPath) {
return new MavenUpgradeVersion(componentName, fixVersion, issue);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public void addNavigation(DependencyNode treeNode, PsiElement navigationTargetEl
}
NavigationTarget navigationTarget = new NavigationTarget(navigationTargetElement, document.getLineNumber(navigationTargetElement.getTextOffset()), componentName);
Set<NavigationTarget> navigationTargets = navigationMap.get(treeNode);

if (navigationTargets == null) {
navigationTargets = new HashSet<>(Collections.singletonList(navigationTarget));

navigationMap.put(treeNode, navigationTargets);
return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/jfrog/ide/idea/scan/GradleScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import com.intellij.util.EnvironmentUtil;
import com.jfrog.ide.common.deptree.DepTree;
import com.jfrog.ide.common.gradle.GradleTreeBuilder;
import com.jfrog.ide.common.nodes.DependencyNode;
import com.jfrog.ide.common.nodes.DescriptorFileTreeNode;
import com.jfrog.ide.common.nodes.FileTreeNode;
import com.jfrog.ide.common.scan.ComponentPrefix;
import com.jfrog.ide.common.scan.ScanLogic;
import com.jfrog.ide.idea.inspections.AbstractInspection;
Expand All @@ -34,7 +37,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;

import static com.jfrog.ide.common.log.Utils.logError;
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/jfrog/ide/idea/ui/ComponentPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.jfrog.ide.idea.ui;

public class ComponentPath {

public String fileName;
public String filePath;
}
26 changes: 18 additions & 8 deletions src/main/java/com/jfrog/ide/idea/ui/LocalComponentsTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
Expand Down Expand Up @@ -108,24 +109,28 @@ private void handleContextMenu(ComponentsTree tree, MouseEvent e) {
return;
}
// Event is right-click.

TreePath selectedPath = tree.getPathForRow(tree.getClosestRowForLocation(e.getX(), e.getY()));
if (selectedPath == null) {
return;
}

Object selected = selectedPath.getLastPathComponent();

if (selected instanceof DependencyNode) {
createNodePopupMenu((DependencyNode) selected);

this.createNodePopupMenu("asd" ,(DependencyNode) selected);
} else if (selected instanceof VulnerabilityNode) {
createIgnoreRuleOption((VulnerabilityNode) selected, e);
} else if (selected instanceof ApplicableIssueNode) {
createIgnoreRuleOption(((ApplicableIssueNode) selected).getIssue(), e);
} else {
// No context menu was created.
return;
}
popupMenu.show(tree, e.getX(), e.getY());
}


private void createIgnoreRuleOption(VulnerabilityNode selectedIssue, MouseEvent mouseEvent) {
popupMenu.removeAll();
popupMenu.add(new CreateIgnoreRuleAction(selectedIssue.getIgnoreRuleUrl(), mouseEvent));
Expand All @@ -134,20 +139,19 @@ private void createIgnoreRuleOption(VulnerabilityNode selectedIssue, MouseEvent
toolTip.setEnabled(true);
}

private void createNodePopupMenu(DependencyNode selectedNode) {
private void createNodePopupMenu(String path,DependencyNode selectedNode) {
popupMenu.removeAll();
NavigationService navigationService = NavigationService.getInstance(project);
Set<NavigationTarget> navigationCandidates = navigationService.getNavigation(selectedNode);

addNodeNavigation(navigationCandidates);
addNodeNavigation(path,navigationCandidates);
}

private void addNodeNavigation(Set<NavigationTarget> navigationCandidates) {
private void addNodeNavigation( String parent,Set<NavigationTarget> navigationCandidates) {
if (navigationCandidates == null) {
return;
}
if (navigationCandidates.size() > 1) {
addMultiNavigation(navigationCandidates);
addMultiNavigation(parent,navigationCandidates);
} else {
addSingleNavigation(navigationCandidates.iterator().next());
}
Expand All @@ -157,10 +161,14 @@ private void addSingleNavigation(NavigationTarget navigationTarget) {
popupMenu.add(createNavigationMenuItem(navigationTarget, SHOW_IN_PROJECT_DESCRIPTOR + " (" + navigationTarget.getComponentName() + ")"));
}

private void addMultiNavigation(Set<NavigationTarget> navigationCandidates) {
private void addMultiNavigation(String parentFile,Set<NavigationTarget> navigationCandidates) {
JMenu multiMenu = new JBMenu();
multiMenu.setText(SHOW_IN_PROJECT_DESCRIPTOR);
for (NavigationTarget navigationTarget : navigationCandidates) {
// if(parentFile.equals(navigationTarget.getElement().getContainingFile().getName())){
// continue;
// }

multiMenu.add(createNavigationMenuItem(navigationTarget, navigationTarget.getComponentName()));
}
popupMenu.add(multiMenu);
Expand Down Expand Up @@ -260,3 +268,5 @@ public void setScanErrorEmptyText() {
SwingUtilities.invokeLater(() -> getEmptyText().setText(ERROR_WHILE_SCANNING));
}
}


0 comments on commit 25f7f44

Please sign in to comment.