Skip to content

Commit e70fe03

Browse files
authored
Add SonarCloud to repository (#720)
1 parent 967e44e commit e70fe03

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

.github/workflows/sonar.yml

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Sonar
2+
on:
3+
workflow_run:
4+
workflows: [Validate PR]
5+
types: [completed]
6+
jobs:
7+
sonar:
8+
name: Sonar
9+
runs-on: ubuntu-latest
10+
if: github.event.workflow_run.conclusion == 'success'
11+
steps:
12+
- name: Download PR number artifact
13+
if: github.event.workflow_run.event == 'pull_request'
14+
uses: dawidd6/action-download-artifact@v2
15+
with:
16+
workflow: Validate PR
17+
run_id: ${{ github.event.workflow_run.id }}
18+
name: PR_NUMBER
19+
- name: Read PR_NUMBER.txt
20+
if: github.event.workflow_run.event == 'pull_request'
21+
id: pr_number
22+
uses: juliangruber/read-file-action@v1
23+
with:
24+
path: ./PR_NUMBER.txt
25+
- name: Request GitHub API for PR data
26+
if: github.event.workflow_run.event == 'pull_request'
27+
uses: octokit/[email protected]
28+
id: get_pr_data
29+
with:
30+
route: GET /repos/{full_name}/pulls/{number}
31+
number: ${{ steps.pr_number.outputs.content }}
32+
full_name: ${{ github.event.repository.full_name }}
33+
env:
34+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35+
- uses: actions/checkout@v3
36+
with:
37+
repository: ${{ github.event.workflow_run.head_repository.full_name }}
38+
ref: ${{ github.event.workflow_run.head_branch }}
39+
fetch-depth: 0
40+
- name: Checkout base branch
41+
if: github.event.workflow_run.event == 'pull_request'
42+
run: |
43+
git remote add upstream ${{ github.event.repository.clone_url }}
44+
git fetch upstream
45+
git checkout -B ${{ fromJson(steps.get_pr_data.outputs.data).base.ref }} upstream/${{ fromJson(steps.get_pr_data.outputs.data).base.ref }}
46+
git checkout ${{ github.event.workflow_run.head_branch }}
47+
git clean -ffdx && git reset --hard HEAD
48+
- name: SonarCloud Scan on PR
49+
if: github.event.workflow_run.event == 'pull_request'
50+
uses: sonarsource/sonarcloud-github-action@master
51+
env:
52+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
54+
with:
55+
args: >
56+
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }}
57+
-Dsonar.pullrequest.key=${{ fromJson(steps.get_pr_data.outputs.data).number }}
58+
-Dsonar.pullrequest.branch=${{ fromJson(steps.get_pr_data.outputs.data).head.ref }}
59+
-Dsonar.pullrequest.base=${{ fromJson(steps.get_pr_data.outputs.data).base.ref }}
60+
- name: SonarCloud Scan on push
61+
if: github.event.workflow_run.event == 'push' && github.event.workflow_run.head_repository.full_name == github.event.repository.full_name
62+
uses: sonarsource/sonarcloud-github-action@master
63+
env:
64+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
65+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
66+
with:
67+
args: >
68+
-Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }}
69+
-Dsonar.branch.name=${{ github.event.workflow_run.head_branch }}

.github/workflows/validate-pr.yml

+11
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ jobs:
4444
/tmp/result.md
4545
- name: Upload job summary
4646
run: cat /tmp/result.md >> $GITHUB_STEP_SUMMARY
47+
- name: Save PR number to file
48+
if: github.event_name == 'pull_request'
49+
run: echo ${{ github.event.number }} > PR_NUMBER.txt
50+
- name: Archive PR number
51+
if: github.event_name == 'pull_request'
52+
uses: actions/upload-artifact@v3
53+
with:
54+
name: PR_NUMBER
55+
path: PR_NUMBER.txt
56+
57+

sonar-project.properties

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sonar.projectKey=open-vsx_publish-extensions
2+
sonar.organization=open-vsx
3+
4+
# This is the name and version displayed in the SonarCloud UI.
5+
#sonar.projectName=publish-extensions
6+
#sonar.projectVersion=1.0
7+
8+
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
9+
#sonar.sources=.
10+
11+
# Encoding of the source code. Default is default system encoding
12+
#sonar.sourceEncoding=UTF-8

0 commit comments

Comments
 (0)