Skip to content

Commit dff9e51

Browse files
author
Andrew Omondi
committed
ci: setup sonarcloud
1 parent c608fa3 commit dff9e51

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

.github/workflows/sonarcloud.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 17
35+
uses: actions/setup-java@v4
36+
with:
37+
distribution: "adopt"
38+
java-version: 17
39+
- name: Setup .NET
40+
uses: actions/setup-dotnet@v4
41+
with: # At the moment the scanner requires dotnet 5 https://www.nuget.org/packages/dotnet-sonarscanner
42+
dotnet-version: |
43+
5.x
44+
6.x
45+
- uses: actions/checkout@v4
46+
with:
47+
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
48+
- name: Cache SonarCloud packages
49+
uses: actions/cache@v4
50+
with:
51+
path: ~/.sonar/cache
52+
key: ${{ runner.os }}-sonar
53+
restore-keys: ${{ runner.os }}-sonar
54+
- name: Cache SonarCloud scanner
55+
id: cache-sonar-scanner
56+
uses: actions/cache@v4
57+
with:
58+
path: ./.sonar/scanner
59+
key: ${{ runner.os }}-sonar-scanner
60+
restore-keys: ${{ runner.os }}-sonar-scanner
61+
- name: Install SonarCloud scanner
62+
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
63+
shell: pwsh
64+
run: |
65+
New-Item -Path ./.sonar/scanner -ItemType Directory
66+
dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
67+
- name: Build and analyze
68+
env:
69+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
70+
CollectCoverage: true
71+
CoverletOutputFormat: "opencover" # https://github.com/microsoft/vstest/issues/4014#issuecomment-1307913682
72+
shell: pwsh
73+
run: |
74+
./.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"
75+
dotnet workload restore
76+
dotnet build
77+
dotnet test Microsoft.Graph.Core.sln --no-build --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --framework net6.0
78+
./.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)