-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathhtml_report.go
More file actions
486 lines (423 loc) · 14.7 KB
/
html_report.go
File metadata and controls
486 lines (423 loc) · 14.7 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package cmd
import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
whatChangedModel "github.com/pb33f/libopenapi/what-changed/model"
"github.com/pb33f/openapi-changes/git"
htmlReport "github.com/pb33f/openapi-changes/html-report"
"github.com/pb33f/openapi-changes/model"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
func GetHTMLReportCommand() *cobra.Command {
cmd := &cobra.Command{
SilenceUsage: false,
Use: "html-report",
Short: "Generate the sexiest, most interactive diffing experience you have ever seen.",
Long: "Generate a ready to go, super sexy, and highly interactive HTML report that " +
"you can explore and review in your browser",
Example: "openapi-changes html-report /path/to/git/repo path/to/file/in/repo/openapi.yaml",
RunE: func(cmd *cobra.Command, args []string) error {
updateChan := make(chan *model.ProgressUpdate)
errorChan := make(chan model.ProgressError)
doneChan := make(chan bool)
failed := false
baseFlag, _ := cmd.Flags().GetString("base")
baseCommitFlag, _ := cmd.Flags().GetString("base-commit")
noColorFlag, _ := cmd.Flags().GetBool("no-color")
cdnFlag, _ := cmd.Flags().GetBool("use-cdn")
latestFlag, _ := cmd.Flags().GetBool("top")
globalRevisionsFlag, _ := cmd.Flags().GetBool("global-revisions")
limitFlag, _ := cmd.Flags().GetInt("limit")
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
remoteFlag, _ := cmd.Flags().GetBool("remote")
reportFile, _ := cmd.Flags().GetString("report-file")
extRefs, _ := cmd.Flags().GetBool("ext-refs")
configFlag, _ := cmd.Flags().GetString("config")
// load breaking rules configuration
breakingConfig, err := LoadBreakingRulesConfig(configFlag)
if err != nil {
PrintConfigError(err)
return err
}
if noColorFlag {
pterm.DisableStyling()
pterm.DisableColor()
}
noBanner, _ := cmd.Flags().GetBool("no-logo")
if !noBanner {
PrintBanner()
}
// if there are no args, print out how to use the console.
if len(args) == 0 {
PrintHowToUse("html-report")
return nil
}
listenForUpdates := func(updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError) {
var spinner *pterm.SpinnerPrinter
if !noColorFlag {
spinner, _ = pterm.DefaultSpinner.Start("starting work.")
spinner.InfoPrinter = &pterm.PrefixPrinter{
MessageStyle: &pterm.Style{pterm.FgLightCyan},
Prefix: pterm.Prefix{
Style: &pterm.Style{pterm.FgBlack, pterm.BgLightMagenta},
Text: " SPEC ",
},
}
spinner.SuccessPrinter = &pterm.PrefixPrinter{
MessageStyle: &pterm.Style{pterm.FgLightCyan},
Prefix: pterm.Prefix{
Style: &pterm.Style{pterm.FgBlack, pterm.BgLightCyan},
Text: " DONE ",
},
}
}
var warnings []string
for {
select {
case update, ok := <-updateChan:
if ok {
if !noColorFlag {
if !update.Completed {
spinner.UpdateText(update.Message)
} else {
spinner.Info(update.Message)
}
}
if update.Warning {
warnings = append(warnings, update.Message)
}
} else {
if !failed {
if !noColorFlag {
spinner.Success("completed processing")
spinner.Stop()
pterm.Println()
pterm.Println()
}
} else {
if !noColorFlag {
spinner.Fail("failed to complete. sorry!")
pterm.Println()
pterm.Println()
}
}
if len(warnings) > 0 {
pterm.Warning.Print("warnings reported during processing")
pterm.Println()
dupes := make(map[string]bool)
for w := range warnings {
sum := md5.Sum([]byte(warnings[w]))
md5 := hex.EncodeToString(sum[:])
if !dupes[md5] {
dupes[md5] = true
} else {
continue
}
pterm.Println(fmt.Sprintf("⚠️ %s", pterm.FgYellow.Sprint(warnings[w])))
}
}
doneChan <- true
return
}
case err := <-errorChan:
if err.Fatal {
if !noColorFlag {
spinner.Fail(fmt.Sprintf("Stopped: %s", err.Message))
spinner.Stop()
} else {
pterm.Error.Println(err)
}
// doneChan <- true
//return
} else {
warnings = append(warnings, err.Message)
}
}
}
}
// check for two args (left and right)
if len(args) < 2 {
// check if arg is an url (like a github url)
specUrl, err := url.Parse(args[0])
if err == nil {
if specUrl.Host == "github.com" {
go listenForUpdates(updateChan, errorChan)
user, repo, filePath, err := ExtractGithubDetailsFromURL(specUrl)
if err != nil {
errorChan <- model.ProgressError{
Job: "github url",
Message: fmt.Sprintf("error extracting github details from url: %s", err.Error()),
}
<-doneChan
return err
}
report, _, er := RunGithubHistoryHTMLReport(user, repo, filePath, baseCommitFlag, latestFlag, cdnFlag,
false, updateChan, errorChan, limitFlag, limitTimeFlag, baseFlag, remoteFlag, extRefs, breakingConfig)
// wait for things to be completed.
<-doneChan
if len(report) <= 0 && er != nil {
return er[0]
}
return writeReportFile(reportFile, report)
}
} else {
pterm.Error.Println("Two arguments are required to compare left and right OpenAPI Specifications.")
return nil
}
}
if len(args) == 2 {
var left, right string
var urlErr error
// check if first arg is a URL
left, urlErr = checkURL(args[0], errorChan)
if urlErr != nil {
pterm.Error.Println(urlErr.Error())
return urlErr
}
// check if the first arg is a directory, if so - process as a git history operation.
p := left
f, err := os.Stat(p)
if err != nil {
pterm.Error.Printf("Cannot open file/repository: '%s'\n\n", args[0])
return err
}
if f.IsDir() {
repo, err := absoluteRepoPath(p)
if err != nil {
pterm.Error.Println(err.Error())
return err
}
p = args[1]
_, err = os.Stat(filepath.Join(repo, p))
if err != nil {
pterm.Error.Printf("Cannot open file/repository: '%s'\n\n", args[1])
return err
}
go listenForUpdates(updateChan, errorChan)
report, _, er := RunGitHistoryHTMLReport(args[0], args[1], baseCommitFlag, latestFlag, cdnFlag,
updateChan, errorChan, baseFlag, remoteFlag, extRefs, globalRevisionsFlag, limitFlag, limitTimeFlag, breakingConfig)
<-doneChan
if er != nil {
for x := range er {
pterm.Error.Println(er[x].Error())
}
return er[0]
}
writeErr := writeReportFile(reportFile, report)
return writeErr
} else {
go listenForUpdates(updateChan, errorChan)
// check if the first arg is a URL, if so download it, if not - assume it's a file.
left, urlErr = checkURL(args[0], errorChan)
if urlErr != nil {
pterm.Error.Println(urlErr.Error())
return urlErr
}
// check if the second arg is a URL, if so download it, if not - assume it's a file.
right, urlErr = checkURL(args[1], errorChan)
if urlErr != nil {
pterm.Error.Println(urlErr.Error())
return urlErr
}
report, errs := RunLeftRightHTMLReport(left, right, cdnFlag, updateChan, errorChan, baseFlag, remoteFlag, extRefs, breakingConfig)
<-doneChan
if len(errs) > 0 {
for e := range errs {
pterm.Error.Println(errs[e].Error())
}
return errors.New("unable to process specifications")
}
return writeReportFile(reportFile, report)
}
}
pterm.Error.Println("wrong number of arguments, expecting two (2)")
return nil
},
}
cmd.Flags().BoolP("no-color", "n", false, "Disable color and style output (very useful for CI/CD)")
cmd.Flags().Bool("use-cdn", false, "Use CDN for CSS and JS delivery instead of bundling inline")
cmd.Flags().StringP("report-file", "", "report.html", "The name of the HTML report file (defaults to 'report.html')")
return cmd
}
func writeReportFile(reportFile string, report []byte) error {
err := os.WriteFile(reportFile, report, 0744)
if err != nil {
pterm.Error.Println(err.Error())
return err
}
pterm.Success.Printf("report written to file '%s' (%dkb)", reportFile, len(report)/1024)
pterm.Println()
pterm.Println()
return nil
}
func ExtractGithubDetailsFromURL(url *url.URL) (string, string, string, error) {
path := url.Path
dir, file := filepath.Split(path)
dirSegments := strings.Split(dir, "/")
if len(dirSegments) >= 6 {
user := dirSegments[1]
repo := dirSegments[2]
filePath := fmt.Sprintf("%s%s", strings.Join(dirSegments[5:], "/"), file)
return user, repo, filePath, nil
} else {
return "", "", "", fmt.Errorf("url: '%s' not correctly formed", url.String())
}
}
func RunGitHistoryHTMLReport(gitPath, filePath, baseCommit string, latest, useCDN bool,
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote, extRefs bool, globalRevisions bool, limit int, limitTime int,
breakingConfig *whatChangedModel.BreakingRulesConfig,
) ([]byte, []*model.Report, []error) {
if gitPath == "" || filePath == "" {
err := errors.New("please supply a path to a git repo via -r, and a path to a file via -f")
model.SendProgressError("reading paths",
err.Error(), errorChan)
close(progressChan)
return nil, nil, []error{err}
}
// build commit history.
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, baseCommit, progressChan, errorChan, globalRevisions, limit, limitTime)
if err != nil {
model.SendFatalError("extraction",
fmt.Sprintf("cannot extract history %s", errors.Join(err...)), errorChan)
close(progressChan)
return nil, nil, err
}
// populate history with changes and data
commitHistory, err = git.PopulateHistoryWithChanges(commitHistory, 0, limitTime, progressChan, errorChan, base, remote, extRefs, breakingConfig)
if err != nil {
model.SendFatalError("extraction",
fmt.Sprintf("cannot extract history %s", errors.Join(err...)), errorChan)
close(progressChan)
return nil, nil, err
}
if latest {
commitHistory = commitHistory[:1]
}
var reports []*model.Report
for r := range commitHistory {
if commitHistory[r].Changes != nil {
reports = append(reports, createReport(commitHistory[r]))
}
}
if len(reports) > 0 {
model.SendProgressUpdate("extraction",
fmt.Sprintf("Extracted '%d' reports from file history", len(reports)), true, progressChan)
}
close(progressChan)
generator := htmlReport.NewHTMLReport(false, time.Now(), commitHistory)
return generator.GenerateReport(false, useCDN, false), reports, nil
}
func RunGithubHistoryHTMLReport(username, repo, filePath, baseCommit string, latest, useCDN, embeddedMode bool,
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, limit int, limitTime int, base string, remote, extRefs bool,
breakingConfig *whatChangedModel.BreakingRulesConfig,
) ([]byte, []*model.Report, []error) {
commitHistory, errs := git.ProcessGithubRepo(username, repo, filePath, baseCommit, progressChan, errorChan, true, limit, limitTime, base, remote, extRefs, breakingConfig)
if latest && len(commitHistory) > 1 {
commitHistory = commitHistory[:1]
}
var reports []*model.Report
for r := range commitHistory {
if commitHistory[r].Changes != nil {
reports = append(reports, createReport(commitHistory[r]))
}
}
if len(reports) > 0 {
model.SendProgressUpdate("extraction",
fmt.Sprintf("Extracted '%d' reports from file history", len(reports)), true, progressChan)
}
// if there are no reports and no commits, return no bytes.
if len(reports) == 0 {
model.SendProgressError("extraction",
fmt.Sprintf("history extraction failed %d reports generated for '%s'", len(reports), filePath), errorChan)
close(progressChan)
return nil, nil, append(errs, fmt.Errorf("no reports extracted, no history found for file '%s'", filePath))
}
generator := htmlReport.NewHTMLReport(false, time.Now(), commitHistory)
close(progressChan)
return generator.GenerateReport(false, useCDN, embeddedMode), reports, errs
}
func RunLeftRightHTMLReport(left, right string, useCDN bool,
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote, extRefs bool,
breakingConfig *whatChangedModel.BreakingRulesConfig,
) ([]byte, []error) {
var leftBytes, rightBytes []byte
var errs []error
var err error
leftBytes, err = os.ReadFile(left)
if err != nil {
model.SendFatalError("extraction",
fmt.Sprintf("cannot read original spec: %s", err.Error()), errorChan)
close(progressChan)
return nil, []error{err}
}
rightBytes, err = os.ReadFile(right)
if err != nil {
model.SendFatalError("extraction",
fmt.Sprintf("cannot read modified spec: %s", err.Error()), errorChan)
close(progressChan)
return nil, []error{err}
}
commits := []*model.Commit{
{
Hash: uuid.New().String()[:6],
Message: fmt.Sprintf("Original: %s. Modified: %s", left, right),
CommitDate: time.Now(),
Data: rightBytes,
FilePath: right,
}, {
Hash: uuid.New().String()[:6],
Message: fmt.Sprintf("Original file: %s", left),
CommitDate: time.Now(),
Data: leftBytes,
FilePath: left,
},
}
commits, errs = git.BuildCommitChangelog(commits, progressChan, errorChan, base, remote, extRefs, breakingConfig)
if len(errs) > 0 {
close(progressChan)
return nil, errs
}
generator := htmlReport.NewHTMLReport(false, time.Now(), commits)
close(progressChan)
return generator.GenerateReport(false, useCDN, false), nil
//return generator.GenerateReport(true, false, false), nil
}
func RunLeftRightHTMLReportViaString(left, right string, useCDN, embedded bool,
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote, extRefs bool,
breakingConfig *whatChangedModel.BreakingRulesConfig,
) ([]byte, []error) {
var errs []error
commits := []*model.Commit{
{
Hash: uuid.New().String()[:6],
Message: fmt.Sprintf("Uploaded original (%d bytes)", len(left)),
CommitDate: time.Now(),
Data: []byte(left),
},
{
Hash: uuid.New().String()[:6],
Message: fmt.Sprintf("Uploaded modification (%d bytes)", len(right)),
CommitDate: time.Now(),
Data: []byte(right),
},
}
commits, errs = git.BuildCommitChangelog(commits, progressChan, errorChan, base, remote, extRefs, breakingConfig)
if len(errs) > 0 {
close(progressChan)
return nil, errs
}
generator := htmlReport.NewHTMLReport(false, time.Now(), commits)
close(progressChan)
return generator.GenerateReport(false, useCDN, embedded), nil
}