1
1
package io .snyk .plugins .teamcity .server ;
2
2
3
3
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 ;
4
9
import java .util .Map ;
5
10
6
11
import io .snyk .plugins .teamcity .common .SnykSecurityRunnerConstants ;
13
18
import jetbrains .buildServer .web .openapi .PluginDescriptor ;
14
19
import jetbrains .buildServer .web .openapi .PositionConstraint ;
15
20
import jetbrains .buildServer .web .openapi .ViewLogTab ;
21
+ import org .apache .log4j .Logger ;
16
22
import org .jetbrains .annotations .NotNull ;
17
23
18
24
import static io .snyk .plugins .teamcity .common .SnykSecurityRunnerConstants .SNYK_ARTIFACTS_DIR ;
@@ -25,16 +31,19 @@ public class SnykSecurityReportTab extends ViewLogTab {
25
31
private static final String TAB_TITLE = "Snyk Security Report" ;
26
32
private static final String TAB_CODE = "snykSecurityReport" ;
27
33
34
+ private static final Logger LOG = Logger .getLogger (SnykSecurityReportTab .class );
35
+
28
36
public SnykSecurityReportTab (@ NotNull PagePlaces pagePlaces , @ NotNull SBuildServer server , @ NotNull PluginDescriptor pluginDescriptor ) {
29
37
super (TAB_TITLE , TAB_CODE , pagePlaces , server );
30
38
31
- setIncludeUrl ("/artifactsViewer.jsp" );
39
+ String path = pluginDescriptor .getPluginResourcesPath ("tab/snykReport.jsp" );
40
+ setIncludeUrl (path );
32
41
setPosition (PositionConstraint .after ("artifacts" ));
33
42
}
34
43
35
44
@ Override
36
45
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 ));
38
47
}
39
48
40
49
@ Override
@@ -46,9 +55,30 @@ protected boolean isAvailable(@NotNull HttpServletRequest request, @NotNull SBui
46
55
return buildType .getRunnerTypes ().contains (SnykSecurityRunnerConstants .RUNNER_TYPE );
47
56
}
48
57
49
- private String getSnykHtmlReport ( SBuild build ) {
58
+ private String readSnykHtmlReport ( @ NotNull SBuild build ) {
50
59
String snykHtmlReportPath = TEAMCITY_ARTIFACTS_DIR + separator + SNYK_ARTIFACTS_DIR + separator + SNYK_REPORT_HTML_FILE ;
51
60
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>" ;
53
83
}
54
84
}
0 commit comments