Repository navigation
sonarcloud #259
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
| # Licensed to the Apache Software Foundation (ASF) under one or more | |
| # contributor license agreements. See the NOTICE file distributed with | |
| # this work for additional information regarding copyright ownership. | |
| # The ASF licenses this file to You under the Apache License, Version 2.0 | |
| # (the "License"); you may not use this file except in compliance with | |
| # the License. You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| name: sonarcloud | |
| on: | |
| workflow_run: | |
| workflows: [master pull request ci] | |
| types: [completed] | |
| # Explicitly drop all permissions. | |
| # Permissions will be granted to individual jobs as needed. | |
| permissions: {} | |
| concurrency: | |
| group: sonarcloud-${{ github.event.workflow_run.pull_requests[0].number || github.event.workflow_run.head_sha }} | |
| cancel-in-progress: true | |
| jobs: | |
| # WARNING: this job runs with access to repository secrets: do not execute untrusted code here. | |
| # It is triggered after the Build workflow completes; avoid running any code from the pull request itself. | |
| sonarcloud-scan: | |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
| runs-on: ubuntu-24.04 | |
| steps: | |
| - uses: actions/checkout@v7 | |
| with: | |
| repository: ${{ github.event.workflow_run.head_repository.full_name }} | |
| ref: ${{ github.event.workflow_run.head_sha }} | |
| fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis | |
| allow-unsafe-pr-checkout: true # Explicitely allow (no code is compiled or run in this workflow) | |
| persist-credentials: false | |
| - name: Fetch target branch for PR analysis | |
| if: github.event.workflow_run.event == 'pull_request' | |
| run: | | |
| git remote add upstream https://github.com/${{ github.repository }}.git || true | |
| git fetch upstream master:refs/remotes/upstream/master | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v6 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| # Coverage and JUnit XML come only from the master-build Ubuntu JDK 17 matrix job. | |
| - name: Download Binary Artifacts (Ubuntu JDK 17) | |
| id: bins | |
| uses: dawidd6/action-download-artifact@v25 | |
| with: | |
| name: build-artifacts-linux-jdk17 | |
| workflow: master-build.yml | |
| run_id: ${{ github.event.workflow_run.id }} | |
| path: ./build/ | |
| if_no_artifact_found: ignore | |
| - name: Skip SonarCloud when test artifacts are absent | |
| if: steps.bins.outputs.found_artifact != 'true' | |
| run: | | |
| echo "::notice::No Ubuntu JDK 17 build artifacts (tests may have been path-filtered). Skipping SonarCloud scan." | |
| - name: Download coverage data (Ubuntu JDK 17) | |
| if: steps.bins.outputs.found_artifact == 'true' | |
| uses: dawidd6/action-download-artifact@v25 | |
| with: | |
| name: coverage-data-linux-jdk17 | |
| workflow: master-build.yml | |
| run_id: ${{ github.event.workflow_run.id }} | |
| path: ./build/coverage/ | |
| - name: Download test reports (Ubuntu JDK 17) | |
| if: steps.bins.outputs.found_artifact == 'true' | |
| uses: dawidd6/action-download-artifact@v25 | |
| with: | |
| name: junit-test-results-linux-jdk17 | |
| workflow: master-build.yml | |
| run_id: ${{ github.event.workflow_run.id }} | |
| # Artifact has build/ stripped (upload-artifact LCA); re-root under build/ | |
| # so paths match build/test/... and build/<plugin>/test/... | |
| path: ./build/ | |
| - name: Flatten test reports (JDK 17 only) | |
| if: steps.bins.outputs.found_artifact == 'true' | |
| run: | | |
| mkdir -p ./build/test-reports | |
| find ./build/test ./build/*/test/ -name 'TEST-*.xml' -exec cp {} ./build/test-reports/ \; | |
| - name: Verify Sonar inputs | |
| if: steps.bins.outputs.found_artifact == 'true' | |
| run: | | |
| set -euo pipefail | |
| if ! find ./build -name '*.class' -print -quit | grep -q .; then | |
| echo "::error::No compiled class files found under build/" | |
| exit 1 | |
| fi | |
| if [ ! -s ./build/coverage/jacoco.xml ]; then | |
| echo "::error::Missing or empty build/coverage/jacoco.xml" | |
| exit 1 | |
| fi | |
| if ! ls ./build/test-reports/TEST-*.xml >/dev/null 2>&1; then | |
| echo "::error::No JUnit TEST-*.xml reports in build/test-reports/" | |
| exit 1 | |
| fi | |
| echo "Sonar inputs OK: classes, jacoco.xml, and JUnit reports present." | |
| - name: Set Sonar project version | |
| if: steps.bins.outputs.found_artifact == 'true' | |
| id: version | |
| run: | | |
| NUTCH_VERSION=$(grep '^version=' default.properties | cut -d= -f2) | |
| if [ -z "$NUTCH_VERSION" ]; then | |
| echo "::error::Could not read version from default.properties" | |
| exit 1 | |
| fi | |
| echo "value=${NUTCH_VERSION}" >> "$GITHUB_OUTPUT" | |
| echo "Sonar project version: ${NUTCH_VERSION}" | |
| - name: Resolve PR number | |
| if: steps.bins.outputs.found_artifact == 'true' | |
| id: pr | |
| run: | | |
| if [ "${{ github.event.workflow_run.event }}" != "pull_request" ]; then | |
| echo "is_pr=false" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| fi | |
| PR_NUMBER="${{ github.event.workflow_run.pull_requests[0].number }}" | |
| if [ -z "$PR_NUMBER" ]; then | |
| PR_NUMBER=$(gh api \ | |
| "repos/${{ github.repository }}/commits/${{ github.event.workflow_run.head_sha }}/pulls" \ | |
| --jq '.[0].number // empty') | |
| fi | |
| if [ -z "$PR_NUMBER" ]; then | |
| PR_NUMBER=$(gh pr list --repo "${{ github.repository }}" --state open \ | |
| --json number,headRefOid \ | |
| --jq ".[] | select(.headRefOid == \"${{ github.event.workflow_run.head_sha }}\") | .number") | |
| fi | |
| if [ -n "$PR_NUMBER" ]; then | |
| echo "number=${PR_NUMBER}" >> "$GITHUB_OUTPUT" | |
| echo "is_pr=true" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "is_pr=false" >> "$GITHUB_OUTPUT" | |
| echo "skip=true" >> "$GITHUB_OUTPUT" | |
| echo "⚠️ Could not resolve PR number for pull_request event. Skipping SonarCloud analysis." | |
| fi | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| - name: SonarCloud Scan (PR) | |
| if: steps.bins.outputs.found_artifact == 'true' && steps.pr.outputs.is_pr == 'true' | |
| uses: SonarSource/sonarqube-scan-action@ba9859eae8dd6bd29e412f25ddbbef3d032000f4 | |
| with: | |
| args: > | |
| -Dsonar.pullrequest.key=${{ steps.pr.outputs.number }} | |
| -Dsonar.pullrequest.branch=${{ github.event.workflow_run.head_branch }} | |
| -Dsonar.pullrequest.base=master | |
| -Dsonar.projectVersion=${{ steps.version.outputs.value }} | |
| env: | |
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
| SONAR_HOST_URL: https://sonarcloud.io | |
| - name: SonarCloud Scan (branch) | |
| if: steps.bins.outputs.found_artifact == 'true' && steps.pr.outputs.is_pr == 'false' && steps.pr.outputs.skip != 'true' | |
| uses: SonarSource/sonarqube-scan-action@ba9859eae8dd6bd29e412f25ddbbef3d032000f4 | |
| with: | |
| args: > | |
| -Dsonar.branch.name=${{ github.event.workflow_run.head_branch }} | |
| -Dsonar.projectVersion=${{ steps.version.outputs.value }} | |
| env: | |
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
| SONAR_HOST_URL: https://sonarcloud.io |