|
1
|
name: Docs Automation |
|
2
|
|
|
3
|
on: |
|
4
|
push: |
|
5
|
branches: |
|
6
|
- develop |
|
7
|
paths: |
|
8
|
- "packages/**" |
|
9
|
workflow_dispatch: |
|
10
|
inputs: |
|
11
|
commit_sha: |
|
12
|
description: "Commit SHA to analyze and generate docs for" |
|
13
|
required: true |
|
14
|
type: string |
|
15
|
|
|
16
|
concurrency: |
|
17
|
group: docs-release-staging |
|
18
|
cancel-in-progress: false |
|
19
|
|
|
20
|
jobs: |
|
21
|
docs-automation: |
|
22
|
if: ${{ github.event_name == 'workflow_dispatch' || github.event.head_commit.author.name != 'github-actions[bot]' }} |
|
23
|
runs-on: ubuntu-latest |
|
24
|
permissions: |
|
25
|
contents: write |
|
26
|
pull-requests: write |
|
27
|
steps: |
|
28
|
- name: Generate GitHub App token |
|
29
|
id: app-token |
|
30
|
uses: actions/create-github-app-token@v1 |
|
31
|
with: |
|
32
|
app-id: ${{ secrets.MEDUSA_APP_ID }} |
|
33
|
private-key: ${{ secrets.MEDUSA_APP_PRIVATE_KEY }} |
|
34
|
|
|
35
|
- name: Checkout repository |
|
36
|
uses: actions/checkout@v4 |
|
37
|
with: |
|
38
|
fetch-depth: 0 |
|
39
|
token: ${{ steps.app-token.outputs.token }} |
|
40
|
|
|
41
|
- name: Setup Node.js |
|
42
|
uses: actions/setup-node@v4 |
|
43
|
with: |
|
44
|
node-version: 22 |
|
45
|
cache: "yarn" |
|
46
|
|
|
47
|
- name: Install monorepo dependencies |
|
48
|
uses: ./.github/actions/cache-deps |
|
49
|
with: |
|
50
|
extension: docs-automation |
|
51
|
|
|
52
|
- name: Install www dependencies |
|
53
|
working-directory: www |
|
54
|
run: yarn install |
|
55
|
|
|
56
|
- name: Build www packages |
|
57
|
working-directory: www |
|
58
|
run: yarn build:packages |
|
59
|
|
|
60
|
- name: Install www/utils dependencies |
|
61
|
working-directory: www/utils |
|
62
|
run: yarn install |
|
63
|
|
|
64
|
- name: Build www/utils packages |
|
65
|
working-directory: www/utils |
|
66
|
run: yarn build |
|
67
|
|
|
68
|
- name: Analyze commit diff |
|
69
|
id: analyze |
|
70
|
working-directory: www/utils/packages/docs-automator |
|
71
|
run: yarn analyze --commit-sha ${{ inputs.commit_sha || github.sha }} --output /tmp/analysis.json |
|
72
|
env: |
|
73
|
MONOREPO_ROOT_PATH: ${{ github.workspace }} |
|
74
|
continue-on-error: true |
|
75
|
|
|
76
|
- name: Check if documentation changes are needed |
|
77
|
id: check |
|
78
|
run: | |
|
79
|
if [ "${{ steps.analyze.outcome }}" = "failure" ]; then |
|
80
|
echo "needs_docs=false" >> "$GITHUB_OUTPUT" |
|
81
|
exit 0 |
|
82
|
fi |
|
83
|
node --input-type=module << 'EOF' |
|
84
|
import { readFileSync, appendFileSync } from "fs" |
|
85
|
const a = JSON.parse(readFileSync("/tmp/analysis.json", "utf8")) |
|
86
|
const needed = a.affectedProjects.length > 0 ? "true" : "false" |
|
87
|
appendFileSync(process.env.GITHUB_OUTPUT, `needs_docs=${needed}\n`) |
|
88
|
EOF |
|
89
|
|
|
90
|
- name: Configure git |
|
91
|
if: steps.check.outputs.needs_docs == 'true' |
|
92
|
run: | |
|
93
|
git config user.name "github-actions[bot]" |
|
94
|
git config user.email "github-actions[bot]@users.noreply.github.com" |
|
95
|
|
|
96
|
- name: Checkout or create release docs branch |
|
97
|
if: steps.check.outputs.needs_docs == 'true' |
|
98
|
run: | |
|
99
|
git fetch origin |
|
100
|
if git ls-remote --exit-code origin docs/release-docs-staging > /dev/null 2>&1; then |
|
101
|
git checkout docs/release-docs-staging |
|
102
|
git merge -X theirs origin/develop -m "chore: sync docs/release-docs-staging with develop" |
|
103
|
echo "Checked out and synced existing branch docs/release-docs-staging" |
|
104
|
else |
|
105
|
git checkout -b docs/release-docs-staging |
|
106
|
echo "Created new branch docs/release-docs-staging" |
|
107
|
fi |
|
108
|
|
|
109
|
- name: Fetch existing PR body |
|
110
|
id: existing-pr |
|
111
|
if: steps.check.outputs.needs_docs == 'true' |
|
112
|
env: |
|
113
|
GH_TOKEN: ${{ steps.app-token.outputs.token }} |
|
114
|
REPO: ${{ github.repository }} |
|
115
|
run: | |
|
116
|
# The docs/release-docs-staging branch is long-lived: its PR is reused |
|
117
|
# until the next release. Claude rewrites this body in place, so a |
|
118
|
# failed lookup fails the step rather than losing the existing entries. |
|
119
|
REPO_OWNER="${REPO%%/*}" |
|
120
|
PR_NUMBER=$(gh api "repos/$REPO/pulls?head=${REPO_OWNER}:docs/release-docs-staging&state=open" --jq '.[0].number') |
|
121
|
if [ "$PR_NUMBER" = "null" ]; then |
|
122
|
PR_NUMBER="" |
|
123
|
fi |
|
124
|
|
|
125
|
if [ -n "$PR_NUMBER" ]; then |
|
126
|
gh api "repos/$REPO/pulls/$PR_NUMBER" --jq '.body // ""' > /tmp/existing-pr-body.md |
|
127
|
echo "Fetched body of PR #$PR_NUMBER" |
|
128
|
else |
|
129
|
: > /tmp/existing-pr-body.md |
|
130
|
echo "No open PR for docs/release-docs-staging yet" |
|
131
|
fi |
|
132
|
echo "number=$PR_NUMBER" >> "$GITHUB_OUTPUT" |
|
133
|
|
|
134
|
- name: Extract Claude prompt |
|
135
|
id: extract-prompt |
|
136
|
if: steps.check.outputs.needs_docs == 'true' |
|
137
|
env: |
|
138
|
COMMIT_SHA: ${{ inputs.commit_sha || github.sha }} |
|
139
|
REPO: ${{ github.repository }} |
|
140
|
run: | |
|
141
|
node --input-type=module << 'EOF' |
|
142
|
import { readFileSync, appendFileSync } from "fs" |
|
143
|
const a = JSON.parse(readFileSync("/tmp/analysis.json", "utf8")) |
|
144
|
const { COMMIT_SHA: sha, REPO: repo } = process.env |
|
145
|
const date = new Date().toISOString().split("T")[0] |
|
146
|
const projectList = a.affectedProjects |
|
147
|
.map((p) => `- **${p.project}**: ${p.reason}`) |
|
148
|
.join("\n") |
|
149
|
|
|
150
|
const prefix = "IMPORTANT: Only make file changes using the Write and Edit tools. Never use git commands to stage, commit, or push files.\n\nIMPORTANT: To load the writing-docs skill, use the Skill tool directly with skill='writing-docs'. Do NOT spawn a sub-agent or use the Task tool to load it.\n\n" |
|
151
|
|
|
152
|
// Claude owns the whole PR description so its format stays consistent |
|
153
|
// across runs instead of being appended to by this workflow. |
|
154
|
const suffix = [ |
|
155
|
"", |
|
156
|
"", |
|
157
|
"---", |
|
158
|
"", |
|
159
|
"IMPORTANT — final step: update the pull request description.", |
|
160
|
"", |
|
161
|
"These documentation changes land on a long-lived pull request that is reused for every commit until the next release. Read the current description from /tmp/existing-pr-body.md with the Read tool (the file is empty if the pull request does not exist yet), then write the FULL updated description to /tmp/pr-body.md with the Write tool.", |
|
162
|
"", |
|
163
|
"Rules for the updated description:", |
|
164
|
"- Keep the existing structure and formatting exactly as-is. Do not reformat, reorder, or rewrite entries that are already there.", |
|
165
|
"- Never remove, merge, or shorten existing entries. The description only grows.", |
|
166
|
"- Add exactly one new entry for this commit, after the existing entries, matching the format the existing entries already use.", |
|
167
|
"- Your new entry must summarize the documentation changes you just made in 2-4 sentences or a few bullet points.", |
|
168
|
"- Write the file even if the description ends up unchanged in structure.", |
|
169
|
"", |
|
170
|
"Facts for this commit's entry:", |
|
171
|
`- Commit: [\`${sha.slice(0, 7)}\`](https://github.com/${repo}/commit/${sha})`, |
|
172
|
`- Date: ${date}`, |
|
173
|
"- Affected projects:", |
|
174
|
projectList.replace(/^/gm, " "), |
|
175
|
"", |
|
176
|
"If /tmp/existing-pr-body.md is empty, create the description from scratch with exactly this structure:", |
|
177
|
"", |
|
178
|
"```markdown", |
|
179
|
"## Automated Documentation Updates", |
|
180
|
"", |
|
181
|
"This PR accumulates automated documentation changes for the next release.", |
|
182
|
"Each entry below corresponds to a merged commit that triggered updates.", |
|
183
|
"", |
|
184
|
"> Review carefully before merging. Claude may have missed context", |
|
185
|
"> or made incorrect assumptions.", |
|
186
|
"", |
|
187
|
"---", |
|
188
|
"", |
|
189
|
`### [\`${sha.slice(0, 7)}\`](https://github.com/${repo}/commit/${sha}) — ${date}`, |
|
190
|
"", |
|
191
|
"<your 2-4 sentence summary of what was updated>", |
|
192
|
"", |
|
193
|
"**Affected projects:**", |
|
194
|
projectList, |
|
195
|
"```", |
|
196
|
"", |
|
197
|
].join("\n") |
|
198
|
|
|
199
|
const delimiter = "CLAUDE_PROMPT_EOF" |
|
200
|
appendFileSync( |
|
201
|
process.env.GITHUB_OUTPUT, |
|
202
|
`CLAUDE_PROMPT<<${delimiter}\n${prefix}${a.claudePrompt}${suffix}\n${delimiter}\n` |
|
203
|
) |
|
204
|
EOF |
|
205
|
|
|
206
|
- name: Run Claude Code to update docs |
|
207
|
if: steps.check.outputs.needs_docs == 'true' |
|
208
|
uses: anthropics/claude-code-base-action@132be24009ca6f292db66a3d1d5a17249cf68a5d # main, installs Claude Code 2.1.282 |
|
209
|
with: |
|
210
|
anthropic_api_key: ${{ secrets.CLAUDE_CODE_API_TOKEN }} |
|
211
|
claude_args: >- |
|
212
|
--model claude-opus-5-5 |
|
213
|
--max-turns 40 |
|
214
|
--allowedTools "Skill,Read,Write,Edit,Glob,Grep,Bash(git diff:*),Bash(ls:*),Bash(find:*),Bash(cat:*)" |
|
215
|
prompt: ${{ steps.extract-prompt.outputs.CLAUDE_PROMPT }} |
|
216
|
|
|
217
|
- name: Verify no protected files were modified |
|
218
|
if: steps.check.outputs.needs_docs == 'true' |
|
219
|
run: | |
|
220
|
PROTECTED=$(git diff --name-only | grep -E \ |
|
221
|
"^www/apps/resources/references/|^www/apps/ui/specs/components/|^www/apps/api-reference/" \ |
|
222
|
|| true) |
|
223
|
if [ -n "$PROTECTED" ]; then |
|
224
|
echo "ERROR: Protected directories were modified:" |
|
225
|
echo "$PROTECTED" |
|
226
|
exit 1 |
|
227
|
fi |
|
228
|
|
|
229
|
- name: Run prep scripts for affected projects |
|
230
|
if: steps.check.outputs.needs_docs == 'true' |
|
231
|
run: | |
|
232
|
PROJECTS=$(node --input-type=module << 'EOF' |
|
233
|
import { readFileSync } from "fs" |
|
234
|
const a = JSON.parse(readFileSync("/tmp/analysis.json", "utf8")) |
|
235
|
process.stdout.write(a.affectedProjects.map((p) => p.project).join(" ")) |
|
236
|
EOF |
|
237
|
) |
|
238
|
for project in $PROJECTS; do |
|
239
|
echo "Running prep for $project..." |
|
240
|
(cd "www/apps/$project" && yarn prep) || true |
|
241
|
done |
|
242
|
|
|
243
|
- name: Run lint:content for affected projects |
|
244
|
if: steps.check.outputs.needs_docs == 'true' |
|
245
|
run: | |
|
246
|
PROJECTS=$(node --input-type=module << 'EOF' |
|
247
|
import { readFileSync } from "fs" |
|
248
|
const a = JSON.parse(readFileSync("/tmp/analysis.json", "utf8")) |
|
249
|
process.stdout.write(a.affectedProjects.map((p) => p.project).join(" ")) |
|
250
|
EOF |
|
251
|
) |
|
252
|
for project in $PROJECTS; do |
|
253
|
echo "Running lint:content for $project..." |
|
254
|
(cd "www/apps/$project" && yarn lint:content) || true |
|
255
|
done |
|
256
|
|
|
257
|
- name: Stage documentation changes |
|
258
|
id: changes |
|
259
|
if: steps.check.outputs.needs_docs == 'true' |
|
260
|
run: | |
|
261
|
git add www/apps/book www/apps/resources www/apps/ui www/apps/user-guide |
|
262
|
if git diff --staged --quiet; then |
|
263
|
echo "has_changes=false" >> "$GITHUB_OUTPUT" |
|
264
|
echo "No documentation changes to commit." |
|
265
|
else |
|
266
|
echo "has_changes=true" >> "$GITHUB_OUTPUT" |
|
267
|
fi |
|
268
|
|
|
269
|
- name: Commit documentation changes |
|
270
|
if: steps.check.outputs.needs_docs == 'true' && steps.changes.outputs.has_changes == 'true' |
|
271
|
run: | |
|
272
|
git commit -m "chore(docs): automated documentation update for ${{ inputs.commit_sha || github.sha }}" |
|
273
|
|
|
274
|
- name: Push release docs branch |
|
275
|
if: steps.check.outputs.needs_docs == 'true' && steps.changes.outputs.has_changes == 'true' |
|
276
|
run: git push origin docs/release-docs-staging |
|
277
|
|
|
278
|
- name: Resolve PR body |
|
279
|
if: steps.check.outputs.needs_docs == 'true' && steps.changes.outputs.has_changes == 'true' |
|
280
|
env: |
|
281
|
COMMIT_SHA: ${{ inputs.commit_sha || github.sha }} |
|
282
|
REPO: ${{ github.repository }} |
|
283
|
run: | |
|
284
|
node --input-type=module << 'EOF' |
|
285
|
import { existsSync, readFileSync, writeFileSync } from "fs" |
|
286
|
|
|
287
|
const a = JSON.parse(readFileSync("/tmp/analysis.json", "utf8")) |
|
288
|
const { COMMIT_SHA: sha, REPO: repo } = process.env |
|
289
|
const date = new Date().toISOString().split("T")[0] |
|
290
|
|
|
291
|
const existingBody = readFileSync("/tmp/existing-pr-body.md", "utf8").trim() |
|
292
|
const claudeBody = existsSync("/tmp/pr-body.md") |
|
293
|
? readFileSync("/tmp/pr-body.md", "utf8").trim() |
|
294
|
: "" |
|
295
|
|
|
296
|
// Claude owns the body. Fall back to a deterministic append only if it |
|
297
|
// wrote nothing, or if it dropped content that was already there. |
|
298
|
const droppedContent = |
|
299
|
existingBody.length > 0 && claudeBody.length < existingBody.length |
|
300
|
if (claudeBody && !droppedContent) { |
|
301
|
writeFileSync("/tmp/pr-body.md", claudeBody) |
|
302
|
process.exit(0) |
|
303
|
} |
|
304
|
|
|
305
|
console.log( |
|
306
|
claudeBody |
|
307
|
? "Claude's PR body is shorter than the existing one; falling back." |
|
308
|
: "Claude wrote no PR body; falling back." |
|
309
|
) |
|
310
|
|
|
311
|
const projectList = a.affectedProjects |
|
312
|
.map((p) => `- **${p.project}**: ${p.reason}`) |
|
313
|
.join("\n") |
|
314
|
|
|
315
|
const newEntry = [ |
|
316
|
`### [\`${sha.slice(0, 7)}\`](https://github.com/${repo}/commit/${sha}) — ${date}`, |
|
317
|
"", |
|
318
|
"_No summary was generated._", |
|
319
|
"", |
|
320
|
"**Affected projects:**", |
|
321
|
projectList, |
|
322
|
].join("\n") |
|
323
|
|
|
324
|
const header = [ |
|
325
|
"## Automated Documentation Updates", |
|
326
|
"", |
|
327
|
"This PR accumulates automated documentation changes for the next release.", |
|
328
|
"Each entry below corresponds to a merged commit that triggered updates.", |
|
329
|
"", |
|
330
|
"> Review carefully before merging. Claude may have missed context", |
|
331
|
"> or made incorrect assumptions.", |
|
332
|
].join("\n") |
|
333
|
|
|
334
|
const base = existingBody || header |
|
335
|
writeFileSync("/tmp/pr-body.md", `${base}\n\n---\n\n${newEntry}`) |
|
336
|
EOF |
|
337
|
|
|
338
|
- name: Create or update Pull Request |
|
339
|
if: steps.check.outputs.needs_docs == 'true' && steps.changes.outputs.has_changes == 'true' |
|
340
|
env: |
|
341
|
GH_TOKEN: ${{ steps.app-token.outputs.token }} |
|
342
|
REPO: ${{ github.repository }} |
|
343
|
PR_NUMBER: ${{ steps.existing-pr.outputs.number }} |
|
344
|
# The branch is long-lived, so the title stays static |
|
345
|
PR_TITLE: "docs: doc changes for next release [automated]" |
|
346
|
run: | |
|
347
|
if [ -n "$PR_NUMBER" ]; then |
|
348
|
gh api "repos/$REPO/pulls/$PR_NUMBER" \ |
|
349
|
--method PATCH \ |
|
350
|
--field title="$PR_TITLE" \ |
|
351
|
--field body="$(cat /tmp/pr-body.md)" |
|
352
|
else |
|
353
|
PR_NUMBER=$(gh api "repos/$REPO/pulls" \ |
|
354
|
--method POST \ |
|
355
|
--field title="$PR_TITLE" \ |
|
356
|
--field body="$(cat /tmp/pr-body.md)" \ |
|
357
|
--field base="develop" \ |
|
358
|
--field head="docs/release-docs-staging" \ |
|
359
|
--jq '.number') |
|
360
|
gh api "repos/$REPO/issues/$PR_NUMBER/labels" \ |
|
361
|
--method POST \ |
|
362
|
--field 'labels[]=type: docs' |
|
363
|
fi |