Repository navigation
519 lines (482 loc) · 23.3 KB
/
Copy pathdoc-check.yaml
File metadata and controls
519 lines (482 loc) · 23.3 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# This workflow checks if a PR requires documentation updates.
# It creates a Coder Agent chat session that uses AI to analyze the PR
# changes, search existing docs, and comment with recommendations.
#
# Triggers:
# - New PR opened: Initial documentation review
# - PR updated (synchronize): Re-review after changes
# - Label "doc-check" added: Manual trigger for review
# - PR marked ready for review: Review when draft is promoted
# - PR merged (closed with merged == true): Delete the review workspace
# - Workflow dispatch: Manual run with PR URL
#
# Note: This workflow requires access to secrets and will be skipped for:
# - Any PR where secrets are not available
# For these PRs, maintainers can manually trigger via workflow_dispatch.
name: AI Documentation Check
on:
pull_request:
types:
- opened
- synchronize
- labeled
- ready_for_review
- closed
workflow_dispatch:
inputs:
pr_url:
description: "Pull Request URL to check"
required: true
type: string
permissions: {}
jobs:
doc-check:
name: Analyze PR for Documentation Updates Needed
runs-on: ubuntu-latest
# Run on: opened, synchronize, labeled (with doc-check label), ready_for_review, or workflow_dispatch
# Skip draft PRs unless manually triggered
if: |
(
github.event.action == 'opened' ||
github.event.action == 'synchronize' ||
github.event.label.name == 'doc-check' ||
github.event.action == 'ready_for_review' ||
github.event_name == 'workflow_dispatch'
) &&
(github.event.pull_request.draft == false || github.event_name == 'workflow_dispatch') &&
(github.event_name == 'workflow_dispatch' || github.event.pull_request.head.repo.full_name == github.repository)
# Runs for one PR share its review workspace. Queue them, so one run
# cannot stop or delete the workspace while another run reviews in it.
concurrency:
group: doc-check-${{ github.event.pull_request.number || inputs.pr_url }}
cancel-in-progress: false
timeout-minutes: 50
permissions:
contents: read
pull-requests: write
steps:
- name: Check if secrets are available
id: check-secrets
env:
CODER_URL: ${{ secrets.DOC_CHECK_CODER_URL }}
CODER_TOKEN: ${{ secrets.DOC_CHECK_CODER_SESSION_TOKEN }}
run: |
if [[ -z "${CODER_URL}" || -z "${CODER_TOKEN}" ]]; then
echo "skip=true" >> "${GITHUB_OUTPUT}"
echo "Secrets not available - skipping doc-check."
echo "This is expected for PRs where secrets are not available."
echo "Maintainers can manually trigger via workflow_dispatch if needed."
{
echo "⚠️ Workflow skipped: Secrets not available"
echo ""
echo "This workflow requires secrets that are unavailable for this run."
echo "Maintainers can manually trigger via workflow_dispatch if needed."
} >> "${GITHUB_STEP_SUMMARY}"
else
echo "skip=false" >> "${GITHUB_OUTPUT}"
fi
# A review costs the full skill context before it can conclude anything,
# so decide here instead of in the model when no changed file can have a
# documentation surface. Path classes live in
# .claude/skills/doc-check/path-priors.md; keep the two in sync.
- name: Classify changed paths
if: steps.check-secrets.outputs.skip != 'true' && github.event_name == 'pull_request'
id: filter
uses: dorny/paths-filter@ceb8a2b8f2d89434be7ff52d3de7ec3738c5cc9d # v4.0.3
with:
filters: |
all:
- "**"
nodocs:
# Extension patterns use the `**/*` form, not `**`. The globber
# treats a `**` glued to a suffix inconsistently: `**.test.ts`
# matches at any depth, but `**_test.go` only matches the repo
# root. `**/*` behaves the same at every depth, including the
# root, so every pattern here uses it.
# Tests, stories, and fixtures.
- "**/*_test.go"
- "**/*.test.ts"
- "**/*.test.tsx"
- "**/*.stories.tsx"
- "**/testdata/**"
# Generated output. Changes belong in the generator.
- "docs/reference/cli/**"
- "site/src/api/typesGenerated.ts"
- "**/*.pb.go"
- "**/*.golden"
- "coderd/apidoc/swagger.json"
- "coderd/apidoc/docs.go"
# Dependency and toolchain manifests.
- "go.mod"
- "go.sum"
- "package.json"
- "pnpm-lock.yaml"
- "site/package.json"
- "site/pnpm-lock.yaml"
- "**/mise.toml"
# Build, lint, and CI plumbing. Docs-affecting CI is carved back
# out by the ci_docs filter below.
- ".github/**"
- "Makefile"
- ".golangci.yaml"
- ".editorconfig"
- ".gitattributes"
- ".gitignore"
- "**/biome.jsonc"
- "**/.markdownlint.jsonc"
- "**/.markdownlint-cli2.jsonc"
# Styling only. A restyle changes no documented behavior.
- "**/*.css"
- "site/components.json"
# Internal environments and vendored trees.
- "dogfood/**"
- "**/vendor/**"
ci_docs:
# CI that builds, deploys, previews, or reviews the docs. A change
# here can change the docs themselves, so it earns a review even
# though it sits under .github.
- ".github/workflows/ci.yaml"
- ".github/workflows/doc-check.yaml"
- ".github/workflows/deploy-docs.yaml"
- ".github/workflows/docs-preview.yaml"
- ".github/workflows/audit-docs-paths.yaml"
- ".github/workflows/weekly-docs.yaml"
- ".github/workflows/test-deploy-docs-diff.sh"
- ".github/workflows/test-deploy-docs-release.sh"
- ".github/workflows/test-docs-preview-mapper.sh"
- name: Decide whether to review
if: steps.check-secrets.outputs.skip != 'true'
id: check-paths
env:
# The doc-check label and a manual dispatch always force a review, so
# any skip below stays overridable from the pull request.
FORCED: ${{ github.event_name == 'workflow_dispatch' || github.event.label.name == 'doc-check' }}
ALL_COUNT: ${{ steps.filter.outputs.all_count }}
NODOCS_COUNT: ${{ steps.filter.outputs.nodocs_count }}
CI_DOCS_COUNT: ${{ steps.filter.outputs.ci_docs_count }}
run: |
set -euo pipefail
if [[ "${FORCED}" == "true" ]]; then
echo "skip=false" >> "${GITHUB_OUTPUT}"
echo "Review forced by label or manual dispatch; path classes ignored."
exit 0
fi
# Comparing against all_count is what makes this "every changed file".
# A pull request that mixes a test file with a CLI flag still gets a
# full review, because the counts differ.
if [[ -n "${ALL_COUNT}" && "${ALL_COUNT}" != "0" &&
"${NODOCS_COUNT}" == "${ALL_COUNT}" && "${CI_DOCS_COUNT}" == "0" ]]; then
echo "skip=true" >> "${GITHUB_OUTPUT}"
echo "All ${ALL_COUNT} changed file(s) are in path classes with no documentation surface."
{
echo "### Documentation check skipped"
echo ""
echo "All ${ALL_COUNT} changed file(s) are in path classes with no documentation surface, so no review ran."
echo ""
echo "Add the \`doc-check\` label to review anyway."
} >> "${GITHUB_STEP_SUMMARY}"
else
echo "skip=false" >> "${GITHUB_OUTPUT}"
fi
- name: Determine PR Context
if: steps.check-secrets.outputs.skip != 'true' && steps.check-paths.outputs.skip != 'true'
id: determine-context
env:
GITHUB_EVENT_NAME: ${{ github.event_name }}
GITHUB_EVENT_ACTION: ${{ github.event.action }}
GITHUB_EVENT_PR_HTML_URL: ${{ github.event.pull_request.html_url }}
GITHUB_EVENT_PR_NUMBER: ${{ github.event.pull_request.number }}
INPUTS_PR_URL: ${{ inputs.pr_url }}
run: |
# Determine trigger type for context
if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
echo "trigger_type=manual" >> "${GITHUB_OUTPUT}"
echo "Using PR URL: ${INPUTS_PR_URL}"
# Validate PR URL format
if [[ ! "${INPUTS_PR_URL}" =~ ^https://github\.com/[^/]+/[^/]+/pull/[0-9]+$ ]]; then
echo "::error::Invalid PR URL format: ${INPUTS_PR_URL}"
echo "::error::Expected format: https://github.com/owner/repo/pull/NUMBER"
exit 1
fi
ISSUE_URL="${INPUTS_PR_URL/\/pull\//\/issues\/}"
echo "pr_url=${ISSUE_URL}" >> "${GITHUB_OUTPUT}"
PR_NUMBER=$(echo "${INPUTS_PR_URL}" | grep -oP '(?<=pull/)\d+')
echo "pr_number=${PR_NUMBER}" >> "${GITHUB_OUTPUT}"
elif [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
echo "Using PR URL: ${GITHUB_EVENT_PR_HTML_URL}"
ISSUE_URL="${GITHUB_EVENT_PR_HTML_URL/\/pull\//\/issues\/}"
echo "pr_url=${ISSUE_URL}" >> "${GITHUB_OUTPUT}"
echo "pr_number=${GITHUB_EVENT_PR_NUMBER}" >> "${GITHUB_OUTPUT}"
# Set trigger type based on action
case "${GITHUB_EVENT_ACTION}" in
opened)
echo "trigger_type=new_pr" >> "${GITHUB_OUTPUT}"
;;
synchronize)
echo "trigger_type=pr_updated" >> "${GITHUB_OUTPUT}"
;;
labeled)
echo "trigger_type=label_requested" >> "${GITHUB_OUTPUT}"
;;
ready_for_review)
echo "trigger_type=ready_for_review" >> "${GITHUB_OUTPUT}"
;;
*)
echo "trigger_type=unknown" >> "${GITHUB_OUTPUT}"
;;
esac
else
echo "::error::Unsupported event type: ${GITHUB_EVENT_NAME}"
exit 1
fi
- name: Prepare the review workspace
if: steps.check-secrets.outputs.skip != 'true' && steps.check-paths.outputs.skip != 'true'
id: check-workspace
env:
CODER_URL: ${{ secrets.DOC_CHECK_CODER_URL }}
CODER_TOKEN: ${{ secrets.DOC_CHECK_CODER_SESSION_TOKEN }}
PR_NUMBER: ${{ steps.determine-context.outputs.pr_number }}
run: |
set -euo pipefail
# A review reuses one chat per PR, and that chat owns one workspace.
# The workspace stops after about a day and then goes dormant. A chat
# cannot repair a dormant workspace: start_workspace and
# create_workspace both fail with
# "unauthorized: rbac: forbidden". This step runs with the bot's own
# session token, which can clear dormancy, so it wakes the workspace
# before the review starts.
# https://linear.app/codercom/issue/CODAGT-1025
WS_NAME="doc-check-pr-${PR_NUMBER}"
RESPONSE=$(curl -sS --fail-with-body -G \
-H "Coder-Session-Token: ${CODER_TOKEN}" \
--data-urlencode "q=owner:me name:${WS_NAME}" \
"${CODER_URL%/}/api/v2/workspaces") || {
echo "::warning::Could not read the workspace state from Coder. Continuing."
exit 0
}
# The name filter matches substrings, so doc-check-pr-12 also
# matches doc-check-pr-123. Keep only the exact name.
WORKSPACE=$(echo "${RESPONSE}" | jq -c --arg name "${WS_NAME}" \
'first(.workspaces[]? | select(.name == $name)) // empty')
if [[ -z "${WORKSPACE}" ]]; then
echo "${WS_NAME} does not exist yet. The review creates it."
exit 0
fi
WS_ID=$(echo "${WORKSPACE}" | jq -r '.id')
DORMANT_AT=$(echo "${WORKSPACE}" | jq -r '.dormant_at // empty')
if [[ -n "${DORMANT_AT}" ]]; then
# Clearing dormancy also resets last_used_at, so the lifecycle
# executor does not mark the workspace dormant again right away.
# The workspace stays stopped; the start below brings it up.
echo "${WS_NAME} is dormant since ${DORMANT_AT}. Clearing dormancy."
curl -sS --fail-with-body -X PUT \
-H "Coder-Session-Token: ${CODER_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"dormant":false}' \
"${CODER_URL%/}/api/v2/workspaces/${WS_ID}/dormant" > /dev/null || {
echo "::error::Could not clear dormancy on ${WS_NAME}. The review cannot run, because a chat cannot start its own dormant workspace: https://linear.app/codercom/issue/CODAGT-1025"
{
echo "### Documentation check did not run"
echo ""
echo "The workspace \`${WS_NAME}\` is dormant since ${DORMANT_AT}, and this job could not clear it."
echo ""
echo "A chat cannot start its own dormant workspace, so the review would read no files and post no comment. See [CODAGT-1025](https://linear.app/codercom/issue/CODAGT-1025)."
echo ""
echo "To run the review now, start \`${WS_NAME}\` in Coder, then push again or re-run this job."
} >> "${GITHUB_STEP_SUMMARY}"
exit 1
}
fi
# The previous run stopped the workspace when its review finished.
# Start it here rather than leave the restart to the model.
TRANSITION=$(echo "${WORKSPACE}" | jq -r '.latest_build.transition')
STATUS=$(echo "${WORKSPACE}" | jq -r '.latest_build.status')
if [[ "${TRANSITION}" == "stop" && "${STATUS}" == "stopped" ]]; then
echo "Starting ${WS_NAME}."
curl -sS --fail-with-body -X POST \
-H "Coder-Session-Token: ${CODER_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"transition":"start"}' \
"${CODER_URL%/}/api/v2/workspaces/${WS_ID}/builds" > /dev/null || {
echo "::warning::Could not start ${WS_NAME}. The review will try to start it."
exit 0
}
for _ in $(seq 36); do
STATUS=$(curl -sS --fail-with-body \
-H "Coder-Session-Token: ${CODER_TOKEN}" \
"${CODER_URL%/}/api/v2/workspaces/${WS_ID}" |
jq -r '.latest_build.status') || STATUS="unknown"
case "${STATUS}" in
running)
echo "${WS_NAME} is running."
exit 0
;;
failed | canceled)
echo "::warning::Starting ${WS_NAME} ended with status ${STATUS}. The review will try to start it."
exit 0
;;
esac
sleep 5
done
echo "::warning::${WS_NAME} did not start within 3 minutes. Continuing."
exit 0
fi
echo "Workspace state is usable."
- name: Build chat prompt
if: steps.check-secrets.outputs.skip != 'true' && steps.check-paths.outputs.skip != 'true'
id: extract-context
env:
PR_NUMBER: ${{ steps.determine-context.outputs.pr_number }}
TRIGGER_TYPE: ${{ steps.determine-context.outputs.trigger_type }}
run: |
echo "Analyzing PR #${PR_NUMBER} (trigger: ${TRIGGER_TYPE})"
# Build context based on trigger type
case "${TRIGGER_TYPE}" in
new_pr)
CONTEXT="This is a NEW PR. Perform initial documentation review."
;;
pr_updated)
CONTEXT="This PR was UPDATED with new commits. Check if previous feedback was addressed or if new doc needs arose."
;;
label_requested)
CONTEXT="A documentation review was REQUESTED via label. Perform a thorough review."
;;
ready_for_review)
CONTEXT="This PR was marked READY FOR REVIEW. Perform a thorough review."
;;
manual)
CONTEXT="This is a MANUAL review request. Perform a thorough review."
;;
*)
CONTEXT="Perform a documentation review."
;;
esac
# Build the chat prompt. Durable rules live in the skill
# (.claude/skills/doc-check/SKILL.md), so this prompt stays short
# and carries only what changes per run.
CHAT_PROMPT="Use the doc-check skill to review PR #${PR_NUMBER} in coder/coder.
${CONTEXT}
Use \`gh\` to read the PR details, the diff, and every comment.
Follow the skill in full: its evidence discipline, its comment
rules, and its comment format. Every item links its page, and the
comment ends with \`<!-- doc-check-sticky -->\` on its own line.
**Do not comment if no documentation changes are needed.**
## When the workspace is broken
A tool call that fails with \`unauthorized: rbac: forbidden\` is an
infrastructure state, not a bad command, so retrying the same
command changes nothing.
Call \`start_workspace\` with no arguments. If it answers
\`workspace was deleted\` or \`chat has no workspace\`, call
\`list_templates\` for the \`coder-workflow-bot\` template ID, then
\`create_workspace\` with it, and run the failed call again. If it
answers \`load workspace: unauthorized: rbac: forbidden\`, the
workspace is dormant and you cannot repair it: stop, say you could
not review the PR, and name the error. Post no comment, and do not
imply the docs are fine, because you did not read them.
Create any new workspace from the \"coder-workflow-bot\" template."
# Output the prompt
{
echo "chat_prompt<<EOFOUTPUT"
echo "${CHAT_PROMPT}"
echo "EOFOUTPUT"
} >> "${GITHUB_OUTPUT}"
- name: Run doc-check via Coder Agent Chat
if: steps.check-secrets.outputs.skip != 'true' && steps.check-paths.outputs.skip != 'true'
id: doc-check
uses: coder/agents-chat-action@9b3ac42d568e9206f9cd51a97789820d6935cc8d # v0.5.1
with:
coder-url: ${{ secrets.DOC_CHECK_CODER_URL }}
coder-token: ${{ secrets.DOC_CHECK_CODER_SESSION_TOKEN }}
# The bot account belongs to many organizations on this deployment,
# where the action's fallback organization choice is not
# deterministic. Pin it, because a model configuration ID only
# resolves inside its own organization.
coder-organization: coder
# Grant the organization's Everyone group read access to new reviews.
share-with-organization: "true"
# Pins the model that reviews the PR. Set this repository variable to
# a model configuration ID in the organization above. Unset means the
# deployment's default model, so clearing the variable is the
# rollback path and needs no code change.
model-config-id: ${{ vars.DOC_CHECK_MODEL_CONFIG_ID }}
chat-prompt: ${{ steps.extract-context.outputs.chat_prompt }}
github-url: ${{ steps.determine-context.outputs.pr_url }}
github-token: ${{ github.token }}
wait: complete
# The chat keeps running in Coder after this job stops watching, so
# a timeout here only turns a finished review red. A review on the
# local model takes 5 to 30 minutes; 40 minutes covers every normal
# run seen so far and leaves the job cap above room for setup.
wait-timeout-seconds: "2400"
# The doc-check agent posts its own sticky comment when there
# are findings; failures surface in the workflow run log.
comment-on-issue: "false"
# A review workspace left running until autostop holds its share of the
# host, and enough of them at once overload it. Stop it once the chat is
# done; the next run starts it again. Skip when the wait timed out,
# because the chat is still working in the workspace.
- name: Stop the review workspace
if: |
always() &&
steps.doc-check.outputs.workspace-id != '' &&
contains(fromJSON('["waiting", "completed", "error"]'), steps.doc-check.outputs.chat-status)
env:
CODER_URL: ${{ secrets.DOC_CHECK_CODER_URL }}
CODER_TOKEN: ${{ secrets.DOC_CHECK_CODER_SESSION_TOKEN }}
WORKSPACE_ID: ${{ steps.doc-check.outputs.workspace-id }}
run: |
curl -sS --fail-with-body -X POST \
-H "Coder-Session-Token: ${CODER_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"transition":"stop"}' \
"${CODER_URL%/}/api/v2/workspaces/${WORKSPACE_ID}/builds" > /dev/null || {
echo "::warning::Could not stop workspace ${WORKSPACE_ID}. It stops at its autostop deadline instead."
exit 0
}
echo "Stopped workspace ${WORKSPACE_ID}."
# Delete the review workspace once its PR merges. A PR closed without
# merging keeps its workspace, because stale PRs get closed and reopened
# and the review needs its context back.
delete-workspace:
name: Delete the review workspace after merge
runs-on: ubuntu-latest
if: |
github.event_name == 'pull_request' &&
github.event.action == 'closed' &&
github.event.pull_request.merged == true &&
github.event.pull_request.head.repo.full_name == github.repository
concurrency:
group: doc-check-${{ github.event.pull_request.number }}
cancel-in-progress: false
timeout-minutes: 5
permissions: {}
steps:
- name: Delete the review workspace
env:
CODER_URL: ${{ secrets.DOC_CHECK_CODER_URL }}
CODER_TOKEN: ${{ secrets.DOC_CHECK_CODER_SESSION_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
set -euo pipefail
if [[ -z "${CODER_URL}" || -z "${CODER_TOKEN}" ]]; then
echo "Secrets not available - skipping workspace cleanup."
exit 0
fi
WS_NAME="doc-check-pr-${PR_NUMBER}"
RESPONSE=$(curl -sS --fail-with-body -G \
-H "Coder-Session-Token: ${CODER_TOKEN}" \
--data-urlencode "q=owner:me name:${WS_NAME}" \
"${CODER_URL%/}/api/v2/workspaces")
# The name filter matches substrings; keep only the exact name.
WS_ID=$(echo "${RESPONSE}" | jq -r --arg name "${WS_NAME}" \
'first(.workspaces[]? | select(.name == $name) | .id) // empty')
if [[ -z "${WS_ID}" ]]; then
echo "${WS_NAME} does not exist. Nothing to delete."
exit 0
fi
curl -sS --fail-with-body -X POST \
-H "Coder-Session-Token: ${CODER_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"transition":"delete"}' \
"${CODER_URL%/}/api/v2/workspaces/${WS_ID}/builds" > /dev/null
echo "Deleted ${WS_NAME}."