forked from nhs-england-tools/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathperform-static-analysis.sh
More file actions
executable file
·80 lines (61 loc) · 2.38 KB
/
Copy pathperform-static-analysis.sh
File metadata and controls
executable file
·80 lines (61 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/bash
# WARNING: Please, DO NOT edit this file! It is maintained in the Repository Template (https://github.com/nhs-england-tools/repository-template). Raise a PR instead.
set -euo pipefail
# Script to perform static analysis of the repository content and upload the
# report to SonarCloud.
#
# Usage:
# $ [options] ./perform-static-analysis.sh
#
# Expects:
# BRANCH_NAME=branch-name # Branch to report on
# SONAR_ORGANISATION_KEY=org-key # SonarCloud organisation key
# SONAR_PROJECT_KEY=project-key # SonarCloud project key
# SONAR_TOKEN=token # SonarCloud token
#
# Options:
# FORCE_USE_DOCKER=true # If set to true the command is run in a Docker container, default is 'false'
# VERBOSE=true # Show all the executed commands, default is 'false'
# ==============================================================================
function main() {
cd "$(git rev-parse --show-toplevel)"
if command -v sonar-scanner > /dev/null 2>&1 && ! is-arg-true "${FORCE_USE_DOCKER:-false}"; then
run-sonar-scanner-natively
else
run-sonar-scanner-in-docker
fi
}
function run-sonar-scanner-natively() {
sonar-scanner \
-Dproject.settings="$PWD/scripts/config/sonar-scanner.properties" \
-Dsonar.branch.name="${BRANCH_NAME:-$(git rev-parse --abbrev-ref HEAD)}" \
-Dsonar.organization="$SONAR_ORGANISATION_KEY" \
-Dsonar.projectKey="$SONAR_PROJECT_KEY" \
-Dsonar.token="$SONAR_TOKEN"
}
function run-sonar-scanner-in-docker() {
# shellcheck disable=SC1091
source ./scripts/docker/docker.lib.sh
# shellcheck disable=SC2155
local image=$(name=sonarsource/sonar-scanner-cli docker-get-image-version-and-pull)
docker run --rm --platform linux/amd64 \
--volume "$PWD":/usr/src \
"$image" \
-Dproject.settings=/usr/src/scripts/config/sonar-scanner.properties \
-Dsonar.branch.name="${BRANCH_NAME:-$(git rev-parse --abbrev-ref HEAD)}" \
-Dsonar.organization="$SONAR_ORGANISATION_KEY" \
-Dsonar.projectKey="$SONAR_PROJECT_KEY" \
-Dsonar.token="$SONAR_TOKEN"
}
# ==============================================================================
function is-arg-true() {
if [[ "$1" =~ ^(true|yes|y|on|1|TRUE|YES|Y|ON)$ ]]; then
return 0
else
return 1
fi
}
# ==============================================================================
is-arg-true "${VERBOSE:-false}" && set -x
main "$@"
exit 0