|
| 1 | +name: CI on forks - Sonar analysis |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: [CI on forks - build and tests] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +permissions: {} |
| 10 | + |
| 11 | +jobs: |
| 12 | + sonar: |
| 13 | + name: Run Sonar Analysis for forks |
| 14 | + runs-on: ubuntu-latest |
| 15 | + if: > |
| 16 | + github.event.workflow_run.event == 'pull_request' && |
| 17 | + github.event.workflow_run.conclusion == 'success' |
| 18 | + permissions: |
| 19 | + actions: write |
| 20 | + checks: write |
| 21 | + contents: read |
| 22 | + issues: read |
| 23 | + pull-requests: write |
| 24 | + statuses: write |
| 25 | + steps: |
| 26 | + - name: Download PR information |
| 27 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 28 | + with: |
| 29 | + script: | |
| 30 | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 31 | + owner: context.repo.owner, |
| 32 | + repo: context.repo.repo, |
| 33 | + run_id: context.payload.workflow_run.id, |
| 34 | + }); |
| 35 | + let prInfoArtifact = allArtifacts.data.artifacts.filter((artifact) => { |
| 36 | + return artifact.name.startsWith("pr-info-") |
| 37 | + })[0]; |
| 38 | + if (!prInfoArtifact) { |
| 39 | + core.setFailed("❌ No PR info artifact found"); |
| 40 | + return; |
| 41 | + } |
| 42 | + let download = await github.rest.actions.downloadArtifact({ |
| 43 | + owner: context.repo.owner, |
| 44 | + repo: context.repo.repo, |
| 45 | + artifact_id: prInfoArtifact.id, |
| 46 | + archive_format: 'zip', |
| 47 | + }); |
| 48 | + const fs = require('fs'); |
| 49 | + const path = require('path'); |
| 50 | + const temp = '${{ runner.temp }}/pr-info'; |
| 51 | + if (!fs.existsSync(temp)){ |
| 52 | + fs.mkdirSync(temp, { recursive: true }); |
| 53 | + } |
| 54 | + fs.writeFileSync(path.join(temp, 'pr-info.zip'), Buffer.from(download.data)); |
| 55 | + console.log("PR information downloaded"); |
| 56 | +
|
| 57 | + - name: Extract PR Information |
| 58 | + run: | |
| 59 | + mkdir -p ${{ runner.temp }}/pr-info-extracted |
| 60 | + unzip -q ${{ runner.temp }}/pr-info/pr-info.zip -d ${{ runner.temp }}/pr-info-extracted |
| 61 | + REPO_NAME=$(cat ${{ runner.temp }}/pr-info-extracted/repo-name) |
| 62 | + HEAD_REF=$(cat ${{ runner.temp }}/pr-info-extracted/head-ref) |
| 63 | + HEAD_SHA=$(cat ${{ runner.temp }}/pr-info-extracted/head-sha) |
| 64 | + PR_NUMBER=$(cat ${{ runner.temp }}/pr-info-extracted/pr-number) |
| 65 | + BASE_REF=$(cat ${{ runner.temp }}/pr-info-extracted/base-ref) |
| 66 | + echo "REPO_NAME=$REPO_NAME" >> $GITHUB_ENV |
| 67 | + echo "HEAD_REF=$HEAD_REF" >> $GITHUB_ENV |
| 68 | + echo "HEAD_SHA=$HEAD_SHA" >> $GITHUB_ENV |
| 69 | + echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV |
| 70 | + echo "BASE_REF=$BASE_REF" >> $GITHUB_ENV |
| 71 | + echo "PR information extracted: $REPO_NAME $HEAD_REF $HEAD_SHA $PR_NUMBER $BASE_REF" |
| 72 | +
|
| 73 | + - name: Checkout sources |
| 74 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 75 | + with: |
| 76 | + ref: ${{ env.HEAD_REF }} |
| 77 | + repository: ${{ env.REPO_NAME }} |
| 78 | + fetch-depth: 0 |
| 79 | + |
| 80 | + - name: Set up JDK 21 |
| 81 | + uses: actions/setup-java@be666c2fcd27ec809703dec50e508c2fdc7f6654 # v5.2.0 |
| 82 | + with: |
| 83 | + distribution: 'temurin' |
| 84 | + java-version: '21' |
| 85 | + cache: 'maven' |
| 86 | + |
| 87 | + - name: Download artifact |
| 88 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 89 | + with: |
| 90 | + script: | |
| 91 | + let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 92 | + owner: context.repo.owner, |
| 93 | + repo: context.repo.repo, |
| 94 | + run_id: context.payload.workflow_run.id, |
| 95 | + }); |
| 96 | + let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { |
| 97 | + return artifact.name.startsWith("data-for-sonar-analysis-") |
| 98 | + })[0]; |
| 99 | + if (!matchArtifact) { |
| 100 | + core.setFailed("❌ No matching artifact found"); |
| 101 | + return; |
| 102 | + } |
| 103 | + const prNumber = matchArtifact.name.replace("data-for-sonar-analysis-", ""); |
| 104 | + console.log(`PR number: ${prNumber}`); |
| 105 | + core.exportVariable('PR_NUMBER', prNumber); |
| 106 | + let download = await github.rest.actions.downloadArtifact({ |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + artifact_id: matchArtifact.id, |
| 110 | + archive_format: 'zip', |
| 111 | + }); |
| 112 | + const fs = require('fs'); |
| 113 | + const path = require('path'); |
| 114 | + const temp = '${{ runner.temp }}/artifacts'; |
| 115 | + if (!fs.existsSync(temp)){ |
| 116 | + fs.mkdirSync(temp); |
| 117 | + } |
| 118 | + fs.writeFileSync(path.join(temp, 'sonar-data.zip'), Buffer.from(download.data)); |
| 119 | +
|
| 120 | + - name: Extract Sonar Analysis Data |
| 121 | + run: | |
| 122 | + mkdir -p ${{ runner.temp }}/extracted |
| 123 | + unzip -q ${{ runner.temp }}/artifacts/sonar-data.zip -d ${{ runner.temp }}/extracted |
| 124 | + cp -r ${{ runner.temp }}/extracted/* . |
| 125 | + ls -la distribution-diagram/target/site/jacoco-aggregate/ || echo "Jacoco report directory not found" |
| 126 | +
|
| 127 | + - name: Prepare Sonar Analysis |
| 128 | + run: | |
| 129 | + echo "Checking required directories..." |
| 130 | + if [ -f "distribution-diagram/target/site/jacoco-aggregate/jacoco.xml" ]; then |
| 131 | + echo "Jacoco report found" |
| 132 | + else |
| 133 | + echo "Warning: Jacoco report not found at expected location" |
| 134 | + fi |
| 135 | + echo "Finding sources and binaries..." |
| 136 | + SOURCES=$(find . -type d -path "*/main/java" | sort -u | paste -sd "," -) |
| 137 | + if [ -z "$SOURCES" ]; then |
| 138 | + echo "Warning: No source directories found!" |
| 139 | + else |
| 140 | + echo "SOURCES : $SOURCES" |
| 141 | + echo "SOURCES=$SOURCES" >> $GITHUB_ENV |
| 142 | + fi |
| 143 | + GENERATED=$(find . -type d -path "*/target/generated-sources" | sort -u | paste -sd "," -) |
| 144 | + if [ -z "$GENERATED" ]; then |
| 145 | + echo "Warning: No generated source directories found!" |
| 146 | + else |
| 147 | + echo "GENERATED : $GENERATED" |
| 148 | + echo "GENERATED=$GENERATED" >> $GITHUB_ENV |
| 149 | + fi |
| 150 | + TESTS=$(find . -type d -path "*/test/java" | sort -u | paste -sd "," -) |
| 151 | + if [ -z "$TESTS" ]; then |
| 152 | + echo "Warning: No test directories found!" |
| 153 | + else |
| 154 | + echo "TESTS : $TESTS" |
| 155 | + echo "TESTS=$TESTS" >> $GITHUB_ENV |
| 156 | + fi |
| 157 | + BINARIES=$(find . -type d -path "*/target/classes" | sort -u | paste -sd "," -) |
| 158 | + if [ -z "$BINARIES" ]; then |
| 159 | + echo "Warning: No binaries directories found!" |
| 160 | + else |
| 161 | + echo "BINARIES : $BINARIES" |
| 162 | + echo "BINARIES=$BINARIES" >> $GITHUB_ENV |
| 163 | + fi |
| 164 | + LIBRARIES="distribution-diagram/target/dependency" |
| 165 | + if [ -z "$LIBRARIES" ]; then |
| 166 | + echo "Warning: No libraries directory found!" |
| 167 | + else |
| 168 | + echo "LIBRARIES : $LIBRARIES" |
| 169 | + echo "LIBRARIES=$LIBRARIES" >> $GITHUB_ENV |
| 170 | + fi |
| 171 | +
|
| 172 | + # This sonar action should NOT be replaced by a direct use of the mvn verify command since we don't want external |
| 173 | + # code to run in a workflow_run workflow (it may lead to security issues). |
| 174 | + - name: Run Sonar Analysis |
| 175 | + uses: SonarSource/sonarqube-scan-action@a31c9398be7ace6bbfaf30c0bd5d415f843d45e9 # v7.0.0 |
| 176 | + env: |
| 177 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 178 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 179 | + with: |
| 180 | + args: > |
| 181 | + -Dsonar.projectKey=com.powsybl:powsybl-diagram |
| 182 | + -Dsonar.organization=powsybl-ci-github |
| 183 | + -Dsonar.sources=${{ env.SOURCES }} |
| 184 | + -Dsonar.generatedSources=${{ env.GENERATED }} |
| 185 | + -Dsonar.tests=${{ env.TESTS }} |
| 186 | + -Dsonar.java.binaries=${{ env.BINARIES }} |
| 187 | + -Dsonar.java.libraries=${{ env.LIBRARIES }} |
| 188 | + -Dsonar.java.test.libraries=${{ env.LIBRARIES }} |
| 189 | + -Dsonar.coverage.jacoco.xmlReportPaths="distribution-diagram/target/site/jacoco-aggregate/jacoco.xml" |
| 190 | + -Dsonar.pullrequest.key=${{ env.PR_NUMBER }} |
| 191 | + -Dsonar.pullrequest.branch=${{ env.HEAD_REF }} |
| 192 | + -Dsonar.pullrequest.base=${{ env.BASE_REF }} |
| 193 | + -Dsonar.pullrequest.provider=github |
| 194 | + -Dsonar.pullrequest.github.repository=${{ github.repository }} |
| 195 | + -Dsonar.host.url=https://sonarcloud.io |
| 196 | + -Dsonar.scm.provider=git |
| 197 | + -Dsonar.qualitygate.wait=true |
| 198 | + -Dsonar.scm.revision=${{ env.HEAD_SHA }} |
| 199 | +
|
| 200 | + - name: Delete artifacts used in analysis |
| 201 | + if: always() |
| 202 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 |
| 203 | + with: |
| 204 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 205 | + script: | |
| 206 | + let artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 207 | + owner: context.repo.owner, |
| 208 | + repo: context.repo.repo, |
| 209 | + run_id: context.payload.workflow_run.id, |
| 210 | + }); |
| 211 | + for (const artifact of artifacts.data.artifacts) { |
| 212 | + if ( |
| 213 | + artifact.name.startsWith("data-for-sonar-analysis-") || |
| 214 | + artifact.name.startsWith("pr-info-") |
| 215 | + ) { |
| 216 | + console.log(`Deleting artifact: ${artifact.name} (ID: ${artifact.id})`); |
| 217 | + await github.rest.actions.deleteArtifact({ |
| 218 | + owner: context.repo.owner, |
| 219 | + repo: context.repo.repo, |
| 220 | + artifact_id: artifact.id |
| 221 | + }); |
| 222 | + } |
| 223 | + } |
0 commit comments