diff --git a/.github/workflows/qa.yml b/.github/workflows/qa.yml new file mode 100644 index 0000000..2b903ba --- /dev/null +++ b/.github/workflows/qa.yml @@ -0,0 +1,18 @@ +name: QA + +on: push + +jobs: + run_qa: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + - run: ./test/run-qa.sh + timeout-minutes: 5 + services: + sonarqube: + image: sonarqube:8.9-community + ports: + - 9000:9000 diff --git a/test/example-project/sonar-project.properties b/test/example-project/sonar-project.properties new file mode 100644 index 0000000..4ed4ee7 --- /dev/null +++ b/test/example-project/sonar-project.properties @@ -0,0 +1,2 @@ +sonar.projectKey=foo +sonar.sources=src/ diff --git a/test/example-project/src/main.js b/test/example-project/src/main.js new file mode 100644 index 0000000..909cb02 --- /dev/null +++ b/test/example-project/src/main.js @@ -0,0 +1,5 @@ +function main() { + console.log("Hello World"); +} + +main(); diff --git a/test/run-qa.sh b/test/run-qa.sh new file mode 100755 index 0000000..445471d --- /dev/null +++ b/test/run-qa.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +# Helper functions for coloring output. +info() { echo -e "\\e[36m$*\\e[0m"; } +error() { echo -e "\\e[31m✗ $*\\e[0m"; } +success() { echo -e "\\e[32m✔ $*\\e[0m"; } + +# Helper function to check if SonarQube is up and running. +check_sq_is_up() { + local statusCall="$(curl --silent --user admin:admin http://127.0.0.1:9000/api/system/status)" + local status="$(jq -r '.status' <<< "$statusCall")" + if [[ ! $? -eq 0 ]]; then + error "Failed to check if SonarQube is up and running." + exit 1 + fi + echo $status; +} + +info "Build scanner action..." +docker build --no-cache -t sonarsource/sonarqube-scan-action . +if [[ ! $? -eq 0 ]]; then + error "Failed to build the scanner action." + exit 1 +fi +success "Scanner action built." + +info "Find the network SonarQube is running on..." +network=$(docker network ls -f 'name=github_network' --format "{{.Name}}") +if [[ $network != "github_network_"* ]]; then + error "Failed to find the local Docker network." + exit 1 +fi +success "Found the network ($network)." + +info "Wait until SonarQube is up..." +sleep 10 +isUp=$(check_sq_is_up) +until [[ "$isUp" == "UP" ]]; do + sleep 1 + isUp=$(check_sq_is_up) +done +success "SonarQube is up and running." + +info "Generate a new token..." +tokenCall=$(curl --silent --user admin:admin -d "name=token" http://127.0.0.1:9000/api/user_tokens/generate) +token="$(jq -r '.token' <<< "$tokenCall")" +if [[ -z "$token" ]]; then + error "Failed to generate a new token." + exit 1 +fi +success "New token generated." + +info "Analyze project..." +cd test/example-project/ +docker run -v `pwd`:/github/workspace/ --workdir /github/workspace --network $network --env SONAR_TOKEN=$token --env SONAR_HOST_URL='http://sonarqube:9000' sonarsource/sonarqube-scan-action +if [[ ! $? -eq 0 ]]; then + error "Couldn't run the analysis." + exit 1 +elif [[ ! -f ".scannerwork/report-task.txt" ]]; then + error "Couldn't find the report task file. Analysis failed." + exit 1 +fi +success "Analysis successful." + +echo "" # new line +echo "============================" +echo "" # new line +success "QA successful!"