Skip to content

Commit 2a37205

Browse files
committed
feat: delta scans trigger on dialog close. Enable delta scans on startup if state is true.
Put a current branch in the Dialog combobox. Validate that the text from the combobox is equal to one of the existing branches. Update root node with choosen branch. Start a scan when clicking ok in the dialog. Set nodes to delta nodes when Eclipse starts.
1 parent 7637843 commit 2a37205

5 files changed

Lines changed: 154 additions & 91 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.snyk.eclipse.plugin.properties.preferences;
2+
3+
import java.util.List;
4+
5+
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
6+
import org.eclipse.core.runtime.preferences.InstanceScope;
7+
8+
import com.google.gson.Gson;
9+
10+
import io.snyk.eclipse.plugin.utils.SnykLogger;
11+
import io.snyk.languageserver.protocolextension.messageObjects.FolderConfig;
12+
13+
public class EclipsePreferenceState {
14+
private static EclipsePreferenceState instance;
15+
private static final IEclipsePreferences preferenceState = InstanceScope.INSTANCE.getNode("io.snyk.eclipse.plugin");
16+
private static final Gson gson = new Gson();
17+
18+
private EclipsePreferenceState() {
19+
}
20+
21+
public static synchronized EclipsePreferenceState getInstance() {
22+
if (instance == null) {
23+
instance = new EclipsePreferenceState();
24+
}
25+
return instance;
26+
}
27+
28+
public void addFolderConfig(FolderConfig folderConfig) {
29+
preferenceState.put(folderConfig.getFolderPath(), gson.toJson(folderConfig));
30+
try {
31+
preferenceState.flush();
32+
} catch (Exception e) {
33+
SnykLogger.logError(e);
34+
}
35+
}
36+
37+
public List<String> getLocalBranches(String folderPath) {
38+
String json = preferenceState.get(folderPath, null);
39+
if (json != null) {
40+
FolderConfig folderConfig = gson.fromJson(json, FolderConfig.class);
41+
return folderConfig.getLocalBranches();
42+
}
43+
return List.of();
44+
}
45+
46+
public void setBaseBranch(String folderPath, String newBaseBranch) {
47+
String json = preferenceState.get(folderPath, null);
48+
if (json == null) {
49+
SnykLogger.logInfo("No valid configuration for project: " + folderPath);
50+
return;
51+
}
52+
FolderConfig folderConfig = gson.fromJson(json, FolderConfig.class);
53+
folderConfig.setBaseBranch(newBaseBranch);
54+
String updatedJson = gson.toJson(folderConfig);
55+
preferenceState.put(folderPath, updatedJson);
56+
try {
57+
preferenceState.flush();
58+
} catch (Exception e) {
59+
SnykLogger.logError(e);
60+
}
61+
}
62+
63+
public String getBaseBranch(String folderPath) {
64+
String json = preferenceState.get(folderPath, null);
65+
if (json == null)
66+
return null;
67+
FolderConfig folderConfig = gson.fromJson(json, FolderConfig.class);
68+
return folderConfig.getBaseBranch();
69+
}
70+
}

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

Lines changed: 75 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package io.snyk.eclipse.plugin.views.snyktoolview;
22

3-
import java.nio.file.Path;
43
import java.nio.file.Paths;
4+
import java.util.Arrays;
55
import java.util.HashMap;
6-
import java.util.List;
76
import java.util.Map;
87

9-
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
10-
import org.eclipse.core.runtime.preferences.InstanceScope;
118
import org.eclipse.jface.action.IMenuManager;
129
import org.eclipse.jface.action.MenuManager;
1310
import org.eclipse.jface.viewers.ISelectionChangedListener;
@@ -39,13 +36,13 @@
3936
import org.eclipse.ui.menus.CommandContributionItemParameter;
4037
import org.eclipse.ui.part.ViewPart;
4138

