Skip to content

Commit cea83e5

Browse files
authored
Merge pull request #905 from microsoftgraph/andrueastman/setupSonar
ci: setup sonarcloud
2 parents c608fa3 + 36fd11a commit cea83e5

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

.github/workflows/sonarcloud.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Sonarcloud
2+
on:
3+
workflow_dispatch:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
permissions:
11+
contents: read
12+
pull-requests: read
13+
14+
env:
15+
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
16+
17+
jobs:
18+
checksecret:
19+
name: check if SONAR_TOKEN is set in github secrets
20+
runs-on: ubuntu-latest
21+
outputs:
22+
is_SONAR_TOKEN_set: ${{ steps.checksecret_job.outputs.is_SONAR_TOKEN_set }}
23+
steps:
24+
- name: Check whether unity activation requests should be done
25+
id: checksecret_job
26+
run: |
27+
echo "is_SONAR_TOKEN_set=${{ env.SONAR_TOKEN != '' }}" >> $GITHUB_OUTPUT
28+
build:
29+
needs: [checksecret]
30+
if: needs.checksecret.outputs.is_SONAR_TOKEN_set == 'true'
31+
name: Build
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: Set up JDK 21
35+
uses: actions/setup-java@v4
36+
with:
37+
distribution: "adopt"
38+
java-version: 21
39+
- name: Setup .NET
40+
uses: actions/setup-dotnet@v4
41+
with:
42+
dotnet-version: |
43+
8.x
44+
- uses: actions/checkout@v4
45+
with:
46+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
47+
- name: Cache SonarCloud packages
48+
uses: actions/cache@v4
49+
with:
50+
path: ~/.sonar/cache
51+
key: ${{ runner.os }}-sonar
52+
restore-keys: ${{ runner.os }}-sonar
53+
- name: Cache SonarCloud scanner
54+
id: cache-sonar-scanner
55+
uses: actions/cache@v4
56+
with:
57+
path: ./.sonar/scanner
58+
key: ${{ runner.os }}-sonar-scanner
59+
restore-keys: ${{ runner.os }}-sonar-scanner
60+
- name: Install SonarCloud scanner
61+
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
62+
shell: pwsh
63+
run: |
64+
New-Item -Path ./.sonar/scanner -ItemType Directory
65+
dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
66+
- name: Build and analyze
67+
env:
68+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
69+
CollectCoverage: true
70+
CoverletOutputFormat: "opencover" # https://github.com/microsoft/vstest/issues/4014#issuecomment-1307913682
71+
shell: pwsh
72+
run: |
73+
./.sonar/scanner/dotnet-sonarscanner begin /k:"microsoftgraph_msgraph-sdk-dotnet-core" /o:"microsoftgraph2" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="tests/Microsoft.Graph.DotnetCore.Core.Test/coverage.opencover.xml"
74+
dotnet workload restore
75+
dotnet build
76+
dotnet test Microsoft.Graph.Core.sln --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --framework net6.0
77+
./.sonar/scanner/dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"

tests/Microsoft.Graph.DotnetCore.Core.Test/Microsoft.Graph.DotnetCore.Core.Test.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
2727
<PackageReference Include="Moq" Version="4.20.70" />
2828
<PackageReference Include="xunit" Version="2.9.0" />
29+
<PackageReference Include="coverlet.collector" Version="6.0.2">
30+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
31+
<PrivateAssets>all</PrivateAssets>
32+
</PackageReference>
33+
<PackageReference Include="coverlet.msbuild" Version="6.0.2">
34+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
35+
<PrivateAssets>all</PrivateAssets>
36+
</PackageReference>
2937
<ProjectReference Include="..\..\src\Microsoft.Graph.Core\Microsoft.Graph.Core.csproj" />
3038
</ItemGroup>
3139
<ItemGroup>

0 commit comments

Comments
 (0)