Skip to content

Commit 84d75d8

Browse files
CCM-17846: Sonar PR Decoration Fix
1 parent ff786fb commit 84d75d8

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

.github/workflows/cicd-1-pull-request.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ on:
77
branches:
88
- "**"
99
pull_request:
10-
types: [opened, reopened]
10+
types: [opened, reopened, synchronize]
11+
branches:
12+
- main
1113

1214
jobs:
1315
metadata:

scripts/reports/perform-static-analysis.sh

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,50 @@ function main() {
3535

3636
function 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

4649
function 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

Comments
 (0)