Skip to content

Commit

Permalink
temp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed Dec 19, 2024
1 parent dfe91e9 commit 5a33821
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/digger/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func PreRun(cmd *cobra.Command, args []string) {
//PolicyChecker = policy.NewPolicyChecker(hostName, orgName, token)

if os.Getenv("REPORTING_STRATEGY") == "comments_per_run" || os.Getenv("ACCUMULATE_PLANS") == "true" {
ReportStrategy = &reporting.CommentPerRunStrategy{
ReportStrategy = &reporting.SingleCommentStrategy{
TimeOfRun: time.Now(),
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion ee/cli/cmd/digger/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func PreRun(cmd *cobra.Command, args []string) {
BackendApi = NewBackendApi(hostName, token)

if os.Getenv("REPORTING_STRATEGY") == "comments_per_run" || os.Getenv("ACCUMULATE_PLANS") == "true" {
ReportStrategy = &reporting.CommentPerRunStrategy{
ReportStrategy = &reporting.SingleCommentStrategy{
TimeOfRun: time.Now(),
}
} else {
Expand Down
46 changes: 31 additions & 15 deletions libs/comment_utils/reporting/reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type CiReporter struct {
}

func (ciReporter CiReporter) Report(report string, reportFormatting func(report string) string) error {
_, _, err := ciReporter.ReportStrategy.Report(ciReporter.CiService, ciReporter.PrNumber, report, reportFormatting, ciReporter.SupportsMarkdown())
_, _, err := ciReporter.ReportStrategy.Report("", ciReporter.CiService, ciReporter.PrNumber, report, reportFormatting, ciReporter.SupportsMarkdown())
return err
}

Expand Down Expand Up @@ -53,16 +53,30 @@ func (reporter StdOutReporter) Suppress() error {
}

type ReportStrategy interface {
Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error)
Flush() (string, string, error)
Report(projectName string, report string, reportFormatter func(report string) string) (error error)
Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error)
}

type CommentPerRunStrategy struct {
Title string
TimeOfRun time.Time
type ReportFormat struct {
ReportTitle string
ReportFormatter func(report string) string
}

func (strategy CommentPerRunStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) {
type SingleCommentStrategy struct {
Title string
TimeOfRun time.Time
Formatters map[string]ReportFormat
}

func (strategy SingleCommentStrategy) Report(projectName string, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) error {
strategy.Formatters[projectName] = ReportFormat{
ReportTitle: report,
ReportFormatter: reportFormatter,
}
return nil
}

func (strategy SingleCommentStrategy) Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) (string, string, error) {
comments, err := ciService.GetComments(PrNumber)
if err != nil {
return "", "", fmt.Errorf("error getting comments: %v", err)
Expand All @@ -76,9 +90,6 @@ func (strategy CommentPerRunStrategy) Report(ciService ci.PullRequestService, Pr
}
commentId, commentUrl, err := upsertComment(ciService, PrNumber, report, reportFormatter, comments, reportTitle, supportsCollapsibleComment)
return commentId, commentUrl, err
}

func (s CommentPerRunStrategy) Flush() (string, string, error) {
return "", "", nil
}

Expand Down Expand Up @@ -132,13 +143,18 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string,
return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil
}

type MultipleCommentsStrategy struct{}
type MultipleCommentsStrategy struct {
Formatters map[string]ReportFormat
}

func (strategy MultipleCommentsStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) {
_, err := ciService.PublishComment(PrNumber, reportFormatter(report))
return "", "", err
func (strategy MultipleCommentsStrategy) Report(projectName string, report string, reportFormatter func(report string) string) error {
strategy.Formatters[projectName] = ReportFormat{
ReportTitle: report,
ReportFormatter: reportFormatter,
}
return nil
}

func (s MultipleCommentsStrategy) Flush() (string, string, error) {
func (strategy MultipleCommentsStrategy) Flush(ciService ci.PullRequestService, PrNumber int, supportsCollapsibleComment bool) (string, string, error) {
return "", "", nil
}
2 changes: 1 addition & 1 deletion libs/comment_utils/reporting/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func PostInitialSourceComments(ghService ci.PullRequestService, prNumber int, im
reporter := CiReporter{
PrNumber: prNumber,
CiService: ghService,
ReportStrategy: CommentPerRunStrategy{fmt.Sprintf("Report for location: %v", location), time.Now()},
ReportStrategy: SingleCommentStrategy{fmt.Sprintf("Report for location: %v", location), time.Now()},
}
err := reporter.Report("Comment Reporter", func(report string) string { return "" })
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion libs/spec/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c
getStrategy := func(strategy string) reporting.ReportStrategy {
switch reporterSpec.ReportingStrategy {
case "comments_per_run":
return reporting.CommentPerRunStrategy{
return reporting.SingleCommentStrategy{
Title: title,
TimeOfRun: time.Now(),
}
Expand Down

0 comments on commit 5a33821

Please sign in to comment.