Skip to content

Commit 34bab3e

Browse files
jacobm-splunkdaveshanley
authored andcommitted
OPENAPI: added time-limit command and change hashing
1 parent 9da9009 commit 34bab3e

File tree

10 files changed

+153
-49
lines changed

10 files changed

+153
-49
lines changed

cmd/console.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func GetConsoleCommand() *cobra.Command {
3838
failed := false
3939
latestFlag, _ := cmd.Flags().GetBool("top")
4040
limitFlag, _ := cmd.Flags().GetInt("limit")
41+
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
4142
baseFlag, _ := cmd.Flags().GetString("base")
4243
remoteFlag, _ := cmd.Flags().GetBool("remote")
4344

@@ -143,7 +144,7 @@ func GetConsoleCommand() *cobra.Command {
143144
<-doneChan
144145
return err
145146
}
146-
commits, e := runGithubHistoryConsole(user, repo, filePath, latestFlag, limitFlag, updateChan,
147+
commits, e := runGithubHistoryConsole(user, repo, filePath, latestFlag, limitFlag, limitTimeFlag, updateChan,
147148
errorChan, baseFlag, remoteFlag)
148149

149150
// wait for things to be completed.
@@ -204,7 +205,7 @@ func GetConsoleCommand() *cobra.Command {
204205

205206
go listenForUpdates(updateChan, errorChan)
206207

207-
commits, errs := runGitHistoryConsole(args[0], args[1], latestFlag, limitFlag,
208+
commits, errs := runGitHistoryConsole(args[0], args[1], latestFlag, limitFlag, limitTimeFlag,
208209
updateChan, errorChan, baseFlag, remoteFlag)
209210

210211
// wait.
@@ -265,10 +266,10 @@ func GetConsoleCommand() *cobra.Command {
265266
return cmd
266267
}
267268

268-
func runGithubHistoryConsole(username, repo, filePath string, latest bool, limit int,
269+
func runGithubHistoryConsole(username, repo, filePath string, latest bool, limit int, limitTime int,
269270
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) ([]*model.Commit, []error) {
270271

271-
commitHistory, errs := git.ProcessGithubRepo(username, repo, filePath, progressChan, errorChan, true, limit, base, remote)
272+
commitHistory, errs := git.ProcessGithubRepo(username, repo, filePath, progressChan, errorChan, true, limit, limitTime, base, remote)
272273

273274
if latest && len(commitHistory) > 1 {
274275
commitHistory = commitHistory[:1]
@@ -300,7 +301,7 @@ func runGithubHistoryConsole(username, repo, filePath string, latest bool, limit
300301
return commitHistory, nil
301302
}
302303

303-
func runGitHistoryConsole(gitPath, filePath string, latest bool, limit int,
304+
func runGitHistoryConsole(gitPath, filePath string, latest bool, limit int, limitTime int,
304305
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) ([]*model.Commit, []error) {
305306

306307
if gitPath == "" || filePath == "" {
@@ -314,15 +315,16 @@ func runGitHistoryConsole(gitPath, filePath string, latest bool, limit int,
314315
filePath, gitPath), false, updateChan)
315316

316317
// build commit history.
317-
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit)
318+
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit, limitTime)
319+
318320
if err != nil {
319321
close(updateChan)
320322
model.SendProgressError("git", fmt.Sprintf("%d errors found extracting history", len(err)), errorChan)
321323
return nil, err
322324
}
323325

324326
// populate history with changes and data
325-
git.PopulateHistoryWithChanges(commitHistory, limit, updateChan, errorChan, base, remote)
327+
git.PopulateHistoryWithChanges(commitHistory, limit, limitTime, updateChan, errorChan, base, remote)
326328

327329
if latest {
328330
commitHistory = commitHistory[:1]

cmd/flatten_report.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44
package cmd
55

66
import (
7-
whatChanged "github.com/pb33f/libopenapi/what-changed/model"
87
"github.com/pb33f/openapi-changes/model"
98
)
109

1110
func FlattenReport(report *model.Report) *model.FlatReport {
1211

1312
flatReport := &model.FlatReport{}
1413
flatReport.Summary = report.Summary
15-
var changes []*whatChanged.Change
14+
var changes []*model.HashedChange
1615
rpt := report.Commit.Changes
1716
for _, change := range rpt.GetAllChanges() {
18-
changes = append(changes, change)
17+
hashedChange := model.HashedChange{
18+
Change: change,
19+
}
20+
21+
hashedChange.HashChange()
22+
23+
changes = append(changes, &hashedChange)
1924
}
2025
flatReport.Changes = changes
2126

@@ -34,6 +39,7 @@ func FlattenHistoricalReport(report *model.HistoricalReport) *model.FlatHistoric
3439
flatReport.GitFilePath = report.GitFilePath
3540
flatReport.DateGenerated = report.DateGenerated
3641
flatReport.Filename = report.Filename
42+
flatReport.Reports = make([]*model.FlatReport, 0)
3743
for _, r := range report.Reports {
3844
flatReport.Reports = append(flatReport.Reports, FlattenReport(r))
3945
}

cmd/html_report.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func GetHTMLReportCommand() *cobra.Command {
4242
cdnFlag, _ := cmd.Flags().GetBool("use-cdn")
4343
latestFlag, _ := cmd.Flags().GetBool("top")
4444
limitFlag, _ := cmd.Flags().GetInt("limit")
45+
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
4546
remoteFlag, _ := cmd.Flags().GetBool("remote")
4647

4748
if noColorFlag {
@@ -167,7 +168,7 @@ func GetHTMLReportCommand() *cobra.Command {
167168
return err
168169
}
169170
report, _, er := RunGithubHistoryHTMLReport(user, repo, filePath, latestFlag, cdnFlag,
170-
false, updateChan, errorChan, limitFlag, baseFlag, remoteFlag)
171+
false, updateChan, errorChan, limitFlag, limitTimeFlag, baseFlag, remoteFlag)
171172

172173
// wait for things to be completed.
173174
<-doneChan
@@ -225,7 +226,7 @@ func GetHTMLReportCommand() *cobra.Command {
225226
go listenForUpdates(updateChan, errorChan)
226227

227228
report, _, er := RunGitHistoryHTMLReport(args[0], args[1], latestFlag, cdnFlag,
228-
updateChan, errorChan, baseFlag, remoteFlag, limitFlag)
229+
updateChan, errorChan, baseFlag, remoteFlag, limitFlag, limitTimeFlag)
229230
<-doneChan
230231
if er != nil {
231232
for x := range er {
@@ -302,7 +303,7 @@ func ExtractGithubDetailsFromURL(url *url.URL) (string, string, string, error) {
302303
}
303304

304305
func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool,
305-
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int) ([]byte, []*model.Report, []error) {
306+
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int, limitTime int) ([]byte, []*model.Report, []error) {
306307
if gitPath == "" || filePath == "" {
307308
err := errors.New("please supply a path to a git repo via -r, and a path to a file via -f")
308309
model.SendProgressError("reading paths",
@@ -312,7 +313,7 @@ func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool,
312313
}
313314

314315
// build commit history.
315-
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, progressChan, errorChan, limit)
316+
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, progressChan, errorChan, limit, limitTime)
316317
if err != nil {
317318
model.SendFatalError("extraction",
318319
fmt.Sprintf("cannot extract history %s", errors.Join(err...)), errorChan)
@@ -321,7 +322,7 @@ func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool,
321322
}
322323

323324
// populate history with changes and data
324-
commitHistory, err = git.PopulateHistoryWithChanges(commitHistory, 0, progressChan, errorChan, base, remote)
325+
commitHistory, err = git.PopulateHistoryWithChanges(commitHistory, 0, limitTime, progressChan, errorChan, base, remote)
325326
if err != nil {
326327
model.SendFatalError("extraction",
327328
fmt.Sprintf("cannot extract history %s", errors.Join(err...)), errorChan)
@@ -352,9 +353,9 @@ func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool,
352353
}
353354

354355
func RunGithubHistoryHTMLReport(username, repo, filePath string, latest, useCDN, embeddedMode bool,
355-
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, limit int, base string, remote bool) ([]byte, []*model.Report, []error) {
356+
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, limit int, limitTime int, base string, remote bool) ([]byte, []*model.Report, []error) {
356357

357-
commitHistory, errs := git.ProcessGithubRepo(username, repo, filePath, progressChan, errorChan, true, limit, base, remote)
358+
commitHistory, errs := git.ProcessGithubRepo(username, repo, filePath, progressChan, errorChan, true, limit, limitTime, base, remote)
358359
if latest && len(commitHistory) > 1 {
359360
commitHistory = commitHistory[:1]
360361
}

cmd/report.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func GetReportCommand() *cobra.Command {
3737
failed := false
3838
latestFlag, _ := cmd.Flags().GetBool("top")
3939
limitFlag, _ := cmd.Flags().GetInt("limit")
40+
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
4041
baseFlag, _ := cmd.Flags().GetString("base")
4142
noColorFlag, _ := cmd.Flags().GetBool("no-color")
4243
remoteFlag, _ := cmd.Flags().GetBool("remote")
@@ -90,7 +91,7 @@ func GetReportCommand() *cobra.Command {
9091
<-doneChan
9192
return err
9293
}
93-
report, er := runGithubHistoryReport(user, repo, filePath, latestFlag, limitFlag, updateChan,
94+
report, er := runGithubHistoryReport(user, repo, filePath, latestFlag, limitFlag, limitTimeFlag, updateChan,
9495
errorChan, baseFlag, remoteFlag)
9596

9697
// wait for things to be completed.
@@ -153,7 +154,7 @@ func GetReportCommand() *cobra.Command {
153154
go listenForUpdates(updateChan, errorChan)
154155

155156
report, er := runGitHistoryReport(args[0], args[1], latestFlag, updateChan, errorChan, baseFlag,
156-
remoteFlag, limitFlag)
157+
remoteFlag, limitFlag, limitTimeFlag)
157158

158159
<-doneChan
159160

@@ -224,7 +225,7 @@ func GetReportCommand() *cobra.Command {
224225
}
225226

226227
func runGitHistoryReport(gitPath, filePath string, latest bool,
227-
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int) (*model.HistoricalReport, []error) {
228+
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int, limitTime int) (*model.HistoricalReport, []error) {
228229

229230
if gitPath == "" || filePath == "" {
230231
err := errors.New("please supply a path to a git repo via -r, and a path to a file via -f")
@@ -238,15 +239,15 @@ func runGitHistoryReport(gitPath, filePath string, latest bool,
238239
filePath, gitPath), false, updateChan)
239240

240241
// build commit history.
241-
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit)
242+
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit, limitTime)
242243
if err != nil {
243244
model.SendProgressError("git", fmt.Sprintf("%d errors found building history", len(err)), errorChan)
244245
close(updateChan)
245246
return nil, err
246247
}
247248

248249
// populate history with changes and data
249-
commitHistory, err = git.PopulateHistoryWithChanges(commitHistory, 0, updateChan, errorChan, base, remote)
250+
commitHistory, err = git.PopulateHistoryWithChanges(commitHistory, 0, limitTime, updateChan, errorChan, base, remote)
250251
if err != nil {
251252
model.SendProgressError("git", fmt.Sprintf("%d errors found extracting history", len(err)), errorChan)
252253
close(updateChan)
@@ -257,7 +258,7 @@ func runGitHistoryReport(gitPath, filePath string, latest bool,
257258
commitHistory = commitHistory[:1]
258259
}
259260

260-
var changeReports []*model.Report
261+
var changeReports = make([]*model.Report, 0)
261262
for r := range commitHistory {
262263
if commitHistory[r].Changes != nil {
263264
changeReports = append(changeReports, createReport(commitHistory[r]))
@@ -278,11 +279,11 @@ func runGitHistoryReport(gitPath, filePath string, latest bool,
278279

279280
}
280281

281-
func runGithubHistoryReport(username, repo, filePath string, latest bool, limit int,
282+
func runGithubHistoryReport(username, repo, filePath string, latest bool, limit int, limitTime int,
282283
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) (*model.HistoricalReport, []error) {
283284

284285
commitHistory, errs := git.ProcessGithubRepo(username, repo, filePath, progressChan, errorChan,
285-
false, limit, base, remote)
286+
false, limit, limitTime, base, remote)
286287
if errs != nil {
287288
model.SendProgressError("git", errors.Join(errs...).Error(), errorChan)
288289
close(progressChan)
@@ -293,7 +294,7 @@ func runGithubHistoryReport(username, repo, filePath string, latest bool, limit
293294
commitHistory = commitHistory[:1]
294295
}
295296

296-
var ghReports []*model.Report
297+
var ghReports = make([]*model.Report, 0)
297298
for r := range commitHistory {
298299
if commitHistory[r].Changes != nil {
299300
ghReports = append(ghReports, createReport(commitHistory[r]))

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func init() {
6161
rootCmd.AddCommand(GetHTMLReportCommand())
6262
rootCmd.PersistentFlags().BoolP("top", "t", false, "Only show latest changes (last git revision against HEAD)")
6363
rootCmd.PersistentFlags().IntP("limit", "l", 5, "Limit history to number of revisions (default is 5)")
64+
rootCmd.PersistentFlags().IntP("limit-time", "d", -1, "Limit history to number of days. Supersedes limit argument if present.")
6465
rootCmd.PersistentFlags().BoolP("no-logo", "b", false, "Don't print the big purple pb33f banner")
6566
rootCmd.PersistentFlags().StringP("base", "p", "", "Base URL or path to use for resolving relative or remote references")
6667
rootCmd.PersistentFlags().BoolP("remote", "r", true, "Allow remote reference (URLs and files) to be auto resolved, without a base URL or path (default is on)")

cmd/summary.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func GetSummaryCommand() *cobra.Command {
4343
latestFlag, _ := cmd.Flags().GetBool("top")
4444
noColorFlag, _ := cmd.Flags().GetBool("no-color")
4545
limitFlag, _ := cmd.Flags().GetInt("limit")
46+
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
4647
remoteFlag, _ := cmd.Flags().GetBool("remote")
4748
markdownFlag, _ := cmd.Flags().GetBool("markdown")
4849

@@ -153,7 +154,7 @@ func GetSummaryCommand() *cobra.Command {
153154
return err
154155
}
155156

156-
er := runGithubHistorySummary(user, repo, filePath, latestFlag, limitFlag, updateChan,
157+
er := runGithubHistorySummary(user, repo, filePath, latestFlag, limitFlag, limitTimeFlag, updateChan,
157158
errorChan, baseFlag, remoteFlag, markdownFlag)
158159
// wait for things to be completed.
159160
<-doneChan
@@ -206,7 +207,7 @@ func GetSummaryCommand() *cobra.Command {
206207
go listenForUpdates(updateChan, errorChan)
207208

208209
err = runGitHistorySummary(args[0], args[1], latestFlag, updateChan, errorChan,
209-
baseFlag, remoteFlag, markdownFlag, limitFlag)
210+
baseFlag, remoteFlag, markdownFlag, limitFlag, limitTimeFlag)
210211

211212
<-doneChan
212213

@@ -352,10 +353,10 @@ func runLeftRightSummary(left, right string, updateChan chan *model.ProgressUpda
352353
return nil
353354
}
354355

355-
func runGithubHistorySummary(username, repo, filePath string, latest bool, limit int,
356+
func runGithubHistorySummary(username, repo, filePath string, latest bool, limit int, limitTime int,
356357
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote, markdown bool) error {
357358
commitHistory, _ := git.ProcessGithubRepo(username, repo, filePath, progressChan, errorChan,
358-
false, limit, base, remote)
359+
false, limit, limitTime, base, remote)
359360

360361
if latest {
361362
commitHistory = commitHistory[:1]
@@ -370,7 +371,7 @@ func runGithubHistorySummary(username, repo, filePath string, latest bool, limit
370371
}
371372

372373
func runGitHistorySummary(gitPath, filePath string, latest bool,
373-
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote, markdown bool, limit int) error {
374+
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote, markdown bool, limit int, limitTime int) error {
374375
if gitPath == "" || filePath == "" {
375376
err := errors.New("please supply a path to a git repo via -r, and a path to a file via -f")
376377
model.SendProgressError("git", err.Error(), errorChan)
@@ -382,15 +383,15 @@ func runGitHistorySummary(gitPath, filePath string, latest bool,
382383
filePath, gitPath), false, updateChan)
383384

384385
// build commit history.
385-
commitHistory, errs := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit)
386+
commitHistory, errs := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit, limitTime)
386387
if errs != nil {
387388
model.SendProgressError("git", fmt.Sprintf("%d errors found extracting history", len(errs)), errorChan)
388389
close(updateChan)
389390
return errs[0]
390391
}
391392

392393
// populate history with changes and data
393-
git.PopulateHistoryWithChanges(commitHistory, 0, updateChan, errorChan, base, remote)
394+
git.PopulateHistoryWithChanges(commitHistory, 0, limitTime, updateChan, errorChan, base, remote)
394395

395396
if latest {
396397
commitHistory = commitHistory[:1]

0 commit comments

Comments
 (0)