|
| 1 | +#!/bin/sh -e |
| 2 | + |
| 3 | +# ---------------------------------- |
| 4 | +# Colors |
| 5 | +# ---------------------------------- |
| 6 | +NOCOLOR='\033[0m' |
| 7 | +RED='\033[0;31m' |
| 8 | +GREEN='\033[0;32m' |
| 9 | +ORANGE='\033[0;33m' |
| 10 | + |
| 11 | +# Function to get value from a property file |
| 12 | +# arg 1 = the property |
| 13 | +# arg 2 = the file path |
| 14 | +function prop { |
| 15 | + grep "${1}" ${2} | cut -d'=' -f2 |
| 16 | +} |
| 17 | + |
| 18 | +# Configure sonar-scanner |
| 19 | +export SONAR_HOST_URL="http://sonarqube:9000" |
| 20 | +export SONAR_ADMIN_LOGIN="admin" |
| 21 | +export SONAR_ADMIN_PWD="admin" |
| 22 | + |
| 23 | +# Generate Analysis token |
| 24 | +echo "Generating analysis token..." |
| 25 | +# Use an UUID for token name. It's useful to launch the audit several time on the same SonarQube execution |
| 26 | +uuid=$(cat /proc/sys/kernel/random/uuid) |
| 27 | +export SONAR_TOKEN=$(curl -su "$SONAR_ADMIN_LOGIN:$SONAR_ADMIN_PWD" -XPOST "$SONAR_HOST_URL/api/user_tokens/generate?name=$uuid&type=GLOBAL_ANALYSIS_TOKEN" | jq -r '.token') |
| 28 | +echo $SONAR_TOKEN |
| 29 | +# Audit code |
| 30 | +echo "Launching scanner..." |
| 31 | +cd /usr/src/myapp/it |
| 32 | +sonar-scanner -X -Dsonar.qualitygate.wait 2>&1 | tee /tmp/scanner.log |
| 33 | + |
| 34 | +if [ $? -ne 0 ] |
| 35 | +then |
| 36 | + echo "${RED}Error scanning Shell scripts${NOCOLOR}" >&2 |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +# Check for warnings |
| 41 | +if grep -q "^WARN: " /tmp/scanner.log |
| 42 | +then |
| 43 | + echo -e "${ORANGE}Warnings found ${NOCOLOR}" >&2 |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +# Sleep a little because SonarQube needs some time to ingest the audit results |
| 48 | +sleep 10 |
| 49 | + |
| 50 | +export SONAR_PROJECT_KEY=$(prop 'sonar.projectKey' sonar-project.properties) |
| 51 | +echo "SONAR_PROJECT_KEY: $SONAR_PROJECT_KEY" |
| 52 | + |
| 53 | +# Check audit result |
| 54 | +echo "Checking result..." |
| 55 | +python3 << EOF |
| 56 | +from __future__ import print_function |
| 57 | +import requests |
| 58 | +import sys |
| 59 | +
|
| 60 | +r = requests.get('http://sonarqube:9000/api/issues/search?componentKeys=$SONAR_PROJECT_KEY:src/clanhb.f&statuses=OPEN', auth=('$SONAR_ADMIN_LOGIN', '$SONAR_ADMIN_PWD')) |
| 61 | +if r.status_code != 200: |
| 62 | + print('Invalid server response: ' + str(r.status_code), file=sys.stderr) |
| 63 | + sys.exit(1) |
| 64 | +
|
| 65 | +data = r.json() |
| 66 | +
|
| 67 | +if data['total'] != 100: |
| 68 | + print('Wrong total number of issues: ' + str(data['total']), file=sys.stderr) |
| 69 | + sys.exit(1) |
| 70 | +
|
| 71 | +issues = 0 |
| 72 | +if 'f77-rules' in data['issues'][0]['rule'] and data['issues'][0]['line'] == 1: |
| 73 | + issues += 1 |
| 74 | +
|
| 75 | +r = requests.get('http://sonarqube:9000/api/issues/search?componentKeys=$SONAR_PROJECT_KEY:src/clanhb.f90&statuses=OPEN', auth=('$SONAR_ADMIN_LOGIN', '$SONAR_ADMIN_PWD')) |
| 76 | +if r.status_code != 200: |
| 77 | + print('Invalid server response: ' + str(r.status_code), file=sys.stderr) |
| 78 | + sys.exit(1) |
| 79 | +
|
| 80 | +data = r.json() |
| 81 | +
|
| 82 | +if data['total'] != 197: |
| 83 | + print('Wrong total number of issues: ' + str(data['total']), file=sys.stderr) |
| 84 | + sys.exit(1) |
| 85 | +if 'f90-rules' in data['issues'][0]['rule'] and data['issues'][0]['line'] == 1: |
| 86 | + issues += 1 |
| 87 | +
|
| 88 | +
|
| 89 | +sys.exit(0 if issues == 2 else 1) |
| 90 | +EOF |
0 commit comments