Skip to content

Commit

Permalink
Merge pull request #253 from snyk/feat/IDE-892_summary-panel
Browse files Browse the repository at this point in the history
feat: summary panel [IDE-892]
  • Loading branch information
acke authored Jan 30, 2025
2 parents a963f9b + 2147d0b commit 440de46
Show file tree
Hide file tree
Showing 18 changed files with 405 additions and 204 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Snyk Security Changelog

## [3.0.1]
### Changes
- New Summary Panel for scan information and net new issues filter.

## [3.0.0]
### Changes
- process api URL from hasAuthenticated message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
Expand All @@ -17,7 +18,7 @@
*/
public class TaskProcessor {
// left = taskToExecute, right = callback function
private final ConcurrentLinkedQueue<Pair<Consumer<SnykExtendedLanguageClient>, Consumer<Void>>> taskQueue = new ConcurrentLinkedQueue<>();
private final Queue<Pair<Consumer<SnykExtendedLanguageClient>, Consumer<Void>>> taskQueue = new ConcurrentLinkedQueue<>();

private TaskProcessor() {
CompletableFuture.runAsync(() -> {
Expand All @@ -39,9 +40,11 @@ public static TaskProcessor getInstance() {
}

private void start() {
final List<Pair<Consumer<SnykExtendedLanguageClient>, Consumer<Void>>> copyForSending = new ArrayList<>();

while (true) {
String authToken = Preferences.getInstance().getAuthToken();
var lc = SnykExtendedLanguageClient.getInstance();
SnykExtendedLanguageClient lc = SnykExtendedLanguageClient.getInstance();
if (taskQueue.isEmpty() || authToken == null || authToken.isBlank() || lc == null) {
try {
Thread.sleep(1000);
Expand All @@ -50,8 +53,9 @@ private void start() {
}
continue;
}
List<Pair<Consumer<SnykExtendedLanguageClient>, Consumer<Void>>> copyForSending = new ArrayList<>(
taskQueue);

copyForSending.clear(); // Clear the list before reuse
copyForSending.addAll(taskQueue); // Add all elements from taskQueue

for (Pair<Consumer<SnykExtendedLanguageClient>, Consumer<Void>> event : copyForSending) {
try {
Expand Down
278 changes: 144 additions & 134 deletions plugin/src/main/java/io/snyk/eclipse/plugin/html/BaseHtmlProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,33 @@ public class BaseHtmlProvider {
private final Random random = new Random();
private final Map<String, String> colorCache = new HashMap<>();
private String nonce = "";
public String getCss() {
return "";
}

public String getJs() {
return "";
}

public String getInitScript() {
return "";
}

public String getNonce() {
if(!nonce.isEmpty()) {
return nonce;
}
String allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder nonceBuilder = new StringBuilder(32);
for (int i = 0; i < 32; i++) {
nonceBuilder.append(allowedChars.charAt(random.nextInt(allowedChars.length())));
}
nonce = nonceBuilder.toString();
return nonce;
}
public String getNoDescriptionHtml() {

public String getCss() {
return "";
}

public String getJs() {
return "";
}

public String getInitScript() {
return "";
}

public String getNonce() {
if (!nonce.isEmpty()) {
return nonce;
}
String allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder nonceBuilder = new StringBuilder(32);
for (int i = 0; i < 32; i++) {
nonceBuilder.append(allowedChars.charAt(random.nextInt(allowedChars.length())));
}
nonce = nonceBuilder.toString();
return nonce;
}

public String getNoDescriptionHtml() {
String snykWarningText = Platform.getResourceString(Platform.getBundle("io.snyk.eclipse.plugin"),
"snyk.panel.auth.trust.warning.text");

Expand Down Expand Up @@ -75,7 +75,7 @@ public String getNoDescriptionHtml() {
a {
color: var(--link-color)
}
div {
padding: 20px
}
Expand All @@ -94,116 +94,126 @@ public String getNoDescriptionHtml() {
return html;
}

public String replaceCssVariables(String html) {
// Build the CSS with the nonce
String nonce = getNonce();
String css = "<style nonce=\"" + nonce + "\">" + getCss() + "</style>";
String htmlStyled = html.replace("${ideStyle}", css);
htmlStyled = htmlStyled.replace("<style nonce=\"ideNonce\" data-ide-style></style>", css);
htmlStyled = htmlStyled.replace("var(--default-font)",
" ui-sans-serif, \"SF Pro Text\", \"Segoe UI\", \"Ubuntu\", Tahoma, Geneva, Verdana, sans-serif;");

// Replace CSS variables with actual color values
htmlStyled = htmlStyled.replace("var(--text-color)",
getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR", "#000000"));

htmlStyled = htmlStyled.replace("var(--ide-background-color)",
getColorAsHex("org.eclipse.ui.workbench.ACTIVE_NOFOCUS_TAB_BG_START", "#FFFFFF"));
htmlStyled = htmlStyled.replace("var(--background-color)",
getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_BG_END", "#FFFFFF"));
htmlStyled = htmlStyled.replace("var(--code-background-color)",
getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
htmlStyled = htmlStyled.replace("var(--button-color)",
getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
htmlStyled = htmlStyled.replace("var(--circle-color)",
getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));

htmlStyled = htmlStyled.replace("var(--border-color)",
getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));
htmlStyled = htmlStyled.replace("var(--link-color)", getColorAsHex("ACTIVE_HYPERLINK_COLOR", "#0000FF"));
htmlStyled = htmlStyled.replace("var(--horizontal-border-color)",
getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));

htmlStyled = htmlStyled.replace("${headerEnd}", "");
htmlStyled = htmlStyled.replace("${nonce}", nonce);
htmlStyled = htmlStyled.replace("ideNonce", nonce);
htmlStyled = htmlStyled.replace("${ideScript}", "");

return htmlStyled;
}

public String replaceCssVariables(String html) {
// Build the CSS with the nonce
String nonce = getNonce();
String css = "<style nonce=\"" + nonce + "\">" + getCss() + "</style>";
html = html.replace("${ideStyle}", css);
html = html.replace("<style nonce=\"ideNonce\" data-ide-style></style>", css);
html = html.replace("var(--default-font)", " ui-sans-serif, \"SF Pro Text\", \"Segoe UI\", \"Ubuntu\", Tahoma, Geneva, Verdana, sans-serif;");


// Replace CSS variables with actual color values
html = html.replace("var(--text-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR", "#000000"));
html = html.replace("var(--background-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_BG_END", "#FFFFFF"));
html = html.replace("var(--code-background-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
html = html.replace("var(--button-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));
html = html.replace("var(--circle-color)", getColorAsHex("org.eclipse.ui.workbench.INACTIVE_TAB_BG_START", "#F0F0F0"));

html = html.replace("var(--border-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));
html = html.replace("var(--link-color)", getColorAsHex("ACTIVE_HYPERLINK_COLOR", "#0000FF"));
html = html.replace("var(--horizontal-border-color)", getColorAsHex("org.eclipse.ui.workbench.ACTIVE_TAB_OUTER_KEYLINE_COLOR", "#CCCCCC"));

html = html.replace("${headerEnd}", "");
html = html.replace("${nonce}", nonce);
html = html.replace("ideNonce", nonce);
html = html.replace("${ideScript}", "");

return html;
}

public String getColorAsHex(String colorKey, String defaultColor) {
if(Preferences.getInstance().isTest()) {
public String getColorAsHex(String colorKey, String defaultColor) {
if (Preferences.getInstance().isTest()) {
return "";
}
return colorCache.computeIfAbsent(colorKey, key -> {
ColorRegistry colorRegistry = getColorRegistry();
Color color = colorRegistry.get(colorKey);
if (color == null) {
return defaultColor;
} else {
RGB rgb = color.getRGB();
return String.format("#%02x%02x%02x", rgb.red, rgb.green, rgb.blue);
}
});
}

public Boolean isDarkTheme() {
var darkColor = getColorAsHex("org.eclipse.ui.workbench.DARK_BACKGROUND", "");
return darkColor.equals("true");
}

private ColorRegistry colorRegistry;
private ColorRegistry getColorRegistry() {
if(colorRegistry != null) {
return colorRegistry;
}
ITheme currentTheme = getCurrentTheme();
colorRegistry = currentTheme.getColorRegistry();
return colorRegistry;
}


private ITheme currentTheme;
public ITheme getCurrentTheme() {
if(currentTheme != null) {
return currentTheme;
}
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
currentTheme = themeManager.getCurrentTheme();
return currentTheme;
}

public String getErrorHtml(String errorMessage, String path) {
return colorCache.computeIfAbsent(colorKey, key -> {
ColorRegistry colorRegistry = getColorRegistry();
Color color = colorRegistry.get(colorKey);
if (color == null) {
return defaultColor;
} else {
RGB rgb = color.getRGB();
return String.format("#%02x%02x%02x", rgb.red, rgb.green, rgb.blue);
}
});
}

public Boolean isDarkTheme() {
var darkColor = getColorAsHex("org.eclipse.ui.workbench.DARK_BACKGROUND", "");
return Boolean.valueOf(darkColor);
}

private ColorRegistry colorRegistry;

private ColorRegistry getColorRegistry() {
if (colorRegistry != null) {
return colorRegistry;
}
ITheme currentTheme = getCurrentTheme();
colorRegistry = currentTheme.getColorRegistry();
return colorRegistry;
}

private ITheme currentTheme;

public ITheme getCurrentTheme() {
if (currentTheme != null) {
return currentTheme;
}
IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
currentTheme = themeManager.getCurrentTheme();
return currentTheme;
}

public String getErrorHtml(String errorMessage, String path) {
var html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snyk for Eclipse</title>
<style>
body {
font-family: var(--default-font);
background-color: var(--background-color);
color: var(--text-color);
}
.container {
display: flex;
align-items: center;
}
.logo {
margin-right: 20px;
}
</style>
</head>
<body>
<div class="container">
<div>
<p><strong>An error occurred:</strong></p>
<p>
<table>
<tr><td width="150" >Error message:</td><td>%s</td></tr>
<tr></tr>
<tr><td>Path:</td><td>%s</td></tr>
</table>
</p>
</div>
</div>
</body>
</html>
""".formatted(errorMessage, path);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snyk for Eclipse</title>
<style>
body {
font-family: var(--default-font);
background-color: var(--background-color);
color: var(--text-color);
}
.container {
display: flex;
align-items: center;
}
.logo {
margin-right: 20px;
}
</style>
</head>
<body>
<div class="container">
<div>
<p><strong>An error occurred:</strong></p>
<p>
<table>
<tr><td width="150" >Error message:</td><td>%s</td></tr>
<tr></tr>
<tr><td>Path:</td><td>%s</td></tr>
</table>
</p>
</div>
</div>
</body>
</html>
""".formatted(errorMessage, path);
return replaceCssVariables(html);
}
}
Loading

0 comments on commit 440de46

Please sign in to comment.