Skip to content

Commit 25d529a

Browse files
feat: add option to choose a folder as a scan reference (#259)
1 parent 56f808d commit 25d529a

File tree

11 files changed

+215
-144
lines changed

11 files changed

+215
-144
lines changed

CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Snyk Security Changelog
22

3-
## [3.0.1]
3+
## [3.1.0]
44
### Changes
5-
- New Summary Panel for scan information and net new issues filter.
5+
- add option for using a folder as reference instead of a branch in net-new scanning
6+
- add scan summary to custom UI, updating scan statuses live
67

78
## [3.0.0]
89
### Changes

plugin/src/main/java/io/snyk/eclipse/plugin/properties/FolderConfigs.java

+5-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.nio.file.Paths;
55
import java.util.ArrayList;
66
import java.util.Arrays;
7+
import java.util.Collections;
78
import java.util.List;
89

910
import org.eclipse.core.resources.IProject;
@@ -18,7 +19,6 @@
1819
import io.snyk.eclipse.plugin.utils.ResourceUtils;
1920
import io.snyk.eclipse.plugin.utils.SnykLogger;
2021
import io.snyk.languageserver.protocolextension.messageObjects.FolderConfig;
21-
import io.snyk.languageserver.protocolextension.messageObjects.FolderConfigsParam;
2222

2323
public class FolderConfigs {
2424
protected static FolderConfigs instance;
@@ -43,16 +43,10 @@ public void addFolderConfig(FolderConfig folderConfig) {
4343
public List<String> getLocalBranches(Path projectPath) {
4444
return getFolderConfig(projectPath).getLocalBranches();
4545
}
46-
47-
public void setBaseBranch(Path projectPath, String newBaseBranch) {
48-
var folderConfig = getFolderConfig(projectPath);
49-
folderConfig.setBaseBranch(newBaseBranch);
50-
persist(projectPath.normalize(), folderConfig);
51-
}
5246

5347
private void persist(Path path, FolderConfig folderConfig) {
5448
String updatedJson = gson.toJson(folderConfig);
55-
instancePreferences.put(path.toString(), updatedJson);
49+
instancePreferences.put(path.normalize().toString(), updatedJson);
5650
try {
5751
instancePreferences.flush();
5852
} catch (Exception e) {
@@ -70,7 +64,7 @@ public void addAll(List<FolderConfig> folderConfigs) {
7064
}
7165
}
7266

73-
public FolderConfigsParam updateFolderConfigs() {
67+
public List<FolderConfig> getAll() {
7468
List<IProject> openProjects = ResourceUtils.getAccessibleTopLevelProjects();
7569
List<FolderConfig> folderConfigs = new ArrayList<>(openProjects.size());
7670

@@ -87,9 +81,7 @@ public FolderConfigsParam updateFolderConfigs() {
8781
folderConfigs.add(folderConfig);
8882
}
8983

90-
FolderConfigsParam param = new FolderConfigsParam(folderConfigs);
91-
param.setFolderConfigs(folderConfigs);
92-
return param;
84+
return Collections.unmodifiableList(folderConfigs);
9385
}
9486

9587
/**
@@ -102,8 +94,7 @@ public FolderConfig getFolderConfig(Path folderPath) {
10294
String json = instancePreferences.get(path.toString(), null);
10395
if (json == null) {
10496
SnykLogger.logInfo("No valid configuration for path: " + folderPath.toString());
105-
FolderConfig folderConfig = new FolderConfig(path.toString(), null, new ArrayList<>(),
106-
new ArrayList<>());
97+
FolderConfig folderConfig = new FolderConfig(path.toString());
10798
persist(path, folderConfig);
10899
return folderConfig;
109100
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/BaseBranchDialog.java

-71
This file was deleted.

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/ContentRootNode.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
import io.snyk.eclipse.plugin.domain.ProductConstants;
1313
import io.snyk.eclipse.plugin.utils.ResourceUtils;
1414

15-
public class ContentRootNode extends BaseTreeNode {
15+
public final class ContentRootNode extends BaseTreeNode {
1616
private Path path;
1717
private String name;
1818

1919
public ContentRootNode(String name, Path value) {
2020
super(value);
21-
reset();
21+
this.reset();
2222
this.setName(name);
2323
this.setPath(value);
2424
}
@@ -42,10 +42,11 @@ public ProductTreeNode getProductNode(String product) {
4242
return (ProductTreeNode) this.getChildren()[2];
4343
case ProductConstants.DISPLAYED_IAC:
4444
return (ProductTreeNode) this.getChildren()[3];
45+
default:
46+
throw new IllegalArgumentException("unknown product in tree");
4547
}
46-
return null;
4748
}
48-
49+
4950
@Override
5051
public void reset() {
5152
var ossRootNode = new ProductTreeNode(DISPLAYED_OSS);
@@ -74,7 +75,7 @@ public Path getPath() {
7475
return path;
7576
}
7677

77-
public void setPath(Path path) {
78+
public final void setPath(Path path) {
7879
if (path != null) {
7980
this.path = path.normalize();
8081
}
@@ -84,7 +85,7 @@ public String getName() {
8485
return name;
8586
}
8687

87-
public void setName(String name) {
88+
public final void setName(String name) {
8889
this.name = name;
8990
}
9091
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package io.snyk.eclipse.plugin.views.snyktoolview;
2+
3+
import java.nio.file.Path;
4+
import java.util.concurrent.CompletableFuture;
5+
6+
import org.eclipse.jface.dialogs.TitleAreaDialog;
7+
import org.eclipse.swt.SWT;
8+
import org.eclipse.swt.events.ModifyEvent;
9+
import org.eclipse.swt.events.ModifyListener;
10+
import org.eclipse.swt.layout.GridData;
11+
import org.eclipse.swt.layout.GridLayout;
12+
import org.eclipse.swt.widgets.Button;
13+
import org.eclipse.swt.widgets.Combo;
14+
import org.eclipse.swt.widgets.Composite;
15+
import org.eclipse.swt.widgets.Control;
16+
import org.eclipse.swt.widgets.DirectoryDialog;
17+
import org.eclipse.swt.widgets.Shell;
18+
import org.eclipse.swt.widgets.Text;
19+
20+
import io.snyk.eclipse.plugin.properties.FolderConfigs;
21+
import io.snyk.eclipse.plugin.wizards.SWTWidgetHelper;
22+
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
23+
import io.snyk.languageserver.protocolextension.messageObjects.FolderConfig;
24+
25+
public class ReferenceChooserDialog extends TitleAreaDialog {
26+
private Path projectPath;
27+
private FolderConfig folderConfig;
28+
private Combo branches;
29+
private Text folderText;
30+
31+
public ReferenceChooserDialog(Shell parentShell, Path projectPath) {
32+
super(parentShell);
33+
this.projectPath = projectPath;
34+
this.folderConfig = FolderConfigs.getInstance().getFolderConfig(projectPath);
35+
}
36+
37+
@Override
38+
protected boolean isResizable() {
39+
return true;
40+
}
41+
42+
@Override
43+
protected Control createDialogArea(Composite parent) {
44+
Composite panel = new Composite(parent, SWT.NONE);
45+
panel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
46+
47+
var gridLayout = new GridLayout();
48+
gridLayout.numColumns = 1;
49+
gridLayout.marginBottom = 5;
50+
gridLayout.marginTop = 5;
51+
gridLayout.marginLeft = 5;
52+
gridLayout.marginRight = 5;
53+
panel.setLayout(gridLayout);
54+
var group = SWTWidgetHelper.createGroup(panel, "Reference branch for " + projectPath.getFileName(), 1,
55+
GridData.FILL_HORIZONTAL);
56+
final var localBranches = folderConfig.getLocalBranches().toArray(new String[0]);
57+
branches = new Combo(group, SWT.DROP_DOWN);
58+
branches.setItems(localBranches);
59+
branches.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
60+
branches.setText(folderConfig.getBaseBranch());
61+
branches.addModifyListener(new ModifyListener() {
62+
@Override
63+
public void modifyText(ModifyEvent e) {
64+
validate();
65+
}
66+
});
67+
68+
group = SWTWidgetHelper.createGroup(panel, "Reference folder for " + projectPath.getFileName(), 1,
69+
GridData.FILL_HORIZONTAL);
70+
folderText = new Text(group, SWT.BORDER);
71+
folderText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
72+
folderText.setText(folderConfig.getReferenceFolderPath());
73+
Button browseButton = new Button(group, SWT.PUSH);
74+
browseButton.setText("Browse...");
75+
76+
browseButton.addListener(SWT.Selection, event -> {
77+
DirectoryDialog dlg = new DirectoryDialog(getShell());
78+
dlg.setMessage("Select a folder");
79+
dlg.setFilterPath(folderText.getText());
80+
String selectedDir = dlg.open();
81+
validate();
82+
if (selectedDir != null) {
83+
setErrorMessage(null);
84+
folderText.setText(selectedDir);
85+
}
86+
});
87+
88+
this.setTitle("Please specify the reference for the net-new issues scan");
89+
return panel;
90+
}
91+
92+
@Override
93+
public boolean close() {
94+
if (getErrorMessage() != null)
95+
return false;
96+
97+
final var referenceBranch = branches.getText();
98+
final var referenceFolder = folderText.getText();
99+
100+
folderConfig.setBaseBranch(referenceBranch);
101+
folderConfig.setReferenceFolderPath(referenceFolder);
102+
FolderConfigs.getInstance().addFolderConfig(folderConfig);
103+
CompletableFuture.runAsync(() -> {
104+
final var lc = SnykExtendedLanguageClient.getInstance();
105+
lc.updateConfiguration();
106+
lc.triggerScan(projectPath);
107+
});
108+
109+
return super.close();
110+
}
111+
112+
@Override
113+
protected void okPressed() {
114+
validate();
115+
super.okPressed();
116+
}
117+
118+
private void validate() {
119+
final var referenceBranch = branches.getText();
120+
final var referenceFolder = folderText.getText();
121+
122+
if (referenceBranch.isBlank() && referenceFolder.isBlank()) {
123+
setErrorMessage("Either a reference branch or a reference folder need to be set.");
124+
} else {
125+
setErrorMessage(null);
126+
}
127+
}
128+
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/SnykToolView.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public SnykToolView() {
7171
private BrowserHandler browserHandler;
7272
private Browser summaryBrowser;
7373
private SummaryBrowserHandler summaryBrowserHandler;
74-
private FolderConfigs folderConfigs = FolderConfigs.getInstance();
7574
private TreeNode selectedNode;
7675

7776
@Override
@@ -127,7 +126,6 @@ public void selectionChanged(SelectionChangedEvent event) {
127126
if (selection.isEmpty())
128127
return;
129128
selectedNode = (TreeNode) selection.getFirstElement();
130-
browserHandler.updateBrowserContent(selectedNode);
131129
if (selectedNode instanceof IssueTreeNode) {
132130
IssueTreeNode issueTreeNode = (IssueTreeNode) selectedNode;
133131
FileTreeNode fileNode = (FileTreeNode) issueTreeNode.getParent();
@@ -137,11 +135,12 @@ public void selectionChanged(SelectionChangedEvent event) {
137135
boolean deltaEnabled = Preferences.isDeltaEnabled();
138136
if (selectedNode instanceof ContentRootNode && deltaEnabled) {
139137
ContentRootNode contentNode = (ContentRootNode) selectedNode;
140-
String[] localBranches = folderConfigs.getLocalBranches(contentNode.getPath())
141-
.toArray(new String[0]);
142-
143-
new BaseBranchDialog().open(Display.getDefault(), contentNode.getPath(), localBranches);
138+
new ReferenceChooserDialog(Display.getDefault().getActiveShell(), contentNode.getPath()).open();
139+
} else {
140+
// update the browser content for everything but content roots
141+
browserHandler.updateBrowserContent(selectedNode);
144142
}
143+
145144
});
146145
}
147146
});
@@ -420,10 +419,16 @@ public void enableDelta() {
420419
for (BaseTreeNode node : children) {
421420
if (node instanceof ContentRootNode) {
422421
ContentRootNode contentNode = (ContentRootNode) node;
423-
String baseBranch = folderConfigs.getBaseBranch(contentNode.getPath());
422+
var folderConfig = FolderConfigs.getInstance().getFolderConfig(contentNode.getPath());
423+
String referenceFolder = folderConfig.getReferenceFolderPath();
424+
String baseBranch = folderConfig.getBaseBranch();
425+
var reference = referenceFolder;
426+
if (reference.isBlank()) {
427+
reference = baseBranch;
428+
}
424429

425-
contentNode.setName(String.format("%s - Click here choose base branch [ current: %s ]",
426-
contentNode.getName(), baseBranch));
430+
contentNode.setName(String.format("%s - Click here choose reference [ current: %s ]",
431+
contentNode.getName(), reference));
427432
}
428433
}
429434
}
@@ -472,4 +477,4 @@ protected void outputCommandResult(Object result) {
472477
}
473478
}
474479

475-
}
480+
}

0 commit comments

Comments
 (0)