Repository navigation
374 lines (330 loc) · 15.2 KB
/
Copy pathskill-coverage.yml
File metadata and controls
374 lines (330 loc) · 15.2 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
name: skill-coverage
# NOTE: The issue_comment trigger sources this workflow from the default branch.
# The /skill-coverage slash command will only work after this workflow is merged to main.
on:
pull_request:
paths:
- 'plugins/**'
- 'tests/**'
issue_comment:
types: [created]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.event.issue.number || github.ref }}
cancel-in-progress: true
permissions: {}
jobs:
# ──────────────────────────────────────────────────────────────
# Job 1: Analyze — runs trusted script with read-only permissions
# Checks out base branch for tooling, PR head into a
# separate worktree so untrusted code is never executed.
# ──────────────────────────────────────────────────────────────
analyze:
if: >-
github.event_name == 'pull_request' ||
(github.event_name == 'issue_comment' &&
github.event.issue.pull_request &&
(github.event.comment.body == '/skill-coverage' ||
startsWith(github.event.comment.body, '/skill-coverage ') ||
startsWith(github.event.comment.body, '/skill-coverage\n')))
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
count: ${{ steps.detect.outputs.count }}
pr_number: ${{ github.event.pull_request.number || steps.pr-ref.outputs.pr_number }}
steps:
- name: Check commenter permissions
id: check-perms
if: github.event_name == 'issue_comment'
uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553 # v9
with:
script: |
const { data: perm } = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: context.actor
});
const allowed = ['admin', 'write', 'maintain'];
if (!allowed.includes(perm.permission)) {
core.setFailed(`User ${context.actor} does not have write access (permission: ${perm.permission}).`);
}
- name: Parse slash command arguments
id: parse-command
if: github.event_name == 'issue_comment'
shell: bash
env:
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
# Extract the first line of the command
cmd_line=$(echo "$COMMENT_BODY" | head -n 1)
# Strip the command prefix
args="${cmd_line#/skill-coverage}"
args="$(echo "$args" | xargs)" # trim whitespace
if [ -z "$args" ]; then
echo "mode=auto" >> "$GITHUB_OUTPUT"
echo "plugin=" >> "$GITHUB_OUTPUT"
echo "skill=" >> "$GITHUB_OUTPUT"
else
# Parse: plugin [skill]
plugin=$(echo "$args" | awk '{print $1}')
skill=$(echo "$args" | awk '{print $2}')
echo "mode=explicit" >> "$GITHUB_OUTPUT"
echo "plugin=$plugin" >> "$GITHUB_OUTPUT"
echo "skill=$skill" >> "$GITHUB_OUTPUT"
fi
- name: Add reaction to comment
if: github.event_name == 'issue_comment'
continue-on-error: true
uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553 # v9
with:
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: 'eyes'
});
- name: Get PR ref for comment trigger
id: pr-ref
if: github.event_name == 'issue_comment'
uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553 # v9
with:
script: |
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
core.setOutput('sha', pr.data.head.sha);
core.setOutput('base_sha', pr.data.base.sha);
core.setOutput('pr_number', context.issue.number);
# Checkout the default branch for trusted tooling (Measure-SkillCoverage.ps1),
# then fetch the PR head into a separate worktree so untrusted code is never executed.
- name: Checkout base branch (trusted tooling)
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v6
with:
persist-credentials: false
- name: Fetch PR head into worktree
shell: bash
env:
PR_REF: ${{ github.event_name == 'issue_comment' && format('refs/pull/{0}/head', steps.pr-ref.outputs.pr_number) || format('refs/pull/{0}/head', github.event.pull_request.number) }}
run: |
git fetch --depth=1 origin "$PR_REF":pr-head-ref
git worktree add pr-head pr-head-ref
- name: Fetch full history for diff
run: git fetch --unshallow || true
- name: Detect changed plugins and skills
id: detect
shell: pwsh
env:
EVENT_NAME: ${{ github.event_name }}
CMD_MODE: ${{ steps.parse-command.outputs.mode }}
CMD_PLUGIN: ${{ steps.parse-command.outputs.plugin }}
CMD_SKILL: ${{ steps.parse-command.outputs.skill }}
PR_BASE_SHA: ${{ github.event.pull_request.base.sha || steps.pr-ref.outputs.base_sha }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha || steps.pr-ref.outputs.sha }}
run: |
# If explicit plugin/skill was provided via slash command, use that directly
if ($env:EVENT_NAME -eq 'issue_comment' -and $env:CMD_MODE -eq 'explicit') {
$targets = @([PSCustomObject]@{ Plugin = $env:CMD_PLUGIN; Skill = $env:CMD_SKILL })
$json = $targets | ConvertTo-Json -Compress -AsArray
"targets=$json" >> $env:GITHUB_OUTPUT
"count=$($targets.Count)" >> $env:GITHUB_OUTPUT
return
}
# Detect changed files between base and head
$diff = git diff --name-only "$env:PR_BASE_SHA" "$env:PR_HEAD_SHA" 2>$null
if ($LASTEXITCODE -ne 0) {
# Fallback: diff against merge-base
$mergeBase = git merge-base "$env:PR_BASE_SHA" "$env:PR_HEAD_SHA"
$diff = git diff --name-only $mergeBase "$env:PR_HEAD_SHA"
}
$changedFiles = $diff -split "`n" | Where-Object { $_ }
# Collect unique plugin/skill pairs from changed paths
$seen = @{}
$pluginWide = @{}
$targets = @()
foreach ($file in $changedFiles) {
# Match plugins/<plugin>/skills/<skill>/...
if ($file -match '^plugins/([^/]+)/skills/([^/]+)/') {
$key = "$($Matches[1])/$($Matches[2])"
if (-not $seen.ContainsKey($key)) {
$seen[$key] = $true
$targets += [PSCustomObject]@{ Plugin = $Matches[1]; Skill = $Matches[2] }
}
}
# Match tests/<plugin>/<skill>/...
elseif ($file -match '^tests/([^/]+)/([^/]+)/') {
$key = "$($Matches[1])/$($Matches[2])"
if (-not $seen.ContainsKey($key)) {
$seen[$key] = $true
$targets += [PSCustomObject]@{ Plugin = $Matches[1]; Skill = $Matches[2] }
}
}
# Match plugins/<plugin>/ level changes (plugin.json etc.) - run all skills
elseif ($file -match '^plugins/([^/]+)/[^/]+$') {
$pluginWide[$Matches[1]] = $true
}
}
# For plugin-wide changes, replace per-skill targets with a single plugin-wide target
foreach ($plugin in $pluginWide.Keys) {
$targets = @($targets | Where-Object { $_.Plugin -ne $plugin })
$targets += [PSCustomObject]@{ Plugin = $plugin; Skill = '' }
}
if ($targets.Count -eq 0) {
Write-Host "No plugin/skill changes detected."
"count=0" >> $env:GITHUB_OUTPUT
"targets=[]" >> $env:GITHUB_OUTPUT
return
}
Write-Host "Detected $($targets.Count) target(s):"
$targets | ForEach-Object { Write-Host " - $($_.Plugin)$(if ($_.Skill) { "/$($_.Skill)" })" }
$json = $targets | ConvertTo-Json -Compress -AsArray
"targets=$json" >> $env:GITHUB_OUTPUT
"count=$($targets.Count)" >> $env:GITHUB_OUTPUT
- name: Run skill coverage
if: steps.detect.outputs.count != '0'
id: coverage
shell: pwsh
env:
TARGETS_JSON: ${{ steps.detect.outputs.targets }}
run: |
$targets = $env:TARGETS_JSON | ConvertFrom-Json
$allJson = @()
$summary = @()
# Run the trusted script from base branch against PR content in the worktree
$scriptPath = "$env:GITHUB_WORKSPACE/eng/skill-coverage/Measure-SkillCoverage.ps1"
$prRoot = "$env:GITHUB_WORKSPACE/pr-head"
foreach ($t in $targets) {
$args = @{ Format = 'Json'; RepoRoot = $prRoot }
if ($t.Plugin) { $args.PluginName = $t.Plugin }
if ($t.Skill) { $args.SkillName = $t.Skill }
Write-Host "::group::Coverage: $($t.Plugin)$(if ($t.Skill) { "/$($t.Skill)" })"
try {
$result = & $scriptPath @args
$parsed = ($result | Out-String) | ConvertFrom-Json
# Handle single or array results
$reports = if ($parsed -is [array]) { $parsed } else { @($parsed) }
foreach ($r in $reports) {
$allJson += $r
if ($r.coverageMode -eq 'consumer') {
$summary += "| :information_source: | ``$($r.plugin)`` | ``$($r.skill)`` | - | **N/A (reference-only)** |"
continue
}
$pct = $r.summary.percentage
$icon = if ($pct -ge 80) { ':white_check_mark:' }
elseif ($pct -ge 50) { ':warning:' }
else { ':x:' }
$summary += "| $icon | ``$($r.plugin)`` | ``$($r.skill)`` | $($r.summary.coveredPoints)/$($r.summary.totalPoints) | **$pct%** |"
}
}
catch {
Write-Host "::warning::Failed to analyze $($t.Plugin)/$($t.Skill): $_"
$summary += "| :grey_question: | ``$($t.Plugin)`` | ``$($t.Skill)`` | — | error |"
}
Write-Host "::endgroup::"
}
# Build markdown report
$md = @()
$md += '## Skill Coverage Report'
$md += ''
$md += '| | Plugin | Skill | Covered | Coverage |'
$md += '|---|--------|-------|---------|----------|'
$md += $summary
$md += ''
# Add uncovered items details if any
$uncoveredDetails = @()
foreach ($r in $allJson) {
if ($r.coverageMode -ne 'consumer' -and $r.uncovered.Count -gt 0) {
$uncoveredDetails += "<details><summary>Uncovered: <code>$($r.plugin)/$($r.skill)</code></summary>"
$uncoveredDetails += ''
foreach ($u in $r.uncovered) {
$uncoveredDetails += "- **[$($u.category)]** $($u.description) (line $($u.line))"
}
$uncoveredDetails += ''
$uncoveredDetails += '</details>'
$uncoveredDetails += ''
}
}
if ($uncoveredDetails.Count -gt 0) {
$md += $uncoveredDetails
}
# Write the report to a file for the comment job
$report = $md -join "`n"
$report | Out-File -FilePath coverage-report.md -Encoding utf8
# Also write to step summary
$report >> $env:GITHUB_STEP_SUMMARY
- name: Upload coverage report
if: steps.detect.outputs.count != '0'
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: coverage-report
path: coverage-report.md
retention-days: 1
# ──────────────────────────────────────────────────────────────
# Job 2: Comment — posts results with write permissions only.
# No PR code is checked out or executed in this job.
# ──────────────────────────────────────────────────────────────
comment:
needs: analyze
if: >-
needs.analyze.outputs.count != '0' &&
(github.event_name != 'pull_request' ||
github.event.pull_request.head.repo.full_name == github.repository)
runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: write
steps:
- name: Download coverage report
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8
with:
name: coverage-report
- name: Post or update PR comment
uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553 # v9
env:
PR_NUMBER: ${{ needs.analyze.outputs.pr_number }}
with:
script: |
const fs = require('fs');
const body = fs.readFileSync('coverage-report.md', 'utf8');
const marker = '<!-- skill-coverage-report -->';
const fullBody = `${marker}\n${body}`;
const prNumber = parseInt(process.env.PR_NUMBER, 10);
// Paginate to find existing marker comment (handles PRs with >100 comments)
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100
});
const existing = comments.find(c => c.body && c.body.startsWith(marker));
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: fullBody
});
core.info(`Updated comment ${existing.id}`);
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: fullBody
});
core.info('Created new coverage comment');
}
- name: Post no-changes comment
if: needs.analyze.outputs.count == '0' && github.event_name == 'issue_comment'
continue-on-error: true
uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553 # v9
with:
script: |
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '**Skill Coverage:** No plugin/skill changes detected in this PR. Use `/skill-coverage <plugin> [skill]` to target specific skills.'
});