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

Dedup bitbucket comments #790

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
pull requests. Changes that would previously be invisible to pint, like modifying
comments or moving the entire rules file, will now trigger checks for affected
rules.
- pint will now try to create fewer BitBucket comments by merging multiple
problem reports into a single comment.

## v0.49.2

Expand Down
74 changes: 54 additions & 20 deletions internal/reporter/bitbucket_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,14 +540,14 @@ func (bb bitBucketAPI) getPullRequestComments(pr *bitBucketPR) ([]bitBucketComme

func (bb bitBucketAPI) makeComments(summary Summary, changes *bitBucketPRChanges) []BitBucketPendingComment {
comments := []BitBucketPendingComment{}
for _, report := range summary.reports {
if _, ok := changes.pathModifiedLines[report.ReportedPath]; !ok {
for _, reports := range dedupReports(summary.reports) {
if _, ok := changes.pathModifiedLines[reports[0].ReportedPath]; !ok {
continue
}

var buf strings.Builder
var icon string
switch report.Problem.Severity {
switch reports[0].Problem.Severity {
case checks.Fatal, checks.Bug:
icon = ":stop_sign:"
case checks.Warning:
Expand All @@ -557,30 +557,33 @@ func (bb bitBucketAPI) makeComments(summary Summary, changes *bitBucketPRChanges
}
buf.WriteString(icon)
buf.WriteString(" **")
buf.WriteString(report.Problem.Severity.String())
buf.WriteString(reports[0].Problem.Severity.String())
buf.WriteString("** reported by [pint](https://cloudflare.github.io/pint/) **")
buf.WriteString(report.Problem.Reporter)
buf.WriteString("** check.")
buf.WriteString("\n\n------\n\n")
buf.WriteString(report.Problem.Text)
if report.Problem.Details != "" {
buf.WriteString(reports[0].Problem.Reporter)
buf.WriteString("** check.\n\n")
for _, report := range reports {
buf.WriteString("------\n\n")
buf.WriteString(report.Problem.Text)
buf.WriteString("\n\n")
buf.WriteString(report.Problem.Details)
}
buf.WriteString("\n\n------\n\n")
if report.ReportedPath != report.SourcePath {
buf.WriteString(":leftwards_arrow_with_hook: This problem was detected on a symlinked file ")
buf.WriteRune('`')
buf.WriteString(report.SourcePath)
buf.WriteString("`.\n\n")
if report.Problem.Details != "" {
buf.WriteString(report.Problem.Details)
buf.WriteString("\n\n")
}
if report.ReportedPath != report.SourcePath {
buf.WriteString(":leftwards_arrow_with_hook: This problem was detected on a symlinked file ")
buf.WriteRune('`')
buf.WriteString(report.SourcePath)
buf.WriteString("`.\n\n")
}
}
buf.WriteString("------\n\n")
buf.WriteString(":information_source: To see documentation covering this check and instructions on how to resolve it [click here](https://cloudflare.github.io/pint/checks/")
buf.WriteString(report.Problem.Reporter)
buf.WriteString(reports[0].Problem.Reporter)
buf.WriteString(".html).\n")

pending := pendingComment{
path: report.ReportedPath,
line: report.Problem.Lines[0],
path: reports[0].ReportedPath,
line: reports[0].Problem.Lines[0],
text: buf.String(),
}
comments = append(comments, pending.toBitBucketComment(changes))
Expand Down Expand Up @@ -702,3 +705,34 @@ func reportToAnnotation(report Report) BitBucketAnnotation {
Link: fmt.Sprintf("https://cloudflare.github.io/pint/checks/%s.html", report.Problem.Reporter),
}
}

func dedupReports(src []Report) (dst [][]Report) {
for _, report := range src {
index := -1
for i, d := range dst {
if d[0].Problem.Severity != report.Problem.Severity {
continue
}
if d[0].Problem.Reporter != report.Problem.Reporter {
continue
}
if d[0].ReportedPath != report.ReportedPath {
continue
}
if d[0].SourcePath != report.SourcePath {
continue
}
if d[0].Problem.Lines[0] != report.Problem.Lines[0] {
continue
}
index = i
break
}
if index < 0 {
dst = append(dst, []Report{report})
continue
}
dst[index] = append(dst[index], report)
}
return dst
}
Loading
Loading