Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: support single comment summary with orchestrator mode #1850

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
revert upsert function, remove lazyreporter
  • Loading branch information
motatoes committed Dec 19, 2024
commit ffa6e556ee97d2e7103058de9293a69660f5c8db
118 changes: 9 additions & 109 deletions libs/comment_utils/reporting/reporting.go
Original file line number Diff line number Diff line change
@@ -33,57 +33,6 @@ func (ciReporter CiReporter) SupportsMarkdown() bool {
return ciReporter.IsSupportMarkdown
}

type CiReporterLazy struct {
CiReporter CiReporter
isSuppressed bool
reports []string
formatters []func(report string) string
}

func NewCiReporterLazy(ciReporter CiReporter) *CiReporterLazy {
return &CiReporterLazy{
CiReporter: ciReporter,
isSuppressed: false,
reports: []string{},
formatters: []func(report string) string{},
}
}

func (lazyReporter *CiReporterLazy) Report(report string, reportFormatting func(report string) string) (string, string, error) {
lazyReporter.reports = append(lazyReporter.reports, report)
lazyReporter.formatters = append(lazyReporter.formatters, reportFormatting)
return "", "", nil
}

func (lazyReporter *CiReporterLazy) Flush() (string, string, error) {
if lazyReporter.isSuppressed {
log.Printf("Reporter is suprresed, ignoring messages ...")
return "", "", nil
}
var commentId, commentUrl string
for i, _ := range lazyReporter.formatters {
var err error
commentId, commentUrl, err = lazyReporter.CiReporter.ReportStrategy.Report(lazyReporter.CiReporter.CiService, lazyReporter.CiReporter.PrNumber, lazyReporter.reports[i], lazyReporter.formatters[i], lazyReporter.SupportsMarkdown())
if err != nil {
log.Printf("failed to report strategy: ")
return "", "", err
}
}
// clear the buffers
lazyReporter.formatters = []func(comment string) string{}
lazyReporter.reports = []string{}
return commentId, commentUrl, nil
}

func (lazyReporter *CiReporterLazy) Suppress() error {
lazyReporter.isSuppressed = true
return nil
}

func (lazyReporter *CiReporterLazy) SupportsMarkdown() bool {
return lazyReporter.CiReporter.IsSupportMarkdown
}

type StdOutReporter struct{}

func (reporter StdOutReporter) Report(report string, reportFormatting func(report string) string) (string, string, error) {
@@ -107,42 +56,6 @@ type ReportStrategy interface {
Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (commentId string, commentUrl string, error error)
}

type AlwaysSameCommentStrategy struct {
Title string
CommentId string
TimeOfRun time.Time
}

func (strategy AlwaysSameCommentStrategy) Report(ciService ci.PullRequestService, PrNumber int, report string, reportFormatter func(report string) string, supportsCollapsibleComment bool) (string, string, error) {
comments, err := ciService.GetComments(PrNumber)
if err != nil {
return "", "", fmt.Errorf("error getting comments: %v", err)
}

var commentBody *string
var commentUrl string
for _, comment := range comments {
if comment.Id == strategy.CommentId {
commentBody = comment.Body
commentUrl = comment.Url
}
}

var reportTitle string
if strategy.Title != "" {
reportTitle = strategy.Title + " " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")
} else {
reportTitle = "Digger run report at " + strategy.TimeOfRun.Format("2006-01-02 15:04:05 (MST)")
}

err = appendToExistingComment(ciService, PrNumber, *commentBody, report, reportTitle, strategy.CommentId, supportsCollapsibleComment)
if err != nil {
return "", "", fmt.Errorf("error while adding to existing comment: %v", err)
}

return strategy.CommentId, commentUrl, err
}

type CommentPerRunStrategy struct {
Title string
TimeOfRun time.Time
@@ -192,39 +105,26 @@ func upsertComment(ciService ci.PullRequestService, PrNumber int, report string,
return fmt.Sprintf("%v", comment.Id), comment.Url, nil
}

err := appendToExistingComment(ciService, PrNumber, commentBody, report, reportTitle, commentIdForThisRun, supportsCollapsible)
if err != nil {
return "", "", err
}

return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil

}

func appendToExistingComment(ciService ci.PullRequestService, prNumber int, existingCommentBody string, reportToAppend string, reportTitle string, commentId string, supportsCollapsible bool) error {

// strip first and last lines
lines := strings.Split(existingCommentBody, "\n")
//if len(lines) > 1 {
// lines = lines[1 : len(lines)-1]
//}
existingCommentBody = strings.Join(lines, "\n")
lines := strings.Split(commentBody, "\n")
lines = lines[1 : len(lines)-1]
commentBody = strings.Join(lines, "\n")

existingCommentBody = existingCommentBody + "\n\n" + reportToAppend + "\n"
commentBody = commentBody + "\n\n" + report + "\n"

var completeComment string
if !supportsCollapsible {
completeComment = utils.AsComment(reportTitle)(existingCommentBody)
completeComment = utils.AsComment(reportTitle)(commentBody)
} else {
completeComment = utils.AsCollapsibleComment(reportTitle, false)(existingCommentBody)
completeComment = utils.AsCollapsibleComment(reportTitle, false)(commentBody)
}

err := ciService.EditComment(prNumber, commentId, completeComment)
err := ciService.EditComment(PrNumber, commentIdForThisRun, completeComment)

if err != nil {
return fmt.Errorf("error editing comment: %v", err)
return "", "", fmt.Errorf("error editing comment: %v", err)
}
return nil
return fmt.Sprintf("%v", commentIdForThisRun), commentUrl, nil
}

type LatestRunCommentStrategy struct {
2 changes: 1 addition & 1 deletion libs/spec/providers.go
Original file line number Diff line number Diff line change
@@ -156,7 +156,7 @@ func (r ReporterProvider) GetReporter(title string, reporterSpec ReporterSpec, c
IsSupportMarkdown: true,
ReportStrategy: strategy,
}
return reporting.NewCiReporterLazy(ciReporter), nil
return ciReporter, nil
default:
return reporting.NoopReporter{}, nil
}
Loading
Oops, something went wrong.