@@ -35,29 +35,50 @@ function main() {
3535
3636function run-sonar-scanner-natively() {
3737
38+ local -a context_args
39+ context_args=($( build-sonar-context-args) )
40+
3841 sonar-scanner \
3942 -Dproject.settings=" $PWD /scripts/config/sonar-scanner.properties" \
40- -Dsonar.branch.name=" ${BRANCH_NAME:- $(git rev-parse --abbrev-ref HEAD)} " \
4143 -Dsonar.organization=" $SONAR_ORGANISATION_KEY " \
4244 -Dsonar.projectKey=" $SONAR_PROJECT_KEY " \
43- -Dsonar.token=" $SONAR_TOKEN "
45+ -Dsonar.token=" $SONAR_TOKEN " \
46+ " ${context_args[@]} "
4447}
4548
4649function run-sonar-scanner-in-docker() {
4750
4851 # shellcheck disable=SC1091
4952 source ./scripts/docker/docker.lib.sh
5053
54+ local -a context_args
55+ context_args=($( build-sonar-context-args) )
56+
5157 # shellcheck disable=SC2155
5258 local image=$( name=sonarsource/sonar-scanner-cli docker-get-image-version-and-pull)
5359 docker run --rm --platform linux/amd64 \
5460 --volume " $PWD " :/usr/src \
5561 " $image " \
5662 -Dproject.settings=/usr/src/scripts/config/sonar-scanner.properties \
57- -Dsonar.branch.name=" ${BRANCH_NAME:- $(git rev-parse --abbrev-ref HEAD)} " \
5863 -Dsonar.organization=" $SONAR_ORGANISATION_KEY " \
5964 -Dsonar.projectKey=" $SONAR_PROJECT_KEY " \
60- -Dsonar.token=" $SONAR_TOKEN "
65+ -Dsonar.token=" $SONAR_TOKEN " \
66+ " ${context_args[@]} "
67+ }
68+
69+ # ==============================================================================
70+
71+ function build-sonar-context-args() {
72+
73+ if [[ " ${GITHUB_EVENT_NAME:- } " == " pull_request" ]]; then
74+ local pr_number
75+ pr_number=$( echo " ${GITHUB_REF:- } " | grep -oP ' refs/pull/\K[0-9]+' )
76+ echo " -Dsonar.pullrequest.key=${pr_number} "
77+ echo " -Dsonar.pullrequest.branch=${GITHUB_HEAD_REF:- ${BRANCH_NAME} } "
78+ echo " -Dsonar.pullrequest.base=${GITHUB_BASE_REF:- main} "
79+ else
80+ echo " -Dsonar.branch.name=${BRANCH_NAME:- $(git rev-parse --abbrev-ref HEAD)} "
81+ fi
6182}
6283
6384# ==============================================================================
0 commit comments