From e231533aaa5523979c2a3f76e75fc38061fd0c16 Mon Sep 17 00:00:00 2001 From: jamshale Date: Tue, 21 May 2024 15:24:48 -0700 Subject: [PATCH] Sonarcloud with code coverage --- .codecov.yml | 4 -- .github/actions/run-unit-tests/action.yml | 54 +++++++++++++++++ .github/workflows/nigthly.yml | 17 ++++-- .github/workflows/pr-tests.yml | 15 +++-- .github/workflows/sonar-merge-main.yml | 34 +++++++++++ .github/workflows/sonar-pr.yml | 73 +++++++++++++++++++++++ .github/workflows/tests.yml | 37 ------------ README.md | 1 - sonar-project.properties | 5 ++ 9 files changed, 187 insertions(+), 53 deletions(-) delete mode 100644 .codecov.yml create mode 100644 .github/actions/run-unit-tests/action.yml create mode 100644 .github/workflows/sonar-merge-main.yml create mode 100644 .github/workflows/sonar-pr.yml delete mode 100644 .github/workflows/tests.yml create mode 100644 sonar-project.properties diff --git a/.codecov.yml b/.codecov.yml deleted file mode 100644 index 762c4cc63c..0000000000 --- a/.codecov.yml +++ /dev/null @@ -1,4 +0,0 @@ -comment: - layout: "header, diff" - behavior: default - require_changes: no \ No newline at end of file diff --git a/.github/actions/run-unit-tests/action.yml b/.github/actions/run-unit-tests/action.yml new file mode 100644 index 0000000000..ee2c75429e --- /dev/null +++ b/.github/actions/run-unit-tests/action.yml @@ -0,0 +1,54 @@ +name: Tests + +inputs: + python-version: + required: true + os: + required: true + is_pr: + required: false + default: "true" + +runs: + using: "composite" + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Set up Python ${{ inputs.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ inputs.python-version }} + cache: 'pip' + cache-dependency-path: 'requirements*.txt' + - name: Install the project dependencies + shell: bash + run: | + pip install poetry + poetry install --all-extras + - name: Tests + shell: bash + run: | + poetry run pytest --cov=aries_cloudagent --cov-report term-missing --cov-report xml --ignore-glob=/tests/* --ignore-glob=demo/* --ignore-glob=docker/* --ignore-glob=docs/* --ignore-glob=scripts/* 2>&1 | tee pytest.log + PYTEST_EXIT_CODE=${PIPESTATUS[0]} + if grep -Eq "RuntimeWarning: coroutine .* was never awaited" pytest.log; then + echo "Failure: Detected unawaited coroutine warning in pytest output." + exit 1 + fi + exit $PYTEST_EXIT_CODE + - name: Save PR number to file + if: inputs.is_pr == 'true' + shell: bash + run: echo ${{ github.event.number }} > PR_NUMBER + - name: Archive PR number + if: inputs.is_pr == 'true' + uses: actions/upload-artifact@v4 + with: + name: PR_NUMBER + path: PR_NUMBER + - name: Archive Test Results + if: inputs.is_pr == 'true' + uses: actions/upload-artifact@v4 + with: + name: TEST_COV + path: test-reports/coverage.xml diff --git a/.github/workflows/nigthly.yml b/.github/workflows/nigthly.yml index c6e01b95ce..7427d28d21 100644 --- a/.github/workflows/nigthly.yml +++ b/.github/workflows/nigthly.yml @@ -7,17 +7,22 @@ on: jobs: tests: - if: github.repository == 'hyperledger/aries-cloudagent-python' || github.event_name == 'workflow_dispatch' - name: Tests + runs-on: ubuntu-latest strategy: fail-fast: false matrix: os: ["ubuntu-latest"] python-version: ["3.9", "3.10"] - uses: ./.github/workflows/tests.yml - with: - python-version: ${{ matrix.python-version }} - os: ${{ matrix.os }} + if: github.repository == 'hyperledger/aries-cloudagent-python' || github.event_name == 'workflow_dispatch' + steps: + - name: checkout + uses: actions/checkout@v4 + - name: Tests + uses: ./.github/actions/run-unit-tests + with: + python-version: ${{ matrix.python-version }} + os: ${{ matrix.os }} + is_pr: "false" setup_and_check_pub: name: Setup Publish diff --git a/.github/workflows/pr-tests.yml b/.github/workflows/pr-tests.yml index 5de0998c36..6813b88dc0 100644 --- a/.github/workflows/pr-tests.yml +++ b/.github/workflows/pr-tests.yml @@ -9,8 +9,13 @@ concurrency: jobs: tests: - name: Tests - uses: ./.github/workflows/tests.yml - with: - python-version: "3.9" - os: "ubuntu-latest" + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v4 + - name: Tests + uses: ./.github/actions/run-unit-tests + with: + python-version: "3.9" + os: "ubuntu-latest" + is_pr: "true" diff --git a/.github/workflows/sonar-merge-main.yml b/.github/workflows/sonar-merge-main.yml new file mode 100644 index 0000000000..9f00bc5f59 --- /dev/null +++ b/.github/workflows/sonar-merge-main.yml @@ -0,0 +1,34 @@ +name: Sonar Scan and Coverage +on: + push: + branches: + - main + +jobs: + sonarcloud: + name: SonarCloud + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Tests + uses: ./.github/actions/run-unit-tests + with: + python-version: "3.9" + os: "ubuntu-latest" + is_pr: "false" + - name: Adjust Test Coverage Source + run: | + # Need to change source in coverage report because it was generated from another context + sed -i 's/\/home\/runner\/work\/aries-cloudagent-python\/aries-cloudagent-python\//\/github\/workspace\//g' test-reports/coverage.xml + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + with: + args: > + -Dsonar.python.coverage.reportPaths=test-reports/coverage.xml + -Dsonar.coverage.exclusions=**/tests/*,**/demo/*,**/docs/*,**/docker/*,**/scripts/* + -Dsonar.sources=./ \ No newline at end of file diff --git a/.github/workflows/sonar-pr.yml b/.github/workflows/sonar-pr.yml new file mode 100644 index 0000000000..a88ab3e060 --- /dev/null +++ b/.github/workflows/sonar-pr.yml @@ -0,0 +1,73 @@ +name: Sonar Scan and Coverage + +on: + workflow_run: + workflows: [ PR Tests ] + types: + - completed + +jobs: + SonarCloud: + runs-on: ubuntu-latest + if: github.event.workflow_run.conclusion == 'success' + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Download PR number artifact + uses: dawidd6/action-download-artifact@v3 + with: + workflow: Tests + run_id: ${{ github.event.workflow_run.id }} + name: PR_NUMBER + - name: Read PR_NUMBER + id: pr_number + uses: juliangruber/read-file-action@v1 + with: + path: ./PR_NUMBER + - name: Download Test Coverage + uses: dawidd6/action-download-artifact@v3 + with: + workflow: Tests + run_id: ${{ github.event.workflow_run.id }} + name: TEST_COV + - name: Request GitHub API for PR data + uses: octokit/request-action@v2.x + id: get_pr_data + with: + route: GET /repos/${{ github.event.repository.full_name }}/pulls/${{ steps.pr_number.outputs.content }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Checkout base branch + run: | + echo forked repo = ${{ fromJson(steps.get_pr_data.outputs.data).head.repo.html_url }}.git + echo base repo = ${{ github.event.repository.clone_url }} + + git remote add upstream ${{ fromJson(steps.get_pr_data.outputs.data).head.repo.html_url }}.git + git fetch --all + + echo pr number = ${{ fromJson(steps.get_pr_data.outputs.data).number }} + echo forked branch = ${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} + echo base branch = ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} + + git checkout -B temp-branch-for-scanning upstream/${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} + - name: Move Coverage Report And Adjust Source + run: | + mkdir test-reports + mv coverage.xml test-reports + # Need to change source in coverage report because it was generated from another context + sed -i 's/\/home\/runner\/work\/aries-cloudagent-python\/aries-cloudagent-python\//\/github\/workspace\//g' test-reports/coverage.xml + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + with: + args: > + -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} + -Dsonar.pullrequest.key=${{ fromJson(steps.get_pr_data.outputs.data).number }} + -Dsonar.pullrequest.branch=${{ fromJson(steps.get_pr_data.outputs.data).head.ref }} + -Dsonar.pullrequest.base=${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} + -Dsonar.coverage.exclusions=**/tests/*,**/demo/*,**/docs/*,**/docker/*,**/scripts/* + -Dsonar.python.coverage.reportPaths=test-reports/coverage.xml + -Dsonar.sources=./ \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml deleted file mode 100644 index 62699408a3..0000000000 --- a/.github/workflows/tests.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: Tests - -on: - workflow_call: - inputs: - python-version: - required: true - type: string - os: - required: true - type: string - -jobs: - tests: - name: Test Python ${{ inputs.python-version }} - runs-on: ${{ inputs.os }} - steps: - - uses: actions/checkout@v4 - - name: Set up Python ${{ inputs.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ inputs.python-version }} - cache: 'pip' - cache-dependency-path: 'requirements*.txt' - - name: Install the project dependencies - run: | - pip install poetry - poetry install -E "askar bbs" - - name: Tests - run: | - poetry run pytest 2>&1 | tee pytest.log - PYTEST_EXIT_CODE=${PIPESTATUS[0]} - if grep -Eq "RuntimeWarning: coroutine .* was never awaited" pytest.log; then - echo "Failure: Detected unawaited coroutine warning in pytest output." - exit 1 - fi - exit $PYTEST_EXIT_CODE diff --git a/README.md b/README.md index 4077f04249..d25d2b5814 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # Hyperledger Aries Cloud Agent - Python [![pypi releases](https://img.shields.io/pypi/v/aries_cloudagent)](https://pypi.org/project/aries-cloudagent/) -[![codecov](https://codecov.io/gh/hyperledger/aries-cloudagent-python/branch/main/graph/badge.svg)](https://codecov.io/gh/hyperledger/aries-cloudagent-python) diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000000..03ad4b3b74 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,5 @@ +sonar.projectKey=hyperledger_aries-cloudagent-python +sonar.organization=hyperledger +sonar.projectName=aries-cloudagent-python + +sonar.python.version=3.9 \ No newline at end of file