Skip to content

Commit

Permalink
Merge pull request #1032 from cloudflare/maxCommentsLength
Browse files Browse the repository at this point in the history
Truncate BB comments that are too long
  • Loading branch information
prymitive authored Jul 16, 2024
2 parents a3e3afc + 4f41f6b commit ed582ef
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.62.2

### Fixed

- When using BitBucket reporter `pint ci` might create a comment longer than the limit allowed by BitBucket.
To avoid this pint will now truncate long comments.

## v0.62.1

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/fatih/color v1.17.0
github.com/gkampitakis/go-snaps v0.5.4
github.com/google/go-cmp v0.6.0
github.com/google/go-github/v62 v62.0.0
github.com/google/go-github/v63 v63.0.0
github.com/hashicorp/hcl/v2 v2.21.0
github.com/klauspost/compress v1.17.9
github.com/neilotoole/slogt v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v62 v62.0.0 h1:/6mGCaRywZz9MuHyw9gD1CwsbmBX8GWsbFkwMmHdhl4=
github.com/google/go-github/v62 v62.0.0/go.mod h1:EMxeUqGJq2xRu9DYBMwel/mr7kZrzUOfQmmpYrZn2a4=
github.com/google/go-github/v63 v63.0.0 h1:13xwK/wk9alSokujB9lJkuzdmQuVn2QCPeck76wR3nE=
github.com/google/go-github/v63 v63.0.0/go.mod h1:IqbcrgUmIcEaioWrGYei/09o+ge5vhffGOcxrO0AfmA=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand Down
12 changes: 11 additions & 1 deletion internal/reporter/bitbucket_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
NumberType DataType = "NUMBER"
PercentageType DataType = "PERCENTAGE"
TextType DataType = "TEXT"

maxCommentLength = 32768
)

type BitBucketReportData struct {
Expand Down Expand Up @@ -639,11 +641,19 @@ func (bb bitBucketAPI) makeComments(summary Summary, changes *bitBucketPRChanges
severity = "NORMAL"
}

var text string
// BitBucket has a max comment length limit. If we hit it then truncate the comment.
if buf.Len() > maxCommentLength {
text = buf.String()[:maxCommentLength-4] + " ..."
} else {
text = buf.String()
}

pending := pendingComment{
severity: severity,
path: reports[0].Path.SymlinkTarget,
line: reports[0].Problem.Lines.Last,
text: buf.String(),
text: text,
anchor: reports[0].Problem.Anchor,
}
comments = append(comments, pending.toBitBucketComment(changes))
Expand Down
44 changes: 44 additions & 0 deletions internal/reporter/bitbucket_comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reporter
import (
"fmt"
"log/slog"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -563,6 +564,49 @@ func TestBitBucketMakeComments(t *testing.T) {
},
},
},
{
description: "truncate long comments",
maxComments: 2,
summary: Summary{reports: []Report{
{
Path: discovery.Path{
SymlinkTarget: "rule.yaml",
Name: "rule.yaml",
},
ModifiedLines: []int{2, 3},
Problem: checks.Problem{
Severity: checks.Bug,
Lines: parser.LineRange{
First: 2,
Last: 2,
},
Text: strings.Repeat("X", maxCommentLength+1),
Reporter: "r1",
},
},
}},
changes: &bitBucketPRChanges{
pathModifiedLines: map[string][]int{
"rule.yaml": {2, 3},
},
pathLineMapping: map[string]map[int]int{
"rule.yaml": {2: 2, 3: 3},
},
},
comments: []BitBucketPendingComment{
{
Text: ":stop_sign: **Bug** reported by [pint](https://cloudflare.github.io/pint/) **r1** check.\n\n------\n\n" + strings.Repeat("X", maxCommentLength-98-4) + " ...",
Severity: "BLOCKER",
Anchor: BitBucketPendingCommentAnchor{
Path: "rule.yaml",
Line: 2,
LineType: "ADDED",
FileType: "TO",
DiffType: "EFFECTIVE",
},
},
},
},
}

for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion internal/reporter/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"time"

"github.com/google/go-github/v62/github"
"github.com/google/go-github/v63/github"
"golang.org/x/oauth2"

"github.com/cloudflare/pint/internal/checks"
Expand Down

0 comments on commit ed582ef

Please sign in to comment.