From eb8792f9e3e4de5c85be6063649d91e623dd464d Mon Sep 17 00:00:00 2001 From: Annapurna-sdet Date: Tue, 30 Nov 2021 13:46:09 +0530 Subject: [PATCH] added sonar coverage --- .github/workflows/scraper.yml | 51 ++++++++++++++++++++++++++++++++ scraper/scripts/code-coverage.sh | 19 ++++++++++++ scraper/scripts/run_checks.sh | 14 +++++++++ 3 files changed, 84 insertions(+) create mode 100644 scraper/scripts/code-coverage.sh create mode 100644 scraper/scripts/run_checks.sh diff --git a/.github/workflows/scraper.yml b/.github/workflows/scraper.yml index dafa2609..816404f5 100644 --- a/.github/workflows/scraper.yml +++ b/.github/workflows/scraper.yml @@ -74,3 +74,54 @@ jobs: with: name: release-notes.md path: scraper/scripts/release.md + test: + runs-on: [ self-hosted ] + name: Test + steps: + - name: checkout + id: checkout + uses: actions/checkout@v2 + - name: running test + id: running_test + uses: docker://golang:alpine + env: + GIT_TOKEN: ${{ secrets.GIT_TOKEN }} + GIT_USERNAME: rzp + with: + entrypoint: /bin/sh + args: scripts/run_checks.sh test ci + - name: set_sonar_project + id: set_sonar_project + run: | + BRANCH="$(echo ${GITHUB_REF##*/})"; + SONAR_PROJECT="IFSCDevCoverage" + if [ "$BRANCH" == "sonarCoverage" ]; then + SONAR_PROJECT="IfscUT"; + fi + echo ::set-output name=sonar_project::$SONAR_PROJECT + - name: Push to Sonar + id: push_to_sonar + uses: sonarsource/sonarcloud-github-action@v1.4 + env: + SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }} + SONARCLOUD_URL: ${{ secrets.SONARQUBE_HOST }} + with: + args: > + -Dsonar.host.url=${{ secrets.SONARQUBE_HOST }} + -Dsonar.projectKey=${{ steps.set_sonar_project.outputs.sonar_project }} + -Dsonar.projectName=${{ steps.set_sonar_project.outputs.sonar_project }} + -Dsonar.projectVersion=${{ github.sha }} + -Dsonar.login=${{ secrets.SONARQUBE_TOKEN }} + -Dsonar.projectBaseDir=/github/workspace/ + -Dsonar.go.coverage.reportPaths=/github/workspace/sonarqube.cov + -Dsonar.exclusions=migrations/*,**/*_test.go,**/*mock.go,**/*coverage*,**/constants/*,**/*constants.go,coverage/*,**/logger/* + - name: Coverage Check + id: coverage_check + uses: docker://razorpay/onggi:nginx + if: github.ref != 'refs/heads/master' || !contains(github.ref,'hotfix') + with: + entrypoint: /bin/sh + args: scripts/code_coverage.sh #if issue for older coverage comes, needs to add delay or do this in other job + env: + SONAR_HOST: ${{ secrets.SONARQUBE_HOST }} + SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }} diff --git a/scraper/scripts/code-coverage.sh b/scraper/scripts/code-coverage.sh new file mode 100644 index 00000000..218789b4 --- /dev/null +++ b/scraper/scripts/code-coverage.sh @@ -0,0 +1,19 @@ +# sleep for 10 seconds +sleep 10 + +project_key="IfscUT" +threshold=0 + +#Code flow +CURL_URL=$SONAR_HOST'/api/measures/component_tree?metricKeys=coverage&component='$project_key +curl --location --request GET $CURL_URL -u $SONAR_TOKEN:"" > sonar.json +apk update \ +&& apk add jq \ +&& rm -rf /var/cache/apk/* +jq -r '.baseComponent.measures[0].value' sonar.json +code_coverage=$( jq -r '.baseComponent.measures[0].value' sonar.json | cut -d "." -f 1) +echo $code_coverage +echo "SONAR Threshold is $threshold" +cat sonar.json +if [ -z "$code_coverage" ] || [ $code_coverage = "null" ]; then echo "Value not found"; exit 1; fi +if [ $code_coverage -lt $threshold ]; then echo "failed - threshold unit code coverage check"; exit 1; else echo "success"; exit 0; fi diff --git a/scraper/scripts/run_checks.sh b/scraper/scripts/run_checks.sh new file mode 100644 index 00000000..ecb4e4ca --- /dev/null +++ b/scraper/scripts/run_checks.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +test_env=$2 + +if [[ "${test_env}" = "ci" ]]; then + echo "Setting up code" + go env -w GOPRIVATE=github.com/razorpay + apk add --no-cache git gcc xmlsec-dev libxml2 libltdl pkgconfig libc-dev openssl-dev + export CGO_CFLAGS_ALLOW=".*" + git config --global url."https://${GIT_TOKEN}:x-oauth-basic@github.com".insteadOf "https://github.com" + go get -u golang.org/x/tools/cmd/goimports + go get -u golang.org/x/lint/golint + cp config/default.toml config/dev-test.toml +fi