Skip to content

Commit

Permalink
OPENAPI: added time-limit command and change hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobm-splunk authored and daveshanley committed Jun 21, 2024
1 parent 9da9009 commit 34bab3e
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 49 deletions.
16 changes: 9 additions & 7 deletions cmd/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func GetConsoleCommand() *cobra.Command {
failed := false
latestFlag, _ := cmd.Flags().GetBool("top")
limitFlag, _ := cmd.Flags().GetInt("limit")
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
baseFlag, _ := cmd.Flags().GetString("base")
remoteFlag, _ := cmd.Flags().GetBool("remote")

Expand Down Expand Up @@ -143,7 +144,7 @@ func GetConsoleCommand() *cobra.Command {
<-doneChan
return err
}
commits, e := runGithubHistoryConsole(user, repo, filePath, latestFlag, limitFlag, updateChan,
commits, e := runGithubHistoryConsole(user, repo, filePath, latestFlag, limitFlag, limitTimeFlag, updateChan,
errorChan, baseFlag, remoteFlag)

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

go listenForUpdates(updateChan, errorChan)

commits, errs := runGitHistoryConsole(args[0], args[1], latestFlag, limitFlag,
commits, errs := runGitHistoryConsole(args[0], args[1], latestFlag, limitFlag, limitTimeFlag,
updateChan, errorChan, baseFlag, remoteFlag)

// wait.
Expand Down Expand Up @@ -265,10 +266,10 @@ func GetConsoleCommand() *cobra.Command {
return cmd
}

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

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

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

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

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

// build commit history.
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit)
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit, limitTime)

if err != nil {
close(updateChan)
model.SendProgressError("git", fmt.Sprintf("%d errors found extracting history", len(err)), errorChan)
return nil, err
}

// populate history with changes and data
git.PopulateHistoryWithChanges(commitHistory, limit, updateChan, errorChan, base, remote)
git.PopulateHistoryWithChanges(commitHistory, limit, limitTime, updateChan, errorChan, base, remote)

if latest {
commitHistory = commitHistory[:1]
Expand Down
12 changes: 9 additions & 3 deletions cmd/flatten_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
package cmd

import (
whatChanged "github.com/pb33f/libopenapi/what-changed/model"
"github.com/pb33f/openapi-changes/model"
)

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

flatReport := &model.FlatReport{}
flatReport.Summary = report.Summary
var changes []*whatChanged.Change
var changes []*model.HashedChange
rpt := report.Commit.Changes
for _, change := range rpt.GetAllChanges() {
changes = append(changes, change)
hashedChange := model.HashedChange{
Change: change,
}

hashedChange.HashChange()

changes = append(changes, &hashedChange)
}
flatReport.Changes = changes

Expand All @@ -34,6 +39,7 @@ func FlattenHistoricalReport(report *model.HistoricalReport) *model.FlatHistoric
flatReport.GitFilePath = report.GitFilePath
flatReport.DateGenerated = report.DateGenerated
flatReport.Filename = report.Filename
flatReport.Reports = make([]*model.FlatReport, 0)
for _, r := range report.Reports {
flatReport.Reports = append(flatReport.Reports, FlattenReport(r))
}
Expand Down
15 changes: 8 additions & 7 deletions cmd/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func GetHTMLReportCommand() *cobra.Command {
cdnFlag, _ := cmd.Flags().GetBool("use-cdn")
latestFlag, _ := cmd.Flags().GetBool("top")
limitFlag, _ := cmd.Flags().GetInt("limit")
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
remoteFlag, _ := cmd.Flags().GetBool("remote")

if noColorFlag {
Expand Down Expand Up @@ -167,7 +168,7 @@ func GetHTMLReportCommand() *cobra.Command {
return err
}
report, _, er := RunGithubHistoryHTMLReport(user, repo, filePath, latestFlag, cdnFlag,
false, updateChan, errorChan, limitFlag, baseFlag, remoteFlag)
false, updateChan, errorChan, limitFlag, limitTimeFlag, baseFlag, remoteFlag)

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

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

func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool,
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int) ([]byte, []*model.Report, []error) {
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int, limitTime int) ([]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",
Expand All @@ -312,7 +313,7 @@ func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool,
}

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

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

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

commitHistory, errs := git.ProcessGithubRepo(username, repo, filePath, progressChan, errorChan, true, limit, base, remote)
commitHistory, errs := git.ProcessGithubRepo(username, repo, filePath, progressChan, errorChan, true, limit, limitTime, base, remote)
if latest && len(commitHistory) > 1 {
commitHistory = commitHistory[:1]
}
Expand Down
19 changes: 10 additions & 9 deletions cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func GetReportCommand() *cobra.Command {
failed := false
latestFlag, _ := cmd.Flags().GetBool("top")
limitFlag, _ := cmd.Flags().GetInt("limit")
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
baseFlag, _ := cmd.Flags().GetString("base")
noColorFlag, _ := cmd.Flags().GetBool("no-color")
remoteFlag, _ := cmd.Flags().GetBool("remote")
Expand Down Expand Up @@ -90,7 +91,7 @@ func GetReportCommand() *cobra.Command {
<-doneChan
return err
}
report, er := runGithubHistoryReport(user, repo, filePath, latestFlag, limitFlag, updateChan,
report, er := runGithubHistoryReport(user, repo, filePath, latestFlag, limitFlag, limitTimeFlag, updateChan,
errorChan, baseFlag, remoteFlag)

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

report, er := runGitHistoryReport(args[0], args[1], latestFlag, updateChan, errorChan, baseFlag,
remoteFlag, limitFlag)
remoteFlag, limitFlag, limitTimeFlag)

<-doneChan

Expand Down Expand Up @@ -224,7 +225,7 @@ func GetReportCommand() *cobra.Command {
}

func runGitHistoryReport(gitPath, filePath string, latest bool,
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int) (*model.HistoricalReport, []error) {
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int, limitTime int) (*model.HistoricalReport, []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")
Expand All @@ -238,15 +239,15 @@ func runGitHistoryReport(gitPath, filePath string, latest bool,
filePath, gitPath), false, updateChan)

// build commit history.
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit)
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit, limitTime)
if err != nil {
model.SendProgressError("git", fmt.Sprintf("%d errors found building history", len(err)), errorChan)
close(updateChan)
return nil, err
}

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

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

}

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

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