42-
import com.google.gson.Gson;
43-
39+
import io.snyk.eclipse.plugin.properties.preferences.EclipsePreferenceState;
4440
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
4541
import io.snyk.eclipse.plugin.utils.ResourceUtils;
42+
import io.snyk.eclipse.plugin.utils.SnykLogger;
4643
import io.snyk.eclipse.plugin.views.snyktoolview.providers.TreeContentProvider;
4744
import io.snyk.eclipse.plugin.views.snyktoolview.providers.TreeLabelProvider;
48-
import io.snyk.languageserver.protocolextension.messageObjects.FolderConfig;
45+
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
4946

5047
/**
5148
* TODO This view will replace the old SnykView. Move the snyktoolview classes
@@ -65,9 +62,7 @@ public class SnykToolView extends ViewPart implements ISnykToolView {
6562
private Browser browser;
6663
private BrowserHandler browserHandler;
6764
private Map<TreeItem, Listener> itemListeners = new HashMap<>();
68-
private final Gson gson = new Gson();
69-
70-
private final static Shell SHELL = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
65+
private EclipsePreferenceState preferenceState = EclipsePreferenceState.getInstance();
7166

7267
@Override
7368
public void createPartControl(Composite parent) {
@@ -120,6 +115,9 @@ public void selectionChanged(SelectionChangedEvent event) {
120115
});
121116
}
122117
});
118+
119+
if (Preferences.getInstance().getBooleanPref(Preferences.FILTER_DELTA_NEW_ISSUES))
120+
this.enableDelta();
123121
}
124122

125123
private void registerTreeContextMeny(Composite parent) {
@@ -276,32 +274,43 @@ public void toggleIgnoresButtons() {
276274

277275
}
278276

277+
/*
278+
* Sets up for doing a Net New Issues scan.
279+
*/
279280
public void enableDelta() {
280-
TreeItem[] rootItems = getTreeViewer().getTree().getItems();
281-
for (TreeItem item : rootItems) {
282-
ContentRootNode node = (ContentRootNode) item.getData();
283-
String project = node.getText().toString();
284-
String projectPath = node.getPath().toString();
285-
String baseBranch = getBaseBranch(projectPath.toString());
286-
String[] localBranches = getLocalBranches(projectPath).toArray(String[]::new);
287-
288-
item.setText(String.format("Click to choose base branch for: %s [ current: %s ]", project, baseBranch));
289-
290-
Listener selectionListener = new Listener() {
291-
@Override
292-
public void handleEvent(Event event) {
293-
if (event.item == item) {
294-
baseBranchDialog(event.display, projectPath, localBranches);
295-
}
296-
}
297-
};
281+
clearDeltaNodeListeners();
298282

299-
// Store the listener in the map
300-
itemListeners.put(item, selectionListener);
283+
Display.getDefault().asyncExec(() -> {
284+
if (this.treeViewer != null && !this.treeViewer.getTree().isDisposed()) {
301285

302-
// Add the listener to the item's parent
303-
item.getParent().addListener(SWT.Selection, selectionListener);
304-
}
286+
TreeItem[] rootItems = getTreeViewer().getTree().getItems();
287+
for (TreeItem item : rootItems) {
288+
ContentRootNode node = (ContentRootNode) item.getData();
289+
String project = node.getText().toString();
290+
String projectPath = node.getPath().toString();
291+
String baseBranch = preferenceState.getBaseBranch(projectPath);
292+
String[] localBranches = preferenceState.getLocalBranches(projectPath).toArray(String[]::new);
293+
294+
item.setText(
295+
String.format("Click to choose base branch for: %s [ current: %s ]", project, baseBranch));
296+
297+
Listener selectionListener = new Listener() {
298+
@Override
299+
public void handleEvent(Event event) {
300+
if (event.item == item) {
301+
baseBranchDialog(event.display, projectPath, localBranches);
302+
}
303+
}
304+
};
305+
306+
// Store the listener in the map
307+
itemListeners.put(item, selectionListener);
308+
309+
// Add the listener to the item's parent
310+
item.getParent().addListener(SWT.Selection, selectionListener);
311+
}
312+
}
313+
});
305314
}
306315

