0

I am trying to get quality gate status from sonar server using API by passing pull request ID but I am getting error:

$QualityGateResult = Invoke-RestMethod -Method Get -Uri 
"$ServerUrl/api/qualitygates/project_status?pullRequest=$pullrequest_key?api- 
  version=6.0" 
-Headers $Headers
 $QualityGateResult | ConvertTo-Json | Write-Host

  if ($QualityGateResult.projectStatus.status -eq "OK"){
  Write-Host "Quality Gate Succeeded"
   }
  else{
  throw "Quality gate failed. Please check and fix the issues by reviewing the same."
   }

Error msg: Invoke-RestMethod : {"errors":[{"msg":"Either \u0027analysisId\u0027, \u0027projectId\u0027 or \u0027projectKey\u0027 must be provided"}]}

Please help us to understand where I am making mistake.

Santosh Kumar
  • 53
  • 1
  • 10
  • 2
    The error seems pretty self-explanatory - provide the key or id for the project that hosts the pull request: `... -Uri "$ServerUrl/api/qualitygates/project_status?pullRequest=$pullrequest_key&projectKey=&api-version=6.0"` – Mathias R. Jessen May 06 '22 at 12:44
  • Yes it works fine, but I dont want to use projectkey. How can I achieve it only using pullrequestID? – Santosh Kumar May 06 '22 at 16:21
  • You can't - pull requests IDs aren't unique across different projects, so the ID alone is not enough to identify/target a distinct PR. If you're targeting an associated analysis pipeline, supply the associated `analysisId` instead a project identifier – Mathias R. Jessen May 06 '22 at 16:22
  • But how to use or pass analysisID to the Invoke rest-method, I mean before to this should we configure to fetch analysisID? if so how? – Santosh Kumar May 07 '22 at 15:25
  • Yeah correct, it worked when I used projectKey. But same how I can use it for shell script: `quality_gatesstatus=$(curl -u $sonar_token: https://$sonar_server/api/qualitygates/project_status?projectKey=$SONAR_PROJECT_KEY&pullRequest=$PullRequestId | grep -Po '"status": *\K"[^"]*"')` Is this correct? – Santosh Kumar May 10 '22 at 05:27

0 Answers0