var ghReports []*model.Report
var ghReports = make([]*model.Report, 0)
for r := range commitHistory {
if commitHistory[r].Changes != nil {
ghReports = append(ghReports, createReport(commitHistory[r]))
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func init() {
rootCmd.AddCommand(GetHTMLReportCommand())
rootCmd.PersistentFlags().BoolP("top", "t", false, "Only show latest changes (last git revision against HEAD)")
rootCmd.PersistentFlags().IntP("limit", "l", 5, "Limit history to number of revisions (default is 5)")
rootCmd.PersistentFlags().IntP("limit-time", "d", -1, "Limit history to number of days. Supersedes limit argument if present.")
rootCmd.PersistentFlags().BoolP("no-logo", "b", false, "Don't print the big purple pb33f banner")
rootCmd.PersistentFlags().StringP("base", "p", "", "Base URL or path to use for resolving relative or remote references")
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)")
Expand Down
15 changes: 8 additions & 7 deletions cmd/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func GetSummaryCommand() *cobra.Command {
latestFlag, _ := cmd.Flags().GetBool("top")
noColorFlag, _ := cmd.Flags().GetBool("no-color")
limitFlag, _ := cmd.Flags().GetInt("limit")
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
remoteFlag, _ := cmd.Flags().GetBool("remote")
markdownFlag, _ := cmd.Flags().GetBool("markdown")

Expand Down Expand Up @@ -153,7 +154,7 @@ func GetSummaryCommand() *cobra.Command {
return err
}

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

err = runGitHistorySummary(args[0], args[1], latestFlag, updateChan, errorChan,
baseFlag, remoteFlag, markdownFlag, limitFlag)
baseFlag, remoteFlag, markdownFlag, limitFlag, limitTimeFlag)

<-doneChan

Expand Down Expand Up @@ -352,10 +353,10 @@ func runLeftRightSummary(left, right string, updateChan chan *model.ProgressUpda
return nil
}

func runGithubHistorySummary(username, repo, filePath string, latest bool, limit int,
func runGithubHistorySummary(username, repo, filePath string, latest bool, limit int, limitTime int,
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote, markdown bool) error {
commitHistory, _ := git.ProcessGithubRepo(username, repo, filePath, progressChan, errorChan,
false, limit, base, remote)
false, limit, limitTime, base, remote)

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

func runGitHistorySummary(gitPath, filePath string, latest bool,
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote, markdown bool, limit int) error {
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote, markdown bool, limit int, limitTime int) 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("git", err.Error(), errorChan)
Expand All @@ -382,15 +383,15 @@ func runGitHistorySummary(gitPath, filePath string, latest bool,
filePath, gitPath), false, updateChan)

// build commit history.
commitHistory, errs := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit)
commitHistory, errs := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit, limitTime)
if errs != nil {
model.SendProgressError("git", fmt.Sprintf("%d errors found extracting history", len(errs)), errorChan)
close(updateChan)
return errs[0]
}

// populate history with changes and data
git.PopulateHistoryWithChanges(commitHistory, 0, updateChan, errorChan, base, remote)
git.PopulateHistoryWithChanges(commitHistory, 0, limitTime, updateChan, errorChan, base, remote)

if latest {
commitHistory = commitHistory[:1]
Expand Down
Loading

0 comments on commit 34bab3e

Please sign in to comment.