Sonarqube results display in azure pull request

887 Views Asked by At

I need a solution to display the results of sonar in the azure pull request.

I tried to do it with a status check by selecting the sonar pipeline in branch policy. It is showing success/fail and redirecting to sonar portal on click.

Is it really possible to show the actual results(vulnarabilities,duplications,etc.,) in the pull requets itself?

please help.

Thanks

1

There are 1 best solutions below

0
On

After got the result of Sonarqube, you could use DevOps REST API to update the result to Azure pull request.

The flow is : a new pull request created > trigger a pipeline > run REST API to update the pull request description or title.

Add a Powershell task in the pipeline with follow script to update the pull request description and title. You could also refer to the document above to update other properties of the pull request. Please pay attention to PAT, the result of Sonarqube, organization name, project name, repository ID. Here we could use $(System.PullRequest.PullRequestId) to get the pull request ID, thus, the build will fail if it was not triggered by pull request.

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $connectionToken="<PAT>"
      $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
      $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
      $headers.Add("Authorization", "Basic $base64AuthInfo")
      $headers.Add("Content-Type", "application/json")
      $body = '{"description": "<the result of Sonarqube>","title": "<the result of Sonarqube>"}'
      $response = Invoke-RestMethod 'https://dev.azure.com/<organization name>/<project name>/_apis/git/repositories/<repository ID>/pullrequests/$(System.PullRequest.PullRequestId)?api-version=5.0' -Method 'PATCH' -Headers $headers -Body $body
      $response | ConvertTo-Json

After configure the pipeline, please enable Build Validation for the branch in project setting >> repositories >> your repo >> policies >> branch >> Build Validation. Then, every time a new pull request created for that branch will trigger the pipeline. You could also find the repository ID in the URL.

enter image description here