Advice on getting PR number from github?

I’m hosting my code on Github and using AWS Codebuild for CI. I’m trying to run sonar-scanner to analyze a pull request using these cli arguments:


I’m populating PR_NUMBER with the following shell command:

export PR_NUMBER="$(git ls-remote ssh-origin 'pull/*/head' | grep -F $HEAD_REV | cut -d'/' -f3)"

Doing this will get the wrong PR number or get multiple in some circumstances.

Do you have any advice on how to get the correct PR number?


I am not very familiar with CodeBuild, but looking at the doc:

CODEBUILD_WEBHOOK_TRIGGER : Shows the webhook event that triggered the build. This variable is available only for builds triggered by a webhook. The value is parsed from the payload sent to CodeBuild by Github, Github Enterprise, or Bitbucket. The value’s format depends on what type of event triggered the build.

  • For builds triggered by a pull request, it is pr/pull-request-number .
    , you should have an environment variable called CODEBUILD_WEBHOOK_TRIGGER with the format pr/<pr_number> when the build is triggered from a Github Pull Request.

Extracting the pr number from this variable should do the job.



1 Like

Hello, this only works when you only use Github webhooks for creation of a new PR. When you are using webhooks to build every commit the trigger and head ref contain the branch information not the PR information.

is the intended usage only to analyze the code when a new PR is opened?

Hello, this method only works when receiving a webhook for opening a new PR. When receiving webhooks for commits to a repo they do not contain any information about the PR # which encompasses the branch.

Is the intended behavior of sonarcloud to only analyze the code when a new PR is opened and not on every commit? Thanks!