PR decoration fails most of the time on bitbucket

We use sonar for bitbucket server plugin and we are unable to decorate our PR’s. We are rarely able to since we upgraded sonar from 6.7 to 8.1. The analysis itself is performed in sonar and we see the results in sonar UI and even the stats show up fine in bitbucket PR overview page.

When we access the page in sonar UI, we see the following warning on top right. 99% of the time the decoration fails, but oddly a few random PR’s do have the code decorated and don’t see the warning.

“Pull request decoration did not happen. Failed to access Bitbucket Server, the repository or the pull request”

These are the current versions of our plugins.

Sonar version: 8.1 build 31237 - Developer Edition
BitBucket version: 6.4.0
Sonar For Bitbucket server: 2.6.16

Arguments we pass to sonarqube during the build since we analyze PR’s based on leak period. We do have branching and pull request support feature enabled since we use developer edition of sonar. We also have set up configuration correctly per https://docs.sonarqube.org/latest/analysis/pr-decoration/

-Dsonar.pullrequest.branch=$SONAR_BRANCH
-Dsonar.pullrequest.base=master
-Dsonar.pullrequest.key=$SONAR_PR_ID
-Dsonar.projectBaseDir=./

We checked the sonar community for similar issues and unable to find any results that match our issue.

Hi @viadev,

Welcome to our community !

It seems to me that you see the result of the Sonar for Bitbucket Server plugin, which is provided by an external provider.
Could you please send us a screenshot of the PR decoration that you see in BitBucket Server?

Do you see any errors in the ce.logs at server side ?

Thanks,

Cheers,

Yes, we do see the results in Bitbucket once the scan is successful but not the decoration. We have about 50 PR’s and none of them currently have PR decoration in bitbucket. If i find one that was successful, I’ll include the screenshot.

CE.logs on the server side just tell us the following

2020.02.11 07:41:54 WARN ce[AXA0RSZvdnSLw9TUPz9t][c.s.C.D.A.F] Pull request decoration did not happen. Failed to access Bitbucket Server, the repository or the pull request: Unable to contact Bitbucket server

Hi @viadev,

That’s odd. Normally, this error only appears if SonarQube really cannot communicate with Bitbucket Server in any way (if there’s a reason, like Personal Access Token is not valid, or the server returned a status code, it will add that information to this log line).

You see results in Bitbucket, but these are probably coming from the Sonar for Bitbucket Server plugin, right? Are you absolutely sure your configuration in SonarQube is correct?

  • Can you ping Bitbucket from the SonarQube server?
  • Can you make API requests from the SonarQube server using cURL?
  • Is the token valid (should appear in the logs, but you never know)?

I’m getting the same issue with 8.0 and it turned out to be a repository permissions issue (the user SQ was using needed to be added to the repo with READ permissions). There was no other log message to let me know, and in fact the following message said decoration was a success which only made things more confusing…

2020.04.01 19:02:35 INFO  ce[AXE3Hz8CfTKjftD1kskc][o.s.c.t.p.a.p.PostProjectAnalysisTasksExecutor] Webhooks | globalWebhooks=1 | projectWebhooks=0 | status=SUCCESS | time=48ms
2020.04.01 19:02:36 WARN  ce[AXE3Hz8CfTKjftD1kskc][c.s.C.C.A.G] Pull request decoration did not happen. Failed to access Bitbucket Server, the repository or the pull request: Unable to contact Bitbucket server
2020.04.01 19:02:36 INFO  ce[AXE3Hz8CfTKjftD1kskc][o.s.c.t.p.a.p.PostProjectAnalysisTasksExecutor] Pull Request decoration | status=SUCCESS | time=398ms

If you turn the log level to trace, you’ll finally see the error in the noise:

