From d88ec311e47696ef0329b3c89198b47f7d368e81 Mon Sep 17 00:00:00 2001 From: Nithin P Date: Tue, 26 Dec 2023 17:36:14 +0530 Subject: [PATCH] updated sonarqube github action --- .github/workflows/coverage_run_and_upload.yml | 55 +++++++++++++++++ .github/workflows/sonarqube.yml | 61 ++++++++----------- sonar-project.properties | 4 +- 3 files changed, 83 insertions(+), 37 deletions(-) create mode 100644 .github/workflows/coverage_run_and_upload.yml diff --git a/.github/workflows/coverage_run_and_upload.yml b/.github/workflows/coverage_run_and_upload.yml new file mode 100644 index 000000000..599363820 --- /dev/null +++ b/.github/workflows/coverage_run_and_upload.yml @@ -0,0 +1,55 @@ +name: Generate code coverage and upload to artifact +on: + push: + branches: + - main + pull_request: + types: [opened, synchronize, reopened] +jobs: + build: + name: Code Coverage + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Run coverage commands + run: | + #!/bin/bash + + TRACER_PATH=$(pwd) + echo $TRACER_PATH + mkdir coverage + + echo "Starting Couchbase" + docker-compose -f docker-compose-integration.yaml up -d + + echo "Starting Postgres" + sudo systemctl start postgresql.service + sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'mysecretpassword'" + echo "After starting Postgres" + + go test -v -coverpkg=./... -cover -covermode atomic -coverprofile $TRACER_PATH/coverage/coverage.out ./... -json > $TRACER_PATH/coverage/coverage.json + + LIB_LIST=$(find ./instrumentation -name go.mod -exec dirname {} \;) + + for lib in $LIB_LIST + do echo "Generating test coverage for $lib" && cd "$lib" && go mod tidy && go test -v -coverpkg=./... -cover -covermode atomic -coverprofile $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.out ./... -json > $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.json && cd -; + done + + echo "Generating test coverage for ./instrumentation/instagocb" + cd ./instrumentation/instagocb + go mod tidy + go test -v -tags=integration -coverpkg=./... -cover -covermode atomic -coverprofile $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.out ./... -json > $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.json && cd - + + echo "Generating test coverage for ./instrumentation/instapgx" + cd ./instrumentation/instapgx + go mod tidy + go test -v -tags=integration -coverpkg=./... -cover -covermode atomic -coverprofile $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.out ./... -json > $TRACER_PATH/coverage/coverage_$(date +%s%N)_$RANDOM.json && cd - + + - name: Upload code coverage + uses: actions/upload-artifact@v3 + with: + name: go-tracer-code-coverage + path: coverage + diff --git a/.github/workflows/sonarqube.yml b/.github/workflows/sonarqube.yml index ae52af651..f4a1868ae 100644 --- a/.github/workflows/sonarqube.yml +++ b/.github/workflows/sonarqube.yml @@ -1,48 +1,39 @@ name: Generate SonarQube report on: - push: - branches: - - main - pull_request: - types: [opened, synchronize, reopened] + workflow_run: + workflows: [Generate code coverage and upload to artifact] + types: [completed] jobs: build: name: Run analysis runs-on: ubuntu-latest + if: github.event.workflow_run.conclusion == 'success' steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - - name: Run coverage commands - run: | - #!/bin/bash - - echo "Starting Couchbase" - docker-compose -f docker-compose-integration.yaml up -d - - echo "Starting Postgres" - sudo systemctl start postgresql.service - sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'mysecretpassword'" - echo "After starting Postgres" - - go test -v -coverpkg=./... -cover -covermode atomic -coverprofile coverage.out ./... -json > coverage.json - - LIB_LIST=$(find ./instrumentation -name go.mod -exec dirname {} \;) - - for lib in $LIB_LIST - do echo "Generating test coverage for $lib" && cd "$lib" && go mod tidy && go test -v -coverpkg=./... -cover -covermode atomic -coverprofile coverage.out ./... -json > coverage.json && cd -; - done - - echo "Generating test coverage for ./instrumentation/instagocb" - cd ./instrumentation/instagocb - go mod tidy - go test -v -tags=integration -coverpkg=./... -cover -covermode atomic -coverprofile coverage.out ./... -json > coverage.json && cd - - - echo "Generating test coverage for ./instrumentation/instapgx" - cd ./instrumentation/instapgx - go mod tidy - go test -v -tags=integration -coverpkg=./... -cover -covermode atomic -coverprofile coverage.out ./... -json > coverage.json && cd - - + - name: 'Download code coverage' + uses: actions/github-script@v6 + with: + script: | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ + owner: context.repo.owner, + repo: context.repo.repo, + run_id: context.payload.workflow_run.id, + }); + let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { + return artifact.name == "go-tracer-code-coverage" + })[0]; + let download = await github.rest.actions.downloadArtifact({ + owner: context.repo.owner, + repo: context.repo.repo, + artifact_id: matchArtifact.id, + archive_format: 'zip', + }); + let fs = require('fs'); + fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/go-tracer-code-coverage.zip`, Buffer.from(download.data)); + - name: 'Unzip code coverage' + run: unzip go-tracer-code-coverage.zip -d coverage - name: Sonarqube Scan uses: sonarsource/sonarqube-scan-action@master diff --git a/sonar-project.properties b/sonar-project.properties index 7aa392f4c..190e786eb 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -7,5 +7,5 @@ sonar.sources=. sonar.tests=. sonar.exclusions=eum.js,internal/bin/sql/sqlgen.go,example/*.go,example/**/*.go,**/example/*.go,**/**/example/*.go sonar.test.inclusions=**/*_test.go,**/**/*_test.go,instrumentation/**/*_test.go -sonar.go.coverage.reportPaths=coverage.out,instrumentation/**/coverage.out -sonar.go.tests.reportPaths=coverage.json,instrumentation/**/coverage.json +sonar.go.coverage.reportPaths=coverage/coverage*.out +sonar.go.tests.reportPaths=coverage/coverage*.json