-
Notifications
You must be signed in to change notification settings - Fork 0
172 lines (142 loc) · 6.57 KB
/
sonarcloud-and-mutations.yml
File metadata and controls
172 lines (142 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
name: 'SonarCloud and Mutation Tests'
on:
workflow_dispatch:
push: # First pipeline to run when deploy a new version
branches:
- main
paths:
- "src/**" # Only run when exists changes in source code
- "tests/**" # Only run when exists changes in tests code
pull_request:
types: [opened, reopened, edited, synchronize]
branches:
- main
env:
FRAMEWORK_VERSION: 'net9.0'
PROJECT_FILE: 'PowerUtils.BenchmarkDotnet.Reporter.csproj'
TEST_PROJECT_PATH: 'tests/PowerUtils.BenchmarkDotnet.Reporter.Tests/PowerUtils.BenchmarkDotnet.Reporter.Tests.csproj'
# Set up the .NET environment to improve test performance and reliability
DOTNET_CLI_TELEMETRY_OPTOUT: true # Disable .NET CLI telemetry
DOTNET_NOLOGO: true # Disable .NET CLI logo
DOTNET_SYSTEM_CONSOLE_ALLOW_ANSI_COLOR_REDIRECTION: true # Enable ANSI color redirection
TERM: xterm # Enable ANSI color redirection
NUGET_XMLDOC_MODE: skip # Disable NuGet XML documentation generation
SONAR_ORGANIZATION: 'technobre'
permissions:
pull-requests: write # To can create a comment with the results
jobs:
sonar-scanner:
name: "Sonar Scanner and Mutation Tests"
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: actions/checkout@v6.0.1
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: "Define branch name for Stryker"
env:
GH_REF: ${{ github.ref }}
GH_HEAD_REF: ${{ github.head_ref }}
GH_RUN_NUMBER: ${{ github.run_number }}
run: |
# Sanitize branch name
SAFE_HEAD_REF="$(printf '%s' "$GH_HEAD_REF" | tr -c 'A-Za-z0-9._-' '-')"
if [[ "$GH_REF" == "refs/heads/main" ]]; then
echo "STRYKER_BRANCH=main" >> $GITHUB_ENV
else
printf 'STRYKER_BRANCH=%s-%s\n' "$SAFE_HEAD_REF" "$GH_RUN_NUMBER" >> "$GITHUB_ENV"
fi
- name: "Display branch name for Stryker"
run: echo "Stryker branch is '${{ env.STRYKER_BRANCH }}'"
- name: "Setup .NET"
uses: actions/setup-dotnet@v5.0.1
with:
global-json-file: 'global.json'
# Hack reasons:
# - The last version of the `xunit.runner.visualstudio` doesn't generate the report for some OLD SDK versions like .net 5
# - The Stryker versions after 4.0.0 is not compatible with net7.0
- name: "Replace TargetFrameworks"
run: |
find . -type f -name "*.csproj" -exec sed -i 's/<TargetFrameworks>.*<\/TargetFrameworks>/<TargetFrameworks>${{ env.FRAMEWORK_VERSION }}<\/TargetFrameworks>/g' {} +
- name: "Set up JDK"
uses: actions/setup-java@v5.0.0
with:
distribution: 'adopt'
java-version: '17'
- name: "Restore .NET Tools"
run: dotnet tool restore
- name: "Create cache directory"
run: mkdir -p ~/sonar/cache
- name: "Cache SonarCloud packages"
uses: actions/cache@v4
with:
path: ~/sonar/cache
key: ${{ runner.os }}-sonar-${{ hashFiles('**/*.csproj') }}
restore-keys: |
${{ runner.os }}-sonar-
${{ runner.os }}-
- name: "Cache SonarCloud scanner"
id: cache-sonar-scanner
uses: actions/cache@v4
with:
path: ./.sonar/scanner
key: ${{ runner.os }}-sonar-scanner
restore-keys: ${{ runner.os }}-sonar-scanner
- name: "Install SonarCloud scanner"
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
run: |
mkdir -p ./.sonar/scanner
dotnet tool update dotnet-sonarscanner --tool-path ./.sonar/scanner
dotnet tool update dotnet-coverage --tool-path ./.sonar/scanner
- name: "Run Stryker"
env:
STRYKER_API_KEY: ${{ secrets.STRYKER_API_KEY }}
run: |
dotnet stryker \
-tp ${{ env.TEST_PROJECT_PATH }} \
-p ${{ env.PROJECT_FILE }} \
--reporter json \
--reporter markdown \
--reporter progress \
--reporter dashboard --dashboard-api-key $STRYKER_API_KEY \
--version ${{ env.STRYKER_BRANCH }} \
-O ${{ github.workspace }}/mutations
- name: "Convert report to Sonar"
run: jq -f tests/mutation-report-to-sonar.jq '${{ github.workspace }}/mutations/reports/mutation-report.json' > '${{ github.workspace }}/mutations/reports/mutation-sonar.json'
- name: "Add Stryker Report in PR Comment"
uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405
if: github.event_name == 'pull_request'
with:
header: stryker-report
hide_and_recreate: true
hide_classify: "OUTDATED"
message: |
🚀 **Stryker report generated** 🚀
[](https://dashboard.stryker-mutator.io/reports/github.com/${{ github.repository_owner }}/${{ github.event.repository.name }}/${{ env.STRYKER_BRANCH }})
To more details: https://dashboard.stryker-mutator.io/reports/github.com/${{ github.repository_owner }}/${{ github.event.repository.name }}/${{ env.STRYKER_BRANCH }}
- name: "Publish report"
run: cat $(find ${{ github.workspace }}/mutations/reports/* -name "*.md") > $GITHUB_STEP_SUMMARY
- name: "Build and analyze"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_PROJECT_KEY: ${{ secrets.SONAR_PROJECT_KEY }}
run: |
./.sonar/scanner/dotnet-sonarscanner begin \
/k:"$SONAR_PROJECT_KEY" \
/o:"${{ env.SONAR_ORGANIZATION }}" \
/d:sonar.token="$SONAR_TOKEN" \
/d:sonar.host.url="https://sonarcloud.io" \
/d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml \
/d:sonar.vstest.reportsPaths="**/*.trx" \
/d:sonar.projectBaseDir="$(pwd)" \
/d:sonar.scanner.scanAll=false \
/d:sonar.cs.analyzeRazorCode=false \
/d:sonar.cs.analyzeGeneratedCode=false \
/d:sonar.cs.file.suffixes=.cs \
/d:sonar.githubactions.activate=true \
/d:sonar.externalIssuesReportPaths="${{ github.workspace }}/mutations/reports/mutation-sonar.json" \
/s:"$(pwd)/SonarQube.Analysis.xml"
dotnet build -c Release --no-incremental
./.sonar/scanner/dotnet-coverage collect "dotnet test -c Release --no-restore --no-build --logger trx" -f xml -o "coverage.xml"
./.sonar/scanner/dotnet-sonarscanner end /d:sonar.token="$SONAR_TOKEN"