-
Notifications
You must be signed in to change notification settings - Fork 583
/
reporting.go
352 lines (318 loc) · 8.51 KB
/
reporting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package reporting
import (
"bytes"
"encoding/json"
"fmt"
"text/template"
"time"
"github.com/pkg/errors"
)
// IssueDetail represents any content that can be transformed into the body of a GitHub issue
type IssueDetail interface {
Title() string
ToMarkdown() ([]byte, error)
ToTxt() string
}
// ScanReport defines the elements of a scan report used by various scan steps
type ScanReport struct {
StepName string `json:"stepName"`
ReportTitle string `json:"title"`
Subheaders []Subheader `json:"subheaders"`
Overview []OverviewRow `json:"overview"`
FurtherInfo string `json:"furtherInfo"`
ReportTime time.Time `json:"reportTime"`
DetailTable ScanDetailTable `json:"detailTable"`
SuccessfulScan bool `json:"successfulScan"`
}
// ScanDetailTable defines a table containing scan result details
type ScanDetailTable struct {
Headers []string `json:"headers"`
Rows []ScanRow `json:"rows"`
WithCounter bool `json:"withCounter"`
CounterHeader string `json:"counterHeader"`
NoRowsMessage string `json:"noRowsMessage"`
}
// ScanRow defines one row of a scan result table
type ScanRow struct {
Columns []ScanCell `json:"columns"`
}
// AddColumn adds a column to a dedicated ScanRow
func (s *ScanRow) AddColumn(content interface{}, style ColumnStyle) {
if s.Columns == nil {
s.Columns = []ScanCell{}
}
s.Columns = append(s.Columns, ScanCell{Content: fmt.Sprint(content), Style: style})
}
// ScanCell defines one column of a scan result table
type ScanCell struct {
Content string `json:"content"`
Style ColumnStyle `json:"style"`
}
// ColumnStyle defines style for a specific column
type ColumnStyle int
// enum for style types
const (
Green = iota + 1
Yellow
Red
Grey
Black
)
func (c ColumnStyle) String() string {
return [...]string{"", "green-cell", "yellow-cell", "red-cell", "grey-cell", "black-cell"}[c]
}
// OverviewRow defines a row in the report's overview section
// it can consist of a description and some details where the details can have a style attached
type OverviewRow struct {
Description string `json:"description"`
Details string `json:"details,omitempty"`
Style ColumnStyle `json:"style,omitempty"`
}
// Subheader defines a dedicated sub header in a report
type Subheader struct {
Description string `json:"text"`
Details string `json:"details,omitempty"`
}
// AddSubHeader adds a sub header to the report containing of a text/title plus optional details
func (s *ScanReport) AddSubHeader(header, details string) {
s.Subheaders = append(s.Subheaders, Subheader{Description: header, Details: details})
}
// StepReportDirectory specifies the default directory for markdown reports which can later be collected by step pipelineCreateSummary
const StepReportDirectory = ".pipeline/stepReports"
// ToJSON returns the report in JSON format
func (s *ScanReport) ToJSON() ([]byte, error) {
return json.Marshal(s)
}
// ToTxt up to now returns the report in JSON format
func (s ScanReport) ToTxt() string {
txt, _ := s.ToJSON()
return string(txt)
}
const reportHTMLTemplate = `<!DOCTYPE html>
<html>
<head>
<title>{{.Title}}</title>
<style type="text/css">
body {
font-family: Arial, Verdana;
}
table {
border-collapse: collapse;
}
div.code {
font-family: "Courier New", "Lucida Console";
}
th {
border-top: 1px solid #ddd;
}
th, td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
border-right: 1px solid #ddd;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
.bold {
font-weight: bold;
}
.green{
color: olivedrab;
}
.red{
color: orangered;
}
.nobullets {
list-style-type:none;
padding-left: 0;
padding-bottom: 0;
margin: 0;
}
.green-cell {
background-color: #e1f5a9;
padding: 5px
}
.yellow-cell {
background-color: #ffff99;
padding: 5px
}
.red-cell {
background-color: #ffe5e5;
padding: 5px
}
.grey-cell{
background-color: rgba(212, 212, 212, 0.7);
padding: 5px;
}
.black-cell{
background-color: rgba(0, 0, 0, 0.75);
padding: 5px;
}
</style>
</head>
<body>
<h1>{{.Title}}</h1>
<h2>
<span>
{{range $s := .Subheaders}}
{{- $s.Description}}: {{$s.Details}}<br />
{{end -}}
</span>
</h2>
<div>
<h3>
{{range $o := .Overview}}
{{- drawOverviewRow $o}}<br />
{{end -}}
</h3>
<span>{{.FurtherInfo}}</span>
</div>
<p>Snapshot taken: {{reportTime .ReportTime}}</p>
<table>
<tr>
{{if .DetailTable.WithCounter}}<th>{{.DetailTable.CounterHeader}}</th>{{end}}
{{- range $h := .DetailTable.Headers}}
<th>{{$h}}</th>
{{- end}}
</tr>
{{range $i, $r := .DetailTable.Rows}}
<tr>
{{if $.DetailTable.WithCounter}}<td>{{inc $i}}</td>{{end}}
{{- range $c := $r.Columns}}
{{drawCell $c}}
{{- end}}
</tr>
{{else}}
<tr><td colspan="{{columnCount .DetailTable}}">{{.DetailTable.NoRowsMessage}}</td></tr>
{{- end}}
</table>
</body>
</html>
`
// ToHTML creates a HTML version of the report
func (s *ScanReport) ToHTML() ([]byte, error) {
funcMap := template.FuncMap{
"inc": func(i int) int {
return i + 1
},
"reportTime": func(currentTime time.Time) string {
return currentTime.Format("Jan 02, 2006 - 15:04:05 MST")
},
"columnCount": tableColumnCount,
"drawCell": drawCell,
"drawOverviewRow": drawOverviewRow,
}
report := []byte{}
tmpl, err := template.New("report").Funcs(funcMap).Parse(reportHTMLTemplate)
if err != nil {
return report, errors.Wrap(err, "failed to create HTML report template")
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, s)
if err != nil {
return report, errors.Wrap(err, "failed to execute HTML report template")
}
return buf.Bytes(), nil
}
const reportMdTemplate = `## {{if .SuccessfulScan}}:white_check_mark:{{else}}:x:{{end}} {{.Title}}
<table>
{{range $s := .Subheaders -}}
<tr><td><b>{{- $s.Description}}:</b></td><td>{{$s.Details}}</td></tr>
{{- end}}
{{range $o := .Overview -}}
{{drawOverviewRow $o}}
{{- end}}
</table>
{{.FurtherInfo}}
Snapshot taken: <i>{{reportTime .ReportTime}}</i>
{{if shouldDrawTable .DetailTable -}}
<details><summary><i>{{.Title}} details:</i></summary>
<p>
<table>
<tr>
{{if .DetailTable.WithCounter}}<th>{{.DetailTable.CounterHeader}}</th>{{end}}
{{- range $h := .DetailTable.Headers}}
<th>{{$h}}</th>
{{- end}}
</tr>
{{range $i, $r := .DetailTable.Rows}}
<tr>
{{if $.DetailTable.WithCounter}}<td>{{inc $i}}</td>{{end}}
{{- range $c := $r.Columns}}
{{drawCell $c}}
{{- end}}
</tr>
{{else}}
<tr><td colspan="{{columnCount .DetailTable}}">{{.DetailTable.NoRowsMessage}}</td></tr>
{{- end}}
</table>
</p>
</details>
{{ end }}
`
// Title returns the title of the report
func (s ScanReport) Title() string {
return s.ReportTitle
}
// ToMarkdown creates a markdown version of the report content
func (s ScanReport) ToMarkdown() ([]byte, error) {
funcMap := template.FuncMap{
"columnCount": tableColumnCount,
"drawCell": drawCell,
"shouldDrawTable": shouldDrawTable,
"inc": func(i int) int {
return i + 1
},
"reportTime": func(currentTime time.Time) string {
return currentTime.Format("Jan 02, 2006 - 15:04:05 MST")
},
"drawOverviewRow": drawOverviewRowMarkdown,
}
report := []byte{}
tmpl, err := template.New("report").Funcs(funcMap).Parse(reportMdTemplate)
if err != nil {
return report, errors.Wrap(err, "failed to create Markdown report template")
}
buf := new(bytes.Buffer)
err = tmpl.Execute(buf, s)
if err != nil {
return report, errors.Wrap(err, "failed to execute Markdown report template")
}
return buf.Bytes(), nil
}
func tableColumnCount(scanDetails ScanDetailTable) int {
colCount := len(scanDetails.Headers)
if scanDetails.WithCounter {
colCount++
}
return colCount
}
func drawCell(cell ScanCell) string {
if cell.Style > 0 {
return fmt.Sprintf(`<td class="%v">%v</td>`, cell.Style, cell.Content)
}
return fmt.Sprintf(`<td>%v</td>`, cell.Content)
}
func shouldDrawTable(table ScanDetailTable) bool {
if len(table.Headers) > 0 {
return true
}
return false
}
func drawOverviewRow(row OverviewRow) string {
// so far accept only accept max. two columns for overview table: description and content
if len(row.Details) == 0 {
return row.Description
}
// ToDo: allow styling of details
return fmt.Sprintf("%v: %v", row.Description, row.Details)
}
func drawOverviewRowMarkdown(row OverviewRow) string {
// so far accept only accept max. two columns for overview table: description and content
if len(row.Details) == 0 {
return row.Description
}
// ToDo: allow styling of details
return fmt.Sprintf("<tr><td>%v:</td><td>%v</td></tr>", row.Description, row.Details)
}