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:

-Dsonar.pullrequest.branch=$BUILD_NAME
-Dsonar.pullrequest.key=$PR_NUMBER

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?

Hi,

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.

HTH,

Benoit

1 Like