From 4f41f6b2f1d325edbbff8f6e828e5375f70ec983 Mon Sep 17 00:00:00 2001 From: Lukasz Mierzwa Date: Tue, 16 Jul 2024 18:15:14 +0100 Subject: [PATCH] Truncate BB comments that are too long --- docs/changelog.md | 7 ++++ go.mod | 2 +- go.sum | 4 +- internal/reporter/bitbucket_api.go | 12 +++++- internal/reporter/bitbucket_comments_test.go | 44 ++++++++++++++++++++ internal/reporter/github.go | 2 +- 6 files changed, 66 insertions(+), 5 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 76f16ab4..13a55147 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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 diff --git a/go.mod b/go.mod index b536dab0..62abb63f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 473ccd74..5aac2cb5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/reporter/bitbucket_api.go b/internal/reporter/bitbucket_api.go index d920c9bf..8d2230b1 100644 --- a/internal/reporter/bitbucket_api.go +++ b/internal/reporter/bitbucket_api.go @@ -33,6 +33,8 @@ const ( NumberType DataType = "NUMBER" PercentageType DataType = "PERCENTAGE" TextType DataType = "TEXT" + + maxCommentLength = 32768 ) type BitBucketReportData struct { @@ -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)) diff --git a/internal/reporter/bitbucket_comments_test.go b/internal/reporter/bitbucket_comments_test.go index e73ebb7f..ec908269 100644 --- a/internal/reporter/bitbucket_comments_test.go +++ b/internal/reporter/bitbucket_comments_test.go @@ -3,6 +3,7 @@ package reporter import ( "fmt" "log/slog" + "strings" "testing" "time" @@ -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 { diff --git a/internal/reporter/github.go b/internal/reporter/github.go index 25b4e9cf..2ea62a70 100644 --- a/internal/reporter/github.go +++ b/internal/reporter/github.go @@ -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"