2020.04.01 18:52:49 DEBUG ce[AXE3FrJdfTKjftD1kskX][c.s.C.C.A.A.C] Unable to contact Bitbucket server: 401 {"errors":[{"context":null,"message":"You are not permitted to access this resource","exceptionName":"com.atlassian.bitbucket.AuthorisationException"}]}
2020.04.01 18:52:49 TRACE ce[AXE3FrJdfTKjftD1kskX][sql] time=1ms | sql=insert into ce_task_message ( uuid, task_uuid, message, created_at ) values ( ?, ?, ?, ? ) | params=AXE3Fr6gh5s01zK7KtGb, AXE3FrJdfTKjftD1kskX, Pull request decoration did not happen. Failed to access Bitbucket Server, the repository or the pull request, 1585767169696
2020.04.01 18:52:49 WARN  ce[AXE3FrJdfTKjftD1kskX][c.s.C.C.A.G] Pull request decoration did not happen. Failed to access Bitbucket Server, the repository or the pull request
java.lang.IllegalStateException: Unable to contact Bitbucket server
	at com.sonarsource.C.C.A.A.C.E(Unknown Source)
	at com.sonarsource.C.C.A.A.C.A(Unknown Source)
	at com.sonarsource.C.C.A.A.C.A(Unknown Source)
	at com.sonarsource.C.C.A.A.C.A(Unknown Source)
	at com.sonarsource.C.C.A.G.A(Unknown Source)
	at com.sonarsource.C.C.A.G.A(Unknown Source)
	at com.sonarsource.C.C.A.C.A(Unknown Source)
	at com.sonarsource.C.C.Z.A(Unknown Source)
	at java.base/java.util.Optional.ifPresent(Unknown Source)
	at com.sonarsource.C.C.Z.B(Unknown Source)
	at com.sonarsource.C.C.Z.A(Unknown Source)
	at org.sonar.ce.async.SynchronousAsyncExecution.addToQueue(SynchronousAsyncExecution.java:27)
	at com.sonarsource.C.C.Z.A(Unknown Source)
	at java.base/java.util.Optional.ifPresent(Unknown Source)
	at com.sonarsource.C.C.Z.finished(Unknown Source)
	at org.sonar.ce.task.projectanalysis.api.posttask.PostProjectAnalysisTasksExecutor.executeTask(PostProjectAnalysisTasksExecutor.java:118)
	at org.sonar.ce.task.projectanalysis.api.posttask.PostProjectAnalysisTasksExecutor.finished(PostProjectAnalysisTasksExecutor.java:109)
	at org.sonar.ce.task.step.ComputationStepExecutor.executeListener(ComputationStepExecutor.java:91)
	at org.sonar.ce.task.step.ComputationStepExecutor.execute(ComputationStepExecutor.java:63)
	at org.sonar.ce.task.projectanalysis.taskprocessor.ReportTaskProcessor.process(ReportTaskProcessor.java:81)
	at org.sonar.ce.taskprocessor.CeWorkerImpl$ExecuteTask.executeTask(CeWorkerImpl.java:209)
	at org.sonar.ce.taskprocessor.CeWorkerImpl$ExecuteTask.run(CeWorkerImpl.java:191)
	at org.sonar.ce.taskprocessor.CeWorkerImpl.findAndProcessTask(CeWorkerImpl.java:158)
	at org.sonar.ce.taskprocessor.CeWorkerImpl$TrackRunningState.get(CeWorkerImpl.java:133)
	at org.sonar.ce.taskprocessor.CeWorkerImpl.call(CeWorkerImpl.java:85)
	at org.sonar.ce.taskprocessor.CeWorkerImpl.call(CeWorkerImpl.java:53)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
	at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at java.base/java.lang.Thread.run(Unknown Source)
2020.04.01 18:52:49 TRACE ce[AXE3FrJdfTKjftD1kskX][sql] time=1ms | sql=update project_branches set merge_branch_uuid = ?, pull_request_binary = ?, updated_at = ? where uuid = ? | params=AXE2xGq4fTKjftD1ksjh, [B@131982ff, 1585767169703, AXE3C7SufTKjftD1kskJ
2020.04.01 18:52:49 INFO  ce[AXE3FrJdfTKjftD1kskX][o.s.c.t.p.a.p.PostProjectAnalysisTasksExecutor] Pull Request decoration | status=SUCCESS | time=408ms

Hi @Wouter_Admiraal , Yes, we doubled/triple checked the configuration.

  • Can you ping Bitbucket from the SonarQube server?
    YES

  • Can you make API requests from the SonarQube server using cURL?
    YES

  • Is the token valid (should appear in the logs, but you never know)?
    YES, we created a new token from bitbucket and tried it as well. We are able to communicate with bitbucket just fine.

We improved the error handling between Bitbucket Server and SonarQube in SonarQube 8.2 (forwarding the BitBucket Server error to the SonarQube user). Could you upgrade to SonarQube 8.2, retry, and see if there is more information in the logs ?

@pierreguillot, Yes, we will try that and paste the full trace from the logs.

@pierreguillot I updated to 8.2 and the PR decoration works partially. I see issues decorated properly but not code coverage. Lines are not marked red/green which used to indicate coverage. It is properly highlighted when I go to the sonar UI dashboard and I get the coverage % correctly in bitbucket (sonar widget next to the PR info).

How do i enable PR decoration for code coverage?

Hi @viadev,

This is not available for the native integration with SonarQube, you can only see the coverage metric in the SonarQube Widget on BitBucket Server side.
You can see the lines coverage in SonarQube.

Cheers

@aurelie, Thank you. That makes sense. We have now resolved this issue.

It turns out that developers here have personal forks where they create PR’s to upstream. This was messing with the sonar for bitbucket plugin and following instructions here https://mibexsoftware.atlassian.net/wiki/spaces/SONAR4STASH/pages/398196737/Pull+request+analysis+with+a+fork-based+Git+workflow helped resolve that particular issue.

Thank you for looking at this.