Skip to content

Commit 6e74265

Browse files
author
Asaf Agami
authored
Merge pull request #77 from snyk/fix/head-451-teamcity-report-missing
fix: report being downloaded instead of rendered on screen [HEAD-451]
2 parents 3db52c0 + 2de64d2 commit 6e74265

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

teamcity-snyk-security-plugin-server/src/main/java/io/snyk/plugins/teamcity/server/SnykSecurityReportTab.java

+34-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package io.snyk.plugins.teamcity.server;
22

33
import javax.servlet.http.HttpServletRequest;
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.InputStreamReader;
8+
import java.nio.charset.StandardCharsets;
49
import java.util.Map;
510

611
import io.snyk.plugins.teamcity.common.SnykSecurityRunnerConstants;
@@ -13,6 +18,7 @@
1318
import jetbrains.buildServer.web.openapi.PluginDescriptor;
1419
import jetbrains.buildServer.web.openapi.PositionConstraint;
1520
import jetbrains.buildServer.web.openapi.ViewLogTab;
21+
import org.apache.log4j.Logger;
1622
import org.jetbrains.annotations.NotNull;
1723

1824
import static io.snyk.plugins.teamcity.common.SnykSecurityRunnerConstants.SNYK_ARTIFACTS_DIR;
@@ -25,16 +31,19 @@ public class SnykSecurityReportTab extends ViewLogTab {
2531
private static final String TAB_TITLE = "Snyk Security Report";
2632
private static final String TAB_CODE = "snykSecurityReport";
2733

34+
private static final Logger LOG = Logger.getLogger(SnykSecurityReportTab.class);
35+
2836
public SnykSecurityReportTab(@NotNull PagePlaces pagePlaces, @NotNull SBuildServer server, @NotNull PluginDescriptor pluginDescriptor) {
2937
super(TAB_TITLE, TAB_CODE, pagePlaces, server);
3038

31-
setIncludeUrl("/artifactsViewer.jsp");
39+
String path = pluginDescriptor.getPluginResourcesPath("tab/snykReport.jsp");
40+
setIncludeUrl(path);
3241
setPosition(PositionConstraint.after("artifacts"));
3342
}
3443

3544
@Override
3645
protected void fillModel(@NotNull Map<String, Object> map, @NotNull HttpServletRequest httpServletRequest, @NotNull SBuild build) {
37-
map.put("startPage", getSnykHtmlReport(build));
46+
map.put("snykHtmlReportContent", readSnykHtmlReport(build));
3847
}
3948

4049
@Override
@@ -46,9 +55,30 @@ protected boolean isAvailable(@NotNull HttpServletRequest request, @NotNull SBui
4655
return buildType.getRunnerTypes().contains(SnykSecurityRunnerConstants.RUNNER_TYPE);
4756
}
4857

49-
private String getSnykHtmlReport(SBuild build) {
58+
private String readSnykHtmlReport(@NotNull SBuild build) {
5059
String snykHtmlReportPath = TEAMCITY_ARTIFACTS_DIR + separator + SNYK_ARTIFACTS_DIR + separator + SNYK_REPORT_HTML_FILE;
5160
BuildArtifact artifact = build.getArtifacts(BuildArtifactsViewMode.VIEW_HIDDEN_ONLY).getArtifact(snykHtmlReportPath);
52-
return artifact != null ? artifact.getRelativePath() : SNYK_REPORT_HTML_FILE;
61+
62+
if (artifact == null) {
63+
LOG.error("An error occurred while reading the Snyk HTML report - artifact not found");
64+
return generateErrorMessage("Artifact not found");
65+
}
66+
67+
try (InputStream inputStream = artifact.getInputStream();
68+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
69+
StringBuilder content = new StringBuilder();
70+
String line;
71+
while ((line = reader.readLine()) != null) {
72+
content.append(line).append("\n");
73+
}
74+
return content.toString();
75+
} catch (IOException e) {
76+
LOG.error("An error occurred while reading the Snyk HTML report.", e);
77+
return generateErrorMessage("An error occurred while reading the report");
78+
}
79+
}
80+
81+
private String generateErrorMessage(String message) {
82+
return "<html><head><title>Error</title></head><body><h1>Snyk report error: " + message + "</h1></body></html>";
5383
}
5484
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
2+
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3+
<%@ taglib prefix="bs" tagdir="/WEB-INF/tags" %>
4+
5+
<c:if test="${not empty error}">
6+
<c:out value="${error}"/>: <b><c:out value="${startPage}"/></b>. <!-- Probably need to replace startPage with something else -->
7+
</c:if>
8+
9+
<c:if test="${empty error}">
10+
<div>
11+
${snykHtmlReportContent}
12+
</div>
13+
</c:if>

0 commit comments

Comments
 (0)