-
Notifications
You must be signed in to change notification settings - Fork 3
210 lines (191 loc) · 7.61 KB
/
pr-check-dev-branch.yml
File metadata and controls
210 lines (191 loc) · 7.61 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
name: Dev - CI & Unit Tests
permissions:
contents: read
pull-requests: write
on:
pull_request:
branches:
- dev
jobs:
# ---------------------------------------------------------------------
# 1. Validate PR source branch
# ---------------------------------------------------------------------
check-dev-branch:
runs-on: ubuntu-latest
outputs:
branch-allowed: ${{ steps.branch-check.outputs.allowed }}
steps:
- name: Validate PR source branch
id: branch-check
run: |
echo "Source branch: ${GITHUB_HEAD_REF}"
if [[ ${GITHUB_HEAD_REF} =~ ^feature/ ]] || \
[[ ${GITHUB_HEAD_REF} =~ ^hotfix/ ]] || \
[[ ${GITHUB_HEAD_REF} =~ ^bugfix/ ]] || \
[[ ${GITHUB_HEAD_REF} == "test" ]] || \
[[ ${GITHUB_HEAD_REF} == "main" ]]; then
echo "allowed=true" >> $GITHUB_OUTPUT
else
echo "❌ ERROR: PR into dev must come from:"
echo " feature/*, hotfix/*, bugfix/*, test, or main"
echo "allowed=false" >> $GITHUB_OUTPUT
exit 1
fi
# ---------------------------------------------------------------------
# 2. Discover all *Tests.csproj files and output into a matrix array
# ---------------------------------------------------------------------
discover-test-projects:
needs: check-dev-branch
if: needs.check-dev-branch.outputs.branch-allowed == 'true'
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.discover.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- id: discover
run: |
echo "🔍 Discovering test projects..."
PROJECTS=$(find applications/Unity.GrantManager -name "*Tests.csproj")
echo "$PROJECTS"
# Convert list to JSON array for GitHub matrix
MATRIX=$(printf '%s\n' $PROJECTS | jq -R -s -c 'split("\n")[:-1]')
echo "matrix=$MATRIX" >> $GITHUB_OUTPUT
# ---------------------------------------------------------------------
# 3. Run tests for each project in parallel (matrix)
# ---------------------------------------------------------------------
test-project:
needs: discover-test-projects
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
project: ${{ fromJson(needs.discover-test-projects.outputs.matrix) }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: "9.0.x"
- name: Ensure TestResults folder exists
run: mkdir -p TestResults
- name: Run unit tests
run: |
NAME=$(basename "${{ matrix.project }}" .csproj)
TRX="TestResults/${NAME}.trx"
echo "▶ Running: $NAME"
dotnet test "${{ matrix.project }}" \
--logger "trx;LogFileName=${NAME}.trx" \
--results-directory TestResults
- uses: actions/upload-artifact@v4
with:
name: test-output-${{ strategy.job-index }}
path: TestResults/
# ---------------------------------------------------------------------
# 4. Merge all TRX and produce final results
# ---------------------------------------------------------------------
aggregate-results:
needs: test-project
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
path: artifacts/
- name: Install ReportGenerator
run: dotnet tool install -g dotnet-reportgenerator-globaltool
- name: Add dotnet tools to PATH
run: echo "$HOME/.dotnet/tools" >> $GITHUB_PATH
- name: List TRX files
run: |
echo "🔍 TRX files:"
find artifacts/ -name "*.trx"
- name: Extract totals from merged trx files
id: extract
run: |
PASSED=0
FAILED=0
SKIPPED=0
for file in artifacts/**/*.trx; do
COUNTERS=$(grep "<Counters " "$file")
PASSED_FILE=$(echo "$COUNTERS" | sed -n 's/.*passed="\([0-9]*\)".*/\1/p')
FAILED_FILE=$(echo "$COUNTERS" | sed -n 's/.*failed="\([0-9]*\)".*/\1/p')
NOT_EXECUTED_FILE=$(echo "$COUNTERS" | sed -n 's/.*notExecuted="\([0-9]*\)".*/\1/p')
PASSED=$((PASSED + PASSED_FILE))
FAILED=$((FAILED + FAILED_FILE))
SKIPPED=$((SKIPPED + NOT_EXECUTED_FILE))
done
echo "Passed: $PASSED, Failed: $FAILED, Skipped: $SKIPPED"
echo "passed=$PASSED" >> $GITHUB_OUTPUT
echo "failed=$FAILED" >> $GITHUB_OUTPUT
echo "skipped=$SKIPPED" >> $GITHUB_OUTPUT
- uses: actions/upload-artifact@v4
with:
name: merged-test-results
path: merged/
- name: Build test badge
id: badge
run: |
if [[ "${{ steps.extract.outputs.failed }}" -gt 0 ]]; then
echo "badge=" >> $GITHUB_OUTPUT
else
echo "badge=" >> $GITHUB_OUTPUT
fi
- name: Post PR Comment
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body: |
## 🧪 Unit Test Results (Parallel Execution)
${{ steps.badge.outputs.badge }}
### 📊 Summary
| Result | Count |
|--------|-------|
| ✅ Passed | `${{ steps.extract.outputs.passed }}` |
| ❌ Failed | `${{ steps.extract.outputs.failed }}` |
| ⚠️ Skipped | `${{ steps.extract.outputs.skipped }}` |
### 📄 HTML Reports
- **Merged Tests (HTML):** Included in artifacts
_Generated automatically by CI._
- name: Send Microsoft Teams Notification
env:
WEBHOOK_URL: ${{ secrets.TEAMS_UNITY_NOTIFICATIONS_CHANNEL }} # NOSONAR
PR_TITLE: ${{ github.event.pull_request.title }}
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PASSED: ${{ steps.extract.outputs.failed == '0' && 'true' || 'false' }}
PASSED_COUNT: ${{ steps.extract.outputs.passed }}
FAILED_COUNT: ${{ steps.extract.outputs.failed }}
SKIPPED_COUNT: ${{ steps.extract.outputs.skipped }}
run: |
if [[ "$PASSED" == "true" ]]; then
COLOR="00FF00"
STATUS="🟢 Tests Passed $PR_TITLE (by $PR_AUTHOR)"
else
COLOR="FF0000"
STATUS="🔴 Tests Failed $PR_TITLE (by $PR_AUTHOR)"
fi
JSON=$(cat <<EOF
{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "$COLOR",
"activityImage": "https://bcgov.github.io/unity-docs/images/UnityLogo.png",
"summary": "Unit Test Results",
"title": "$STATUS - PR #$PR_NUMBER",
"sections": [{
"facts": [
{ "name": "Passed", "value": "$PASSED_COUNT" },
{ "name": "Failed", "value": "$FAILED_COUNT" },
{ "name": "Skipped", "value": "$SKIPPED_COUNT" }
],
"markdown": true
}]
}
EOF
)
if [ -n "$WEBHOOK_URL" ]; then
curl -H "Content-Type: application/json" -d "$JSON" "$WEBHOOK_URL"
else
echo "⚠️ TEAMS_NOTIFICATION_WEBHOOK_URL secret not configured, skipping notification"
fi
- name: Fail if tests failed
if: steps.extract.outputs.failed != '0'
run: exit 1