Skip to content

Commit

Permalink
Optimize analysis script
Browse files Browse the repository at this point in the history
  • Loading branch information
vnaskos-sonar committed Nov 29, 2024
1 parent 56f107b commit bf4e0f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .cirrus.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ build_task:
ec2_instance: *INSTANCE_DEFINITION
only_if: *ONLY_SONARSOURCE_QA
env:
CIRRUS_CLONE_DEPTH: 50
ARTIFACTORY_DEPLOY_TOKEN: VAULT[development/artifactory/token/${CIRRUS_REPO_OWNER}-${CIRRUS_REPO_NAME}-qa-deployer access_token]
COVERAGE_FILE: coverage.xml
LOCALAPPDATA: C:\sonar-ci\AppData\Local # Fixes unit test error
Expand Down Expand Up @@ -55,6 +54,7 @@ build_task:
git clone --recursive https://x-access-token:${CIRRUS_REPO_CLONE_TOKEN}@github.com/${CIRRUS_REPO_FULL_NAME}.git $CIRRUS_WORKING_DIR
git fetch origin pull/$CIRRUS_PR/head:pull/$CIRRUS_PR
git checkout pull/$CIRRUS_PR
git reset --hard $CIRRUS_CHANGE_IN_REPO
fi
calculate_version_script: *CALCULATE_VERSION_SCRIPT_DEFINITION
prepare_signing_script: |
Expand Down
64 changes: 16 additions & 48 deletions .cirrus/sonarqube-cloud-analysis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,33 @@

set -xeuo pipefail

# Fetch all commit history so that SonarCloud has exact blame information
# for issue auto-assignment
# This command can fail with "fatal: --unshallow on a complete repository does not make sense"
# if there are not enough commits in the Git repository
# For this reason errors are ignored with "|| true"
git fetch --unshallow || true

# fetch references from github for PR analysis
if [ -n "${CIRRUS_BASE_BRANCH}" ]; then
git fetch origin "${CIRRUS_BASE_BRANCH}"
fi
SONAR_PARAMS=(
-k:"${CIRRUS_REPO_NAME}"
-o:"sonarsource"
-d:sonar.host.url="${SONAR_URL}"
-d:sonar.token="${SONAR_TOKEN}"
-d:sonar.analysis.buildNumber="${CI_BUILD_NUMBER}"
-d:sonar.analysis.pipeline="$CIRRUS_BUILD_ID"
-d:sonar.analysis.sha1="${CIRRUS_CHANGE_IN_REPO}"
-d:sonar.cs.vscoveragexml.reportsPaths="${COVERAGE_FILE}"
-d:sonar.scanner.scanAll=false
)

if [ "$CIRRUS_BRANCH" == "master" ] && [ "$CIRRUS_PR" == "false" ]; then
echo '======= Analyze master branch'

dotnet sonarscanner begin \
-k:"${CIRRUS_REPO_NAME}" \
-o:"sonarsource" \
-d:sonar.host.url="${SONAR_URL}" \
-d:sonar.token=${SONAR_TOKEN} \
-d:sonar.analysis.buildNumber="${CI_BUILD_NUMBER}" \
-d:sonar.analysis.pipeline="$CIRRUS_BUILD_ID" \
-d:sonar.analysis.sha1="${CIRRUS_CHANGE_IN_REPO}" \
-d:sonar.cs.vscoveragexml.reportsPaths="${COVERAGE_FILE}" \
-d:sonar.scanner.scanAll=false
dotnet sonarscanner begin "${SONAR_PARAMS[@]}"

elif [[ "$CIRRUS_BRANCH" == "branch-"* || "$CIRRUS_BRANCH" == "feature/"* ]] && [ "$CIRRUS_PR" == "false" ]; then
echo '======= Analyze long lived branch'

dotnet sonarscanner begin \
-k:"${CIRRUS_REPO_NAME}" \
-o:"sonarsource" \
-d:sonar.host.url="${SONAR_URL}" \
-d:sonar.token=${SONAR_TOKEN} \
-d:sonar.analysis.buildNumber="${CI_BUILD_NUMBER}" \
-d:sonar.analysis.pipeline="$CIRRUS_BUILD_ID" \
-d:sonar.analysis.sha1="${CIRRUS_CHANGE_IN_REPO}" \
-d:sonar.branch.name="${CIRRUS_BRANCH}" \
-d:sonar.cs.vscoveragexml.reportsPaths="${COVERAGE_FILE}" \
-d:sonar.scanner.scanAll=false
dotnet sonarscanner begin "${SONAR_PARAMS[@]}" -d:sonar.branch.name="${CIRRUS_BRANCH}"

elif [ "$CIRRUS_PR" != "false" ]; then
echo '======= Analyze pull request'

dotnet sonarscanner begin \
-k:"${CIRRUS_REPO_NAME}" \
-o:"sonarsource" \
-d:sonar.host.url="${SONAR_URL}" \
-d:sonar.token=${SONAR_TOKEN} \
-d:sonar.analysis.buildNumber="${CI_BUILD_NUMBER}" \
-d:sonar.analysis.pipeline="$CIRRUS_BUILD_ID" \
-d:sonar.analysis.sha1="${CIRRUS_CHANGE_IN_REPO}" \
dotnet sonarscanner begin "${SONAR_PARAMS[@]}" \
-d:sonar.pullrequest.key="${CIRRUS_PR}" \
-d:sonar.pullrequest.branch="${CIRRUS_BRANCH}" \
-d:sonar.pullrequest.base="${CIRRUS_BASE_BRANCH}" \
-d:sonar.cs.vscoveragexml.reportsPaths="${COVERAGE_FILE}" \
-d:sonar.scanner.scanAll=false \
-d:sonar.verbose=true
-d:sonar.pullrequest.base="${CIRRUS_BASE_BRANCH}"

else
echo '======= No analysis'
fi
fi

0 comments on commit bf4e0f1

Please sign in to comment.