@@ -31,40 +31,68 @@ function main() {
3131 else
3232 run-sonar-scanner-in-docker
3333 fi
34+
35+ return 0
3436}
3537
3638function run-sonar-scanner-natively() {
3739
40+ local -a context_args
41+ context_args=($( build-sonar-context-args) )
42+
3843 sonar-scanner \
3944 -Dproject.settings=" $PWD /scripts/config/sonar-scanner.properties" \
40- -Dsonar.branch.name=" ${BRANCH_NAME:- $(git rev-parse --abbrev-ref HEAD)} " \
4145 -Dsonar.organization=" $SONAR_ORGANISATION_KEY " \
4246 -Dsonar.projectKey=" $SONAR_PROJECT_KEY " \
43- -Dsonar.token=" $SONAR_TOKEN "
47+ -Dsonar.token=" $SONAR_TOKEN " \
48+ " ${context_args[@]} "
49+
50+ return 0
4451}
4552
4653function run-sonar-scanner-in-docker() {
4754
4855 # shellcheck disable=SC1091
4956 source ./scripts/docker/docker.lib.sh
5057
58+ local -a context_args
59+ context_args=($( build-sonar-context-args) )
60+
5161 # shellcheck disable=SC2155
5262 local image=$( name=sonarsource/sonar-scanner-cli docker-get-image-version-and-pull)
5363 docker run --rm --platform linux/amd64 \
5464 --volume " $PWD " :/usr/src \
5565 " $image " \
5666 -Dproject.settings=/usr/src/scripts/config/sonar-scanner.properties \
57- -Dsonar.branch.name=" ${BRANCH_NAME:- $(git rev-parse --abbrev-ref HEAD)} " \
5867 -Dsonar.organization=" $SONAR_ORGANISATION_KEY " \
5968 -Dsonar.projectKey=" $SONAR_PROJECT_KEY " \
60- -Dsonar.token=" $SONAR_TOKEN "
69+ -Dsonar.token=" $SONAR_TOKEN " \
70+ " ${context_args[@]} "
71+
72+ return 0
73+ }
74+
75+ # ==============================================================================
76+
77+ function build-sonar-context-args() {
78+
79+ if [[ " ${GITHUB_EVENT_NAME:- } " == " pull_request" ]]; then
80+ local pr_number
81+ pr_number=$( echo " ${GITHUB_REF:- } " | grep -oP ' refs/pull/\K[0-9]+' )
82+ echo " -Dsonar.pullrequest.key=${pr_number} "
83+ echo " -Dsonar.pullrequest.branch=${GITHUB_HEAD_REF:- ${BRANCH_NAME} } "
84+ echo " -Dsonar.pullrequest.base=${GITHUB_BASE_REF:- main} "
85+ else
86+ echo " -Dsonar.branch.name=${BRANCH_NAME:- $(git rev-parse --abbrev-ref HEAD)} "
87+ fi
6188}
6289
6390# ==============================================================================
6491
6592function is-arg-true() {
93+ local arg=" $1 "
6694
67- if [[ " $1 " =~ ^(true| yes| y| on| 1| TRUE| YES| Y| ON)$ ]]; then
95+ if [[ " $arg " =~ ^(true| yes| y| on| 1| TRUE| YES| Y| ON)$ ]]; then
6896 return 0
6997 else
7098 return 1
0 commit comments