Skip to content

Commit a473d47

Browse files
committed
Fix Sonar run for PRs
Signed-off-by: Zbynek Cervinka <[email protected]>
1 parent 47c9982 commit a473d47

File tree

2 files changed

+74
-20
lines changed

2 files changed

+74
-20
lines changed

.github/workflows/linux.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ on:
1010
branches: [ main ]
1111

1212
jobs:
13+
configure_sonar:
14+
runs-on: ubuntu-latest
15+
if: github.event_name == 'pull_request'
16+
17+
steps:
18+
- name: Save Sonar config on PR
19+
run: |
20+
mkdir -p ./prInfo
21+
echo ${{ github.event.number }} > ./prInfo/PR
22+
echo ${{ github.base_ref }} > ./prInfo/base_ref
23+
echo ${{ github.head_ref }} > ./prInfo/head_ref
24+
- uses: actions/upload-artifact@v2
25+
with:
26+
name: prInfo
27+
path: prInfo/
28+
1329
build-linux:
1430
runs-on: ubuntu-latest
1531

.github/workflows/sonar.yml

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,64 @@
1-
# This workflow will build a Java project with Gradle
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
3-
4-
name: SonarQube analysis - Linux
5-
1+
name: Sonar
62
on:
7-
push:
8-
branches: [ main ]
9-
pull_request:
10-
branches: [ main ]
11-
3+
workflow_run:
4+
workflows: ["Java CI with Gradle - Linux"]
5+
types:
6+
- completed
127
jobs:
13-
build-linux:
8+
build:
9+
name: Sonar
1410
runs-on: ubuntu-latest
15-
1611
steps:
17-
- uses: actions/checkout@v2
18-
- name: Set up JDK 11
19-
uses: actions/setup-java@v1
20-
with:
21-
java-version: 11
22-
- name: Run Sonar analysis
23-
env:
12+
- uses: actions/checkout@v2
13+
with:
14+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
15+
ref: ${{github.event.workflow_run.head_sha}}
16+
- name: Set up JDK 11
17+
uses: actions/setup-java@v1
18+
with:
19+
java-version: 11
20+
- name: Download artifacts
21+
uses: actions/[email protected]
22+
with:
23+
script: |
24+
var artifacts = await github.actions.listWorkflowRunArtifacts({
25+
owner: context.repo.owner,
26+
repo: context.repo.repo,
27+
run_id: ${{github.event.workflow_run.id}},
28+
});
29+
for (var artifact of artifacts.data.artifacts) {
30+
if (artifact.name == 'linux-integration-test-reports' || artifact.name == 'ubuntu-latest-test-reports' || artifact.name == 'prInfo') {
31+
var zipfile = await github.actions.downloadArtifact({
32+
owner: context.repo.owner,
33+
repo: context.repo.repo,
34+
artifact_id: artifact.id,
35+
archive_format: 'zip',
36+
});
37+
var fs = require('fs');
38+
var path = require('path');
39+
var pathFile = path.format({
40+
root: '${{github.workspace}}/',
41+
name: artifact.name,
42+
ext: '.zip'
43+
});
44+
fs.writeFileSync(pathFile, Buffer.from(zipfile.data));
45+
}
46+
}
47+
- name: Unzip artifacts
48+
run: |
49+
find . -name '*.zip' -exec sh -c 'unzip -d "${{github.workspace}}/build" "$1"' _ {} \;
50+
- name: Run Sonar analysis
51+
env:
2452
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2553
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
26-
run: ./gradlew sonarqube
54+
run: |
55+
if [ '${{github.event.workflow_run.event}}' == 'pull_request' ]; then
56+
PR_NUMBER=$(<build/PR)
57+
BASE_REF=$(<build/base_ref)
58+
HEAD_REF=$(<build/head_ref)
59+
./gradlew sonarqube -Dsonar.pullrequest.base=$BASE_REF -Dsonar.pullrequest.branch=$HEAD_REF -Dsonar.pullrequest.key=$PR_NUMBER -Dsonar.pullrequest.provider=GitHub -Dsonar.pullrequest.github.repository=${{github.repository}}
60+
else
61+
./gradlew sonarqube
62+
fi
63+
shell: bash
64+

0 commit comments

Comments
 (0)