-
Notifications
You must be signed in to change notification settings - Fork 1
fix: split CI to run Sonar analysis via workflow_run #324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| name: Code Analysis | ||
| on: | ||
| workflow_run: | ||
| workflows: ["Build and Test"] | ||
| types: [completed] | ||
|
|
||
| permissions: | ||
| checks: write | ||
| pull-requests: write | ||
| actions: read | ||
|
|
||
| jobs: | ||
| sonar: | ||
| name: SonarQube Analysis | ||
| runs-on: ubuntu-latest | ||
| if: >- | ||
| github.event.workflow_run.conclusion == 'success' && | ||
| (github.event.workflow_run.event == 'push' || github.event.workflow_run.event == 'pull_request') | ||
| steps: | ||
| - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd | ||
| with: | ||
| repository: ${{ github.event.workflow_run.head_repository.full_name }} | ||
| ref: ${{ github.event.workflow_run.head_sha }} | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Set up JDK 17 | ||
| uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 | ||
| with: | ||
| java-version: '17' | ||
| distribution: 'zulu' | ||
| cache: 'gradle' | ||
|
|
||
| - name: Cache SonarQube packages | ||
| uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5 | ||
| with: | ||
| path: ~/.sonar/cache | ||
| key: ${{ runner.os }}-sonar | ||
| restore-keys: ${{ runner.os }}-sonar | ||
|
|
||
| - name: Setup Gradle | ||
| uses: gradle/actions/setup-gradle@0723195856401067f7a2779048b490ace7a47d7c # v5.0.2 | ||
|
|
||
| - name: Download coverage report | ||
| uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 | ||
| with: | ||
| name: coverage-report | ||
| path: build/reports/kover | ||
| run-id: ${{ github.event.workflow_run.id }} | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Download test results | ||
| uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 | ||
| with: | ||
| name: junit-test-results | ||
| path: build/test-results | ||
| run-id: ${{ github.event.workflow_run.id }} | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Resolve PR details | ||
| id: pr | ||
| if: github.event.workflow_run.event == 'pull_request' | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| run: | | ||
| # workflow_run.pull_requests can be empty for fork PRs, fall back to API search | ||
| PR_NUMBER="${{ github.event.workflow_run.pull_requests[0].number }}" | ||
| if [ -z "$PR_NUMBER" ]; then | ||
| PR_NUMBER=$(gh pr list --search "${{ github.event.workflow_run.head_sha }}" --state open --json number --jq '.[0].number // empty') | ||
| fi | ||
| if [ -z "$PR_NUMBER" ]; then | ||
| echo "skip=true" >> "$GITHUB_OUTPUT" | ||
| echo "Could not resolve PR number, skipping PR-specific analysis" | ||
| else | ||
| echo "skip=false" >> "$GITHUB_OUTPUT" | ||
| echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT" | ||
| echo "branch=${{ github.event.workflow_run.head_branch }}" >> "$GITHUB_OUTPUT" | ||
| echo "base=${{ github.event.workflow_run.pull_requests[0].base.ref || 'main' }}" >> "$GITHUB_OUTPUT" | ||
| fi | ||
|
|
||
| - name: Run SonarQube Analysis (PR) | ||
| if: github.event.workflow_run.event == 'pull_request' && steps.pr.outputs.skip != 'true' | ||
| env: | ||
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | ||
| run: >- | ||
| ./gradlew sonar | ||
| -Dsonar.pullrequest.key=${{ steps.pr.outputs.number }} | ||
| -Dsonar.pullrequest.branch=${{ steps.pr.outputs.branch }} | ||
| -Dsonar.pullrequest.base=${{ steps.pr.outputs.base }} | ||
|
|
||
| - name: Run SonarQube Analysis (push) | ||
| if: github.event.workflow_run.event == 'push' | ||
| env: | ||
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | ||
| run: ./gradlew sonar | ||
|
|
||
| coverage-comment: | ||
| name: Coverage PR Comment | ||
| runs-on: ubuntu-latest | ||
| if: >- | ||
| github.event.workflow_run.conclusion == 'success' && | ||
| github.event.workflow_run.event == 'pull_request' | ||
| steps: | ||
| - name: Download coverage report | ||
| uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4 | ||
| with: | ||
| name: coverage-report | ||
| path: build/reports/kover | ||
| run-id: ${{ github.event.workflow_run.id }} | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
|
|
||
| - name: Resolve PR number | ||
| id: pr | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| run: | | ||
| PR_NUMBER="${{ github.event.workflow_run.pull_requests[0].number }}" | ||
| if [ -z "$PR_NUMBER" ]; then | ||
| PR_NUMBER=$(gh pr list --search "${{ github.event.workflow_run.head_sha }}" --state open --json number --jq '.[0].number // empty') | ||
| fi | ||
| if [ -n "$PR_NUMBER" ]; then | ||
| echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT" | ||
| else | ||
| echo "Could not resolve PR number" | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Parse coverage and post comment | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| run: | | ||
| REPORT="build/reports/kover/report.xml" | ||
| if [ ! -f "$REPORT" ]; then | ||
| echo "Coverage report not found at $REPORT" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Extract coverage counters from the Kover/JaCoCo XML report | ||
| # Parse the top-level <counter> elements for INSTRUCTION, BRANCH, and LINE | ||
| extract_coverage() { | ||
| local type=$1 | ||
| local missed covered total pct | ||
| missed=$(grep -oP "<counter type=\"$type\" missed=\"\K[0-9]+" "$REPORT" | tail -1) | ||
| covered=$(grep -oP "<counter type=\"$type\" covered=\"\K[0-9]+" "$REPORT" | tail -1) | ||
| if [ -n "$missed" ] && [ -n "$covered" ]; then | ||
| total=$((missed + covered)) | ||
| if [ "$total" -gt 0 ]; then | ||
| pct=$(( (covered * 10000) / total )) | ||
| echo "$(( pct / 100 )).$(printf '%02d' $(( pct % 100 )))%" | ||
| else | ||
| echo "N/A" | ||
| fi | ||
| else | ||
| echo "N/A" | ||
| fi | ||
| } | ||
|
|
||
| LINE_COV=$(extract_coverage "LINE") | ||
| BRANCH_COV=$(extract_coverage "BRANCH") | ||
| INSTRUCTION_COV=$(extract_coverage "INSTRUCTION") | ||
|
|
||
| BODY=$(cat <<EOF | ||
| ## Code Coverage | ||
|
|
||
| | Metric | Coverage | | ||
| |--------|----------| | ||
| | Line | $LINE_COV | | ||
| | Branch | $BRANCH_COV | | ||
| | Instruction | $INSTRUCTION_COV | | ||
|
|
||
| *Updated for commit ${{ github.event.workflow_run.head_sha }}* | ||
| EOF | ||
| ) | ||
|
|
||
| # Find existing coverage comment to update, or create a new one | ||
| PR_NUMBER=${{ steps.pr.outputs.number }} | ||
| EXISTING_COMMENT=$(gh api "repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \ | ||
| --jq '.[] | select(.body | startswith("## Code Coverage")) | .id' | head -1) | ||
|
|
||
| if [ -n "$EXISTING_COMMENT" ]; then | ||
| gh api "repos/${{ github.repository }}/issues/comments/${EXISTING_COMMENT}" \ | ||
| -X PATCH -f body="$BODY" | ||
| else | ||
| gh api "repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \ | ||
| -X POST -f body="$BODY" | ||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.