-
Notifications
You must be signed in to change notification settings - Fork 341
208 lines (185 loc) · 7.07 KB
/
pr-tests.yml
File metadata and controls
208 lines (185 loc) · 7.07 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
name: PR Tests
on:
pull_request:
branches: [master]
types: [opened, synchronize, reopened]
# Path filtering moved to job level using dorny/paths-filter
# This ensures the workflow always runs and reports a status
# Ensure only one test run per PR at a time
concurrency:
group: pr-tests-${{ github.event.pull_request.number }}
cancel-in-progress: true
# Workflow-level permissions set to maximum needed by any job
# Individual jobs further restrict to only what they need
permissions:
contents: read
checks: write
statuses: write
pull-requests: write
jobs:
changes:
name: Detect Changes
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
should_test: ${{ steps.filter.outputs.src }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
src:
- 'UnityProject/Packages/com.jasonxudeveloper.jengine.core/**'
- 'UnityProject/Packages/com.jasonxudeveloper.jengine.util/**'
- 'UnityProject/Packages/com.jasonxudeveloper.jengine.ui/**'
- 'UnityProject/Assets/Tests/**'
- '.github/workflows/unity-tests.yml'
- '.github/workflows/pr-tests.yml'
run-tests:
name: Run Unity Tests
needs: changes
if: needs.changes.outputs.should_test == 'true'
permissions:
contents: read
checks: write
uses: ./.github/workflows/unity-tests.yml
secrets: inherit
skip-tests:
name: Skip Unity Tests
needs: changes
if: needs.changes.outputs.should_test == 'false'
runs-on: ubuntu-latest
permissions:
statuses: write
steps:
- name: Skip tests
run: echo "No relevant changes detected, skipping tests"
- name: Set PR check status (skipped)
uses: actions/github-script@v7
with:
script: |
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.payload.pull_request.head.sha,
state: 'success',
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
description: 'Skipped - no relevant changes',
context: 'Unity Tests'
});
upload-coverage:
name: Upload Coverage
needs: [changes, run-tests]
runs-on: ubuntu-latest
if: needs.changes.outputs.should_test == 'true' && needs.run-tests.result == 'success'
permissions:
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Download coverage artifacts
uses: actions/download-artifact@v4
with:
name: Coverage-results-2022.3.55f1
path: coverage
- name: List coverage files
run: |
echo "Coverage directory structure:"
find coverage -type f -name "*.xml" 2>/dev/null || echo "No XML files found"
- name: Fix coverage paths
run: |
# Unity test runner generates paths with /github/workspace/ prefix (Docker container path)
# Strip this prefix so Codecov can match paths to repository files
echo "Fixing coverage paths..."
find coverage -name "*.xml" -exec sed -i 's|/github/workspace/||g' {} \;
echo "Path fix complete. Sample paths after fix:"
find coverage -name "TestCoverageResults*.xml" -exec grep -h "fullPath=" {} \; | head -5 || true
- name: Upload coverage to Codecov (util package)
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage/**/TestCoverageResults*.xml
flags: util
name: jengine-util
fail_ci_if_error: true
verbose: true
- name: Upload coverage to Codecov (ui package)
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage/**/TestCoverageResults*.xml
flags: ui
name: jengine-ui
fail_ci_if_error: true
verbose: true
comment-results:
name: Comment Test Results
needs: [changes, run-tests, skip-tests]
runs-on: ubuntu-latest
if: always() && needs.changes.outputs.should_test == 'true'
permissions:
pull-requests: write
statuses: write
steps:
- name: Comment PR with test results
uses: actions/github-script@v7
with:
script: |
const testResults = `${{ needs.run-tests.outputs.test_results }}`;
const jobStatus = '${{ needs.run-tests.result }}';
let comment = testResults;
if (jobStatus === 'success') {
comment += '\n\n✅ All tests passed! The PR is ready for review.';
} else if (jobStatus === 'failure') {
comment += '\n\n❌ Some tests failed. Please fix the failing tests before merging.';
} else {
comment += '\n\n⚠️ Test execution was cancelled or encountered an error.';
}
comment += `\n\n<details><summary>View workflow run</summary>\n\n[Click here to view the full workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})\n\n</details>`;
// Find existing comment
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Unity Test Results')
);
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: comment
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: comment
});
}
- name: Set PR check status
uses: actions/github-script@v7
with:
script: |
const jobStatus = '${{ needs.run-tests.result }}';
const state = jobStatus === 'success' ? 'success' : 'failure';
const description = jobStatus === 'success'
? 'All Unity tests passed'
: 'Unity tests failed';
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: context.payload.pull_request.head.sha,
state: state,
target_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
description: description,
context: 'Unity Tests'
});