|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# IRIS: Issue Replication for Sonarqube |
| 6 | +IRIS_JAR_URL="${ARTIFACTORY_URL}/sonarsource-private-releases/com/sonarsource/iris/iris/\[RELEASE\]/iris-\[RELEASE\]-jar-with-dependencies.jar" |
| 7 | +IRIS_JAR_PATH="target/libs/iris.jar" |
| 8 | + |
| 9 | +function build_and_analyze_the_project() { |
| 10 | + echo |
| 11 | + echo "===== Build and analyze the project targeting a shadow SonarQube instance" |
| 12 | + local BUILD_CMD |
| 13 | + if [[ -e "gradlew" ]]; then |
| 14 | + BUILD_CMD="./gradlew --info --stacktrace --console plain build sonar" |
| 15 | + else |
| 16 | + source set_maven_build_version "$BUILD_NUMBER" |
| 17 | + BUILD_CMD="mvn -Pcoverage -Dmaven.test.redirectTestOutputToFile=false --batch-mode --errors --show-version verify sonar:sonar" |
| 18 | + fi |
| 19 | + ${BUILD_CMD} \ |
| 20 | + -DbuildNumber="${BUILD_NUMBER}" \ |
| 21 | + -Dsonar.host.url="${SHADOW_SONAR_HOST_URL}" \ |
| 22 | + -Dsonar.token="${SHADOW_SONAR_TOKEN}" \ |
| 23 | + -Dsonar.organization="${SHADOW_ORGANIZATION}" \ |
| 24 | + -Dsonar.projectKey="${SONAR_PROJECT_KEY}" \ |
| 25 | + -Dsonar.analysis.buildNumber="${BUILD_NUMBER}" \ |
| 26 | + -Dsonar.analysis.repository="${GITHUB_REPO}" \ |
| 27 | + "$@" |
| 28 | +} |
| 29 | + |
| 30 | +function download_iris() { |
| 31 | + echo |
| 32 | + echo "===== Download ${IRIS_JAR_URL}" |
| 33 | + mkdir -p target/libs |
| 34 | + curl --silent --fail-with-body --location --header "Authorization: Bearer ${ARTIFACTORY_PRIVATE_PASSWORD}" \ |
| 35 | + --output "${IRIS_JAR_PATH}" "${IRIS_JAR_URL}" |
| 36 | +} |
| 37 | + |
| 38 | +function sonarcloud_compute_engine_status_for_given_project() { |
| 39 | + local PROJECT_KEY="$1" |
| 40 | + local RESPONSE |
| 41 | + RESPONSE="$( |
| 42 | + curl --silent --fail-with-body --location --request GET \ |
| 43 | + --header "Authorization: Bearer ${SHADOW_SONAR_TOKEN}" \ |
| 44 | + --output - \ |
| 45 | + "${SHADOW_SONAR_HOST_URL}/api/ce/component?component=${PROJECT_KEY}" |
| 46 | + )" |
| 47 | + local STATUS |
| 48 | + # we first check if there is one or more 'PENDING' tasks in the queue |
| 49 | + STATUS="$(echo "${RESPONSE}" | jq -r '.queue[].status')" |
| 50 | + if [[ "${STATUS}" == "null" ]]; then |
| 51 | + STATUS="" |
| 52 | + fi |
| 53 | + if [[ -z "${STATUS}" ]]; then |
| 54 | + # otherwise we get the status of the current task |
| 55 | + STATUS="$(echo "${RESPONSE}" | jq -r '.current.status')" |
| 56 | + fi |
| 57 | + echo -n "${STATUS}" |
| 58 | +} |
| 59 | + |
| 60 | +function wait_for_sonarcloud_compute_engine_to_finish() { |
| 61 | + local MAX_WAIT_TIME_SECONDS="300" # Default to 5 minutes |
| 62 | + local SLEEP_INTERVAL_SECONDS="1" |
| 63 | + local ELAPSED_TIME=0 |
| 64 | + local LAST_STATUS="" |
| 65 | + local STATUS |
| 66 | + |
| 67 | + echo "Waiting for SonarCloud compute engine to finish for project key: ${SONAR_PROJECT_KEY}" |
| 68 | + while (( ELAPSED_TIME < MAX_WAIT_TIME_SECONDS )); do |
| 69 | + STATUS=$(sonarcloud_compute_engine_status_for_given_project "${SONAR_PROJECT_KEY}") |
| 70 | + if [[ "${STATUS}" != "${LAST_STATUS}" ]]; then |
| 71 | + echo -n " ${STATUS} " |
| 72 | + LAST_STATUS="${STATUS}" |
| 73 | + fi |
| 74 | + |
| 75 | + if [[ "${STATUS}" == "PENDING" || "${STATUS}" == "IN_PROGRESS" ]]; then |
| 76 | + echo -n "." |
| 77 | + elif [[ "${STATUS}" == "FAILED" || "${STATUS}" == "CANCELED" ]]; then |
| 78 | + echo -e "\nERROR: SonarCloud compute engine finished with status: ${STATUS}" |
| 79 | + return 1 |
| 80 | + elif [[ "${STATUS}" == "SUCCESS" ]]; then |
| 81 | + echo -e "\nSonarCloud compute engine finished successfully." |
| 82 | + return 0 |
| 83 | + else |
| 84 | + echo -e "\nERROR: Unknown status: ${STATUS}" |
| 85 | + return 1 |
| 86 | + fi |
| 87 | + sleep "${SLEEP_INTERVAL_SECONDS}" |
| 88 | + ELAPSED_TIME=$((ELAPSED_TIME + SLEEP_INTERVAL_SECONDS)) |
| 89 | + done |
| 90 | + echo -e "\nERROR: Timeout reached after ${MAX_WAIT_TIME_SECONDS} seconds." |
| 91 | + return 1 |
| 92 | +} |
| 93 | + |
| 94 | +function run_iris() { |
| 95 | + local DRY_RUN="$1" |
| 96 | + java \ |
| 97 | + -Diris.source.projectKey="${SONAR_PROJECT_KEY}" \ |
| 98 | + -Diris.source.url="${SONAR_HOST_URL}" \ |
| 99 | + -Diris.source.token="${SONAR_TOKEN}" \ |
| 100 | + -Diris.destination.projectKey="${SONAR_PROJECT_KEY}" \ |
| 101 | + -Diris.destination.organization="${SHADOW_ORGANIZATION}" \ |
| 102 | + -Diris.destination.url="${SHADOW_SONAR_HOST_URL}" \ |
| 103 | + -Diris.destination.token="${SHADOW_SONAR_TOKEN}" \ |
| 104 | + -Diris.dryrun="${DRY_RUN}" \ |
| 105 | + -jar "${IRIS_JAR_PATH}" |
| 106 | +} |
| 107 | + |
| 108 | +function run_iris_with_and_without_dry_run() { |
| 109 | + echo |
| 110 | + echo "===== Execute IRIS as dry-run" |
| 111 | + if run_iris true; then |
| 112 | + echo "===== Successful IRIS execution as dry-run" |
| 113 | + echo "===== Execute IRIS for real" |
| 114 | + if run_iris false; then |
| 115 | + echo "===== Successful IRIS execution for real" |
| 116 | + return 0 |
| 117 | + else |
| 118 | + echo "===== Failed IRIS execution for real" |
| 119 | + return 1 |
| 120 | + fi |
| 121 | + else |
| 122 | + echo "===== Failed IRIS execution as dry-run" |
| 123 | + return 1 |
| 124 | + fi |
| 125 | +} |
| 126 | + |
| 127 | +build_and_analyze_the_project "$@" |
| 128 | +download_iris |
| 129 | +wait_for_sonarcloud_compute_engine_to_finish |
| 130 | +run_iris_with_and_without_dry_run |
0 commit comments