Skip to content

Commit

Permalink
Upload test coverage to SonarCloud (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
cutwater authored Nov 14, 2024
1 parent 7d66cd1 commit f541857
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,24 @@ jobs:
- name: run the integration tests
run: make test/integration


- name: Inject PR number into coverage.xml
if: matrix.python-version == '3.11'
run: sed -i '2i <!-- PR ${{ github.event.number }} -->' coverage.xml

- name: upload coverage as artifact
if: matrix.python-version == '3.11'
uses: actions/upload-artifact@v4
with:
name: coverage
path: coverage.xml

- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
if: github.event_name == 'push' && github.repository == 'ansible/galaxy-importer' && github.ref_name == 'master'
env:
SONAR_TOKEN: ${{ secrets.CICD_ORG_SONAR_TOKEN_CICD_BOT }}


publish:
name: Build and publish to PyPI registry
Expand Down
63 changes: 63 additions & 0 deletions .github/workflows/sonar-pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# With much help from:
# https://community.sonarsource.com/t/how-to-use-sonarcloud-with-a-forked-repository-on-github/7363/30
# https://community.sonarsource.com/t/how-to-use-sonarcloud-with-a-forked-repository-on-github/7363/32
name: SonarCloud
on:
workflow_run:
workflows:
- CI
types:
- completed
jobs:
sonar:
name: Upload to SonarCloud
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
show-progress: false

- uses: actions/download-artifact@v4
with:
name: coverage
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}

- name: Extract PR number from coverage.xml
run: |
echo "PR_NUMBER=$(grep -m 1 '<!-- PR' coverage.xml | awk '{print $3}')" >> $GITHUB_ENV
- name: Get PR info
uses: octokit/[email protected]
id: pr_info
with:
route: GET /repos/{repo}/pulls/{number}
repo: ${{ github.event.repository.full_name }}
number: ${{ env.PR_NUMBER }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Set PR info into env
run: |
echo "PR_BASE=${{ fromJson(steps.pr_info.outputs.data).base.ref }}" >> $GITHUB_ENV
echo "PR_HEAD=${{ fromJson(steps.pr_info.outputs.data).head.ref }}" >> $GITHUB_ENV
- name: Add base branch
run: |
gh pr checkout ${{ env.PR_NUMBER }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
SONAR_TOKEN: ${{ secrets.CICD_ORG_SONAR_TOKEN_CICD_BOT }}
with:
args: >
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }}
-Dsonar.pullrequest.key=${{ env.PR_NUMBER }}
-Dsonar.pullrequest.branch=${{ env.PR_HEAD }}
-Dsonar.pullrequest.base=${{ env.PR_BASE }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ __pycache__/

# Python: coverage reports
.coverage*
coverage.xml

# Distribution / packaging
build/
Expand Down
6 changes: 0 additions & 6 deletions .sonarcloud.properties

This file was deleted.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test: lint test/unit test/functional

.PHONY: test/unit
test/unit:
pytest tests/unit --cov=galaxy_importer --cov-branch
pytest tests/unit --cov=galaxy_importer --cov-branch --cov-report xml:coverage.xml

.PHONY: test/unit/annotate
test/unit/annotate:
Expand Down
10 changes: 10 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sonar.projectKey = ansible_galaxy_ng
sonar.organization = ansible

sonar.sources = galaxy_importer

sonar.tests = tests

sonar.python.version = 3.11

sonar.python.coverage.reportPaths = coverage.xml

0 comments on commit f541857

Please sign in to comment.