reviewdog / reviewdog

🐶 Automated code review tool integrated with any code analysis tools regardless of programming language
https://medium.com/@haya14busa/reviewdog-a-code-review-dog-who-keeps-your-codebase-healthy-d957c471938b#.8xctbaw5u
MIT License
7.94k stars 422 forks source link

bitbucket has started giving 400 Bad Request (but still succeeds) #1285

Open wizardishungry opened 2 years ago

wizardishungry commented 2 years ago

In a fairly new installation of v0.14.1 that was working for a few days, we have started seeing consistent errors from reviewdog (for several days), but the code insights are still updated in bitbucket.

go run github.com/reviewdog/reviewdog/cmd/reviewdog/... -diff="git diff master" -reporter=bitbucket-code-report -filter-mode nofilter
2022/09/15 14:51:09 reviewdog: [start]  runner=golangci-lint
2022/09/15 14:52:34 reviewdog: [finish] runner=golangci-lint    error=exit status 1
reviewdog: failed to post annotations: failed to create code insights annotations: bitbucket Cloud API error: 400 Bad Request
exit status 1
pararang commented 2 years ago

Hi @wizardishungry , did you find out the solution for this issue?

I just stuck on the same error on my bitbucket pipeline. reviewdog: failed to post annotations: failed to create code insights annotations: bitbucket Cloud API error: 400 Bad Request

wizardishungry commented 2 years ago

@pararang We're still seeing it as well; I haven't had time to dig into the code yet.

kade-dentel commented 1 year ago

I was seeing this as well. In my case, the issue was that bitbucket doesn't accept annotations without a message. My linter was not reporting a message, so I had to use sed s/$/"; Code style issue."/ to add a generic message and also update my error format to include %m to capture the message.

Can we add a specific error when reviewdog cannot post an annotation because of a missing message?

From bb docs (summary is the same thing as message):

image

wizardishungry commented 1 year ago

I managed to get the underlying error message from Bitbucket:

{"key": "report-service.general.bad-request", "message": "The summary field cannot contain more than 450 characters.", "arguments": {}}

Suggestions as to how to fix this right now (by reviewdog configuration) would be welcomed, but it would also be helpful to be able to see underlying error messages.

CreateOrUpdateAnnotations in service/bitbucket/cloud_api_client.go:

    _, resp, err := c.cli.ReportsApi.
        BulkCreateOrUpdateAnnotations(ctx, req.Owner, req.Repository, req.Commit, req.ReportID).
        Body(c.helper.BuildAnnotations(req.Comments)).
        Execute()

It appears that this call returns the error in question and checkAPIError seeing as its error argument is not nil, declines to put the body of the error into the message. It would be helpful to see the body of error messages. I believe whatever error type is being returned by the OpenApi go-bitbucket library does satisfy interface{ Body() []byte }.

wizardishungry commented 1 year ago

I patched the code to truncate annotation summaries to 450 characters and now bitbucket is giving me:

{"key": "report-service.annotation.max-annotations", "message": "Too many annotations associated with report with ID /{58263add-5b41-51b5-ae37-e287ca31c86f}", "arguments": {}}
diff --git a/service/bitbucket/cloud_api_helper.go b/service/bitbucket/cloud_api_helper.go
index 62d366f..c46c816 100644
--- a/service/bitbucket/cloud_api_helper.go
+++ b/service/bitbucket/cloud_api_helper.go
@@ -35,10 +35,17 @@ func (c *CloudAPIHelper) BuildAnnotations(comments []*reviewdog.Comment) []bbapi
 }

 func (c *CloudAPIHelper) buildAnnotation(comment *reviewdog.Comment) bbapi.ReportAnnotation {
+
+       const MAX_SUMMARY_LENGTH = 450
+
        data := bbapi.NewReportAnnotation()
        data.SetExternalId(externalIDFromDiagnostic(comment.Result.Diagnostic))
        data.SetAnnotationType(annotationTypeCodeSmell)
-       data.SetSummary(comment.Result.Diagnostic.GetMessage())
+       summary := comment.Result.Diagnostic.GetMessage()
+       if len(summary) > MAX_SUMMARY_LENGTH {
+               summary = summary[:MAX_SUMMARY_LENGTH]
+       }
+       data.SetSummary(summary)
        data.SetDetails(fmt.Sprintf(`[%s] %s`, comment.ToolName, comment.Result.Diagnostic.GetMessage()))
        data.SetLine(comment.Result.Diagnostic.GetLocation().GetRange().GetStart().GetLine())
        data.SetPath(comment.Result.Diagnostic.GetLocation().GetPath())

I'm not convinced I'm exceeding the annotations limit (1000?) – something else may be at foot.

wizardishungry commented 1 year ago

https://developer.atlassian.com/server/bitbucket/how-tos/code-insights/

It is worth considering the behavior of the tool adding annotations to a report, as behavior such as re-running a build could result in duplicate annotations being created. In scenarios where this is an option we recommend that all the annotations are first deleted in bulk before new ones are created.

wizardishungry commented 1 year ago

Lookin at the reviewdog docs - it looks like bitbucket only supports nofilter - so if you exceed 1000 warnings, you will get an error.