-
Notifications
You must be signed in to change notification settings - Fork 0
147 lines (119 loc) · 5.67 KB
/
sonarcloud-and-mutations.yml
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
name: 'SonarCloud and Mutation Tests'
on:
workflow_dispatch: # To can dispatch manually
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.xUnit.Extensions.csproj'
TEST_PROJECT_PATH: 'tests/PowerUtils.xUnit.Extensions.Tests/PowerUtils.xUnit.Extensions.Tests.csproj'
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/[email protected]
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: "Define branch name for Stryker"
run: |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "STRYKER_BRANCH=main" >> $GITHUB_ENV
else
echo "STRYKER_BRANCH=${{ github.head_ref }}-${{ github.run_number }}" >> $GITHUB_ENV
fi
- name: "Display branch name for Stryker"
run: echo "Stryker branch is '${{ env.STRYKER_BRANCH }}'"
- name: "Setup .NET"
uses: actions/[email protected]
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/[email protected]
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"
run: |
dotnet stryker \
-tp ${{ env.TEST_PROJECT_PATH }} \
-p ${{ env.PROJECT_FILE }} \
--reporter json \
--reporter markdown \
--reporter progress \
--reporter dashboard --dashboard-api-key ${{ secrets.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: "Comment PR"
uses: actions/[email protected]
if: github.event_name == 'pull_request'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { issue: { number: issue_number }, repo: { owner, repo } } = context;
const message = `🚀 **Stryker report generated** 🚀
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.jparrowsec.cn%2F${{ github.repository_owner }}%2F${{ github.event.repository.name }}%2F${{ env.STRYKER_BRANCH }})](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 }}`;
github.rest.issues.createComment({
issue_number,
owner,
repo,
body: message
});
- 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 }}
shell: pwsh
run: |
./.sonar/scanner/dotnet-sonarscanner begin /k:"${{ secrets.SONAR_PROJECT_KEY }}" /o:"${{ secrets.SONAR_ORGANIZATION }}" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml /d:sonar.projectBaseDir="$(pwd)" /d:sonar.externalIssuesReportPaths="${{ github.workspace }}/mutations/reports/mutation-sonar.json"
dotnet build -c Release --no-incremental
./.sonar/scanner/dotnet-coverage collect "dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover -c Release --no-restore --no-build" -f xml -o "coverage.xml"
./.sonar/scanner/dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"