307316
private void baseBranchDialog(Display display, String projectPath, String[] localBranches) {
@@ -315,6 +324,7 @@ private void baseBranchDialog(Display display, String projectPath, String[] loca
315324
Combo dropdown = new Combo(shell, SWT.DROP_DOWN);
316325
dropdown.setItems(localBranches);
317326
dropdown.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
327+
dropdown.setText(preferenceState.getBaseBranch(projectPath));
318328

319329
Button okButton = new Button(shell, SWT.PUSH);
320330
okButton.setText("OK");
@@ -323,7 +333,15 @@ private void baseBranchDialog(Display display, String projectPath, String[] loca
323333
@Override
324334
public void widgetSelected(SelectionEvent e) {
325335
// Handle OK button press
326-
shell.close();
336+
String selectedBranch = dropdown.getText();
337+
if (Arrays.asList(localBranches).contains(selectedBranch)) {
338+
preferenceState.setBaseBranch(projectPath, selectedBranch);
339+
shell.close();
340+
SnykExtendedLanguageClient.getInstance().triggerScan(null);
341+
} else {
342+
SnykLogger.logInfo("Branch is not a valid local branch for repository: " + projectPath);
343+
}
344+
327345
}
328346
});
329347

@@ -337,55 +355,36 @@ public void widgetSelected(SelectionEvent e) {
337355
}
338356
}
339357

340-
public List<String> getLocalBranches(String folderPath) {
341-
342-
IEclipsePreferences state = InstanceScope.INSTANCE.getNode("io.snyk.eclipse.plugin");
343-
// Retrieve the JSON string from the preferences state
344-
String json = state.get(folderPath, null);
345-
346-
if (json != null) {
347-
// Deserialize the JSON string back to a FolderConfig object
348-
FolderConfig folderConfig = gson.fromJson(json, FolderConfig.class);
349-
// Return the list of local branches
350-
return folderConfig.getLocalBranches();
351-
}
352-
353-
return List.of(); // Return an empty list if no data is found
354-
}
355-
356-
public String getBaseBranch(String folderPath) {
357-
358-
IEclipsePreferences state = InstanceScope.INSTANCE.getNode("io.snyk.eclipse.plugin");
359-
// Retrieve the JSON string from the preferences state
360-
String json = state.get(folderPath, null);
361-
362-
if (json == null)
363-
return null;
364-
365-
// Deserialize the JSON string back to a FolderConfig object
366-
FolderConfig folderConfig = gson.fromJson(json, FolderConfig.class);
367-
// Return the list of local branches
368-
return folderConfig.getBaseBranch();
358+
/*
359+
* Disables Net New Issues scan, and starts a regular scan.
360+
*/
361+
public void disableDelta() {
362+
clearDeltaNodeListeners();
369363

364+
SnykExtendedLanguageClient.getInstance().triggerScan(null);
370365
}
371366

372-
public void disableDelta() {
373-
for (Map.Entry<TreeItem, Listener> entry : itemListeners.entrySet()) {
374-
TreeItem item = entry.getKey();
375-
Listener listener = entry.getValue();
367+
private void clearDeltaNodeListeners() {
368+
Display.getDefault().asyncExec(() -> {
369+
if (this.treeViewer != null && !this.treeViewer.getTree().isDisposed()) {
370+
for (Map.Entry<TreeItem, Listener> entry : itemListeners.entrySet()) {
371+
TreeItem item = entry.getKey();
372+
Listener listener = entry.getValue();
376373

377-
ContentRootNode node = (ContentRootNode) item.getData();
378-
String project = node.getText().toString();
374+
ContentRootNode node = (ContentRootNode) item.getData();
375+
String project = node.getText().toString();
379376

380-
// Revert text to original
381-
item.setText(project);
377+
// Revert text to original
378+
item.setText(project);
382379

383-
// Remove listener from the item's parent
384-
item.getParent().removeListener(SWT.Selection, listener);
385-
}
380+
// Remove listener from the item's parent
381+
item.getParent().removeListener(SWT.Selection, listener);
382+
}
386383

387-
// Clear the map after removing all listeners
388-
itemListeners.clear();
384+
// Clear the map after removing all listeners
385+
itemListeners.clear();
386+
}
387+
});
389388
}
390389

391390
// Helper method to add a command if it's not already present

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ private static void setupFilters() {
5454
.applyFilter();
5555
new OssFixableFilter(TreeFilterManager.getInstance(), Preferences.getInstance(),
5656
Preferences.FILTER_OSS_FIXABLE_ISSUES).applyFilter();
57-
5857
}
5958

6059
private TreeFilterManager() {

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/handlers/FilterDeltaNewIssuesHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public FilterDeltaNewIssuesHandler() {
2121

2222
@Override
2323
public Object execute(ExecutionEvent event) throws ExecutionException {
24-
super.execute(event);
24+
super.execute(event);
2525

2626
boolean booleanPref = Preferences.getInstance().getBooleanPref(this.preferenceKey);
2727

plugin/src/main/java/io/snyk/languageserver/protocolextension/SnykExtendedLanguageClient.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@
6666

6767
import com.fasterxml.jackson.databind.DeserializationFeature;
6868
import com.fasterxml.jackson.databind.ObjectMapper;
69-
import com.google.gson.Gson;
7069

7170
import io.snyk.eclipse.plugin.SnykStartup;
7271
import io.snyk.eclipse.plugin.analytics.AbstractTask;
7372
import io.snyk.eclipse.plugin.analytics.AnalyticsEventTask;
7473
import io.snyk.eclipse.plugin.analytics.TaskProcessor;
74+
import io.snyk.eclipse.plugin.properties.preferences.EclipsePreferenceState;
7575
import io.snyk.eclipse.plugin.properties.preferences.Preferences;
7676
import io.snyk.eclipse.plugin.utils.SnykLogger;
7777
import io.snyk.eclipse.plugin.views.SnykView;
@@ -112,9 +112,8 @@ public class SnykExtendedLanguageClient extends LanguageClientImpl {
112112
// this field is for testing only
113113
private LanguageServer ls;
114114
private LsConfigurationUpdater configurationUpdater = new LsConfigurationUpdater();
115-
private final Gson gson = new Gson();
115+
private EclipsePreferenceState preferenceState = EclipsePreferenceState.getInstance();
116116

117-
private final IEclipsePreferences state = InstanceScope.INSTANCE.getNode("io.snyk.eclipse.plugin");
118117
private static SnykExtendedLanguageClient instance = null;
119118

120119
public SnykExtendedLanguageClient() {
@@ -183,6 +182,11 @@ public void triggerScan(IWorkbenchWindow window) {
183182
runSnykWizard();
184183
} else {
185184
openToolView();
185+
this.toolView.resetNode(this.toolView.getRoot());
186+
187+
if (Preferences.getInstance().getBooleanPref(Preferences.FILTER_DELTA_NEW_ISSUES))
188+
this.toolView.enableDelta();
189+
186190
try {
187191
this.toolView.resetNode(this.toolView.getRoot());
188192
if (window == null) {
@@ -395,16 +399,7 @@ public void folderConfig(FolderConfigsParam folderConfigParam) {
395399

396400
public void addAll(List<FolderConfig> folderConfigs) {
397401
for (FolderConfig folderConfig : folderConfigs) {
398-
addFolderConfig(folderConfig);
399-
}
400-
}
401-
402-
public void addFolderConfig(FolderConfig folderConfig) {
403-
state.put(folderConfig.getFolderPath(), gson.toJson(folderConfig));
404-
try {
405-
state.flush();
406-
} catch (Exception e) {
407-
SnykLogger.logError(e);
402+
preferenceState.addFolderConfig(folderConfig);
408403
}
409404
}
410405

0 commit comments

Comments
 (0)