1 name: Cloud Webhooks Docs Automation
2
3 on:
4 repository_dispatch:
5 types:
6 - cloud-webhooks
7 workflow_dispatch:
8 inputs:
9 dispatch_payload:
10 description: "WebhooksDispatchPayload as JSON. Used for manual re-runs and testing."
11 required: true
12 type: string
13
14 jobs:
15 cloud-webhooks-docs-automation:
16 runs-on: ubuntu-latest
17 permissions:
18 contents: write
19 pull-requests: write
20 steps:
21 - name: Generate GitHub App token
22 id: app-token
23 uses: actions/create-github-app-token@v1
24 with:
25 app-id: ${{ secrets.MEDUSA_APP_ID }}
26 private-key: ${{ secrets.MEDUSA_APP_PRIVATE_KEY }}
27
28 - name: Checkout repository
29 uses: actions/checkout@v4
30 with:
31 fetch-depth: 0
32 token: ${{ steps.app-token.outputs.token }}
33
34 - name: Setup Node.js
35 uses: actions/setup-node@v4
36 with:
37 node-version: 22
38 cache: "yarn"
39
40 - name: Install monorepo dependencies
41 uses: ./.github/actions/cache-deps
42 with:
43 extension: cloud-webhooks-docs-automation
44
45 - name: Install www dependencies
46 working-directory: www
47 run: yarn install
48
49 - name: Build www packages
50 working-directory: www
51 run: yarn build:packages
52
53 - name: Install www/utils dependencies
54 working-directory: www/utils
55 run: yarn install
56
57 - name: Build www/utils packages
58 working-directory: www/utils
59 run: yarn build
60
61 - name: Write dispatch payload to file
62 id: write-payload
63 env:
64 MANUAL_PAYLOAD: ${{ inputs.dispatch_payload }}
65 run: |
66 node --input-type=module << 'EOF'
67 import { writeFileSync } from "fs"
68 const dispatched = ${{ toJSON(github.event.client_payload) }}
69 const manual = process.env.MANUAL_PAYLOAD
70 const payload = manual ? JSON.parse(manual) : dispatched
71 writeFileSync(
72 "/tmp/webhooks-dispatch.json",
73 JSON.stringify(payload, null, 2)
74 )
75 console.log("Payload written to /tmp/webhooks-dispatch.json")
76 EOF
77
78 - name: Analyze webhook changes
79 id: analyze
80 working-directory: www/utils/packages/docs-automator
81 run: yarn analyze-webhooks --dispatch-file /tmp/webhooks-dispatch.json --output /tmp/analysis.json
82 continue-on-error: true
83
84 - name: Check if documentation changes are needed
85 id: check
86 run: |
87 if [ "${{ steps.analyze.outcome }}" = "failure" ]; then
88 echo "needs_docs=false" >> "$GITHUB_OUTPUT"
89 exit 0
90 fi
91 node --input-type=module << 'EOF'
92 import { readFileSync, appendFileSync } from "fs"
93 const a = JSON.parse(readFileSync("/tmp/analysis.json", "utf8"))
94 const needed = a.affectedProjects.length > 0 ? "true" : "false"
95 appendFileSync(process.env.GITHUB_OUTPUT, `needs_docs=${needed}\n`)
96 EOF
97
98 - name: Configure git
99 if: steps.check.outputs.needs_docs == 'true'
100 run: |
101 git config user.name "github-actions[bot]"
102 git config user.email "github-actions[bot]@users.noreply.github.com"
103
104 - name: Extract Claude prompt
105 id: extract-prompt
106 if: steps.check.outputs.needs_docs == 'true'
107 run: |
108 node --input-type=module << 'EOF'
109 import { readFileSync, appendFileSync } from "fs"
110 const a = JSON.parse(readFileSync("/tmp/analysis.json", "utf8"))
111 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"
112 const suffix = "\n\n---\n\nIMPORTANT — final step: after applying all documentation changes, write these two files using the Write tool:\n\n1. /tmp/pr-title.txt — a single-line pull request title. It MUST start with \"docs: \", followed by a concise summary of what was changed, and MUST end with \" [automated]\". Keep it under 100 characters. Example: \"docs: document the deployment.succeeded webhook [automated]\"\n2. /tmp/pr-summary.md — a short summary (2-4 sentences, or a few bullet points) of what was updated. Plain markdown, no top-level heading.\n\nIf you made no documentation changes, do not create either file.\n"
113 const delimiter = "CLAUDE_PROMPT_EOF"
114 appendFileSync(
115 process.env.GITHUB_OUTPUT,
116 `CLAUDE_PROMPT<<${delimiter}\n${prefix}${a.claudePrompt}${suffix}\n${delimiter}\n`
117 )
118 EOF
119
120 - name: Run Claude Code to update webhooks docs
121 if: steps.check.outputs.needs_docs == 'true'
122 uses: anthropics/claude-code-base-action@132be24009ca6f292db66a3d1d5a17249cf68a5d # main, installs Claude Code 2.1.282
123 with:
124 anthropic_api_key: ${{ secrets.CLAUDE_CODE_API_TOKEN }}
125 claude_args: >-
126 --model claude-opus-5-5
127 --max-turns 40
128 --allowedTools "Skill,Read,Write,Edit,Glob,Grep,Bash(git diff:*),Bash(ls:*),Bash(find:*),Bash(cat:*)"
129 prompt: ${{ steps.extract-prompt.outputs.CLAUDE_PROMPT }}
130
131 - name: Run prep script for cloud project
132 if: steps.check.outputs.needs_docs == 'true'
133 run: |
134 echo "Running prep for cloud..."
135 (cd "www/apps/cloud" && yarn prep) || true
136
137 - name: Run lint:content for cloud project
138 if: steps.check.outputs.needs_docs == 'true'
139 run: |
140 echo "Running lint:content for cloud..."
141 (cd "www/apps/cloud" && yarn lint:content) || true
142
143 - name: Check for documentation changes
144 id: changes
145 if: steps.check.outputs.needs_docs == 'true'
146 run: |
147 CHANGES=$(git diff --name-only -- www/apps/cloud/app www/apps/cloud/generated www/apps/cloud/sidebar.mjs \
148 | grep -E '\.(mdx|json|mjs)$' || true)
149 if [ -n "$CHANGES" ]; then
150 echo "has_changes=true" >> "$GITHUB_OUTPUT"
151 else
152 echo "has_changes=false" >> "$GITHUB_OUTPUT"
153 echo "No documentation changes found."
154 fi
155
156 - name: Build PR metadata
157 id: pr-body
158 if: steps.check.outputs.needs_docs == 'true' && steps.changes.outputs.has_changes == 'true'
159 run: |
160 node --input-type=module << 'EOF'
161 import { existsSync, readFileSync, appendFileSync } from "fs"
162
163 const a = JSON.parse(readFileSync("/tmp/analysis.json", "utf8"))
164 const flagged = Array.isArray(a.featureFlaggedFeatures) ? a.featureFlaggedFeatures : []
165 const summaryLines = Array.isArray(a.changeSummary) ? a.changeSummary : []
166
167 // Title generated by Claude, with a fallback if it is missing or malformed
168 let title = existsSync("/tmp/pr-title.txt")
169 ? readFileSync("/tmp/pr-title.txt", "utf8").split("\n")[0].trim()
170 : ""
171 if (!title.startsWith("docs: ") || !title.endsWith("[automated]")) {
172 title = "docs: update cloud webhooks documentation [automated]"
173 }
174
175 const summary = existsSync("/tmp/pr-summary.md")
176 ? readFileSync("/tmp/pr-summary.md", "utf8").trim()
177 : ""
178
179 const lines = [
180 "## Automated Cloud Webhooks Documentation Updates",
181 "",
182 `This PR contains automated documentation changes for webhook events delivered on **${a.changelogDate}**.`,
183 "",
184 summary || "_No summary was generated._",
185 "",
186 "> Review carefully before merging. Claude may have missed context or made incorrect assumptions.",
187 ]
188
189 if (summaryLines.length > 0) {
190 lines.push("")
191 lines.push("---")
192 lines.push("")
193 lines.push("### Reported changes")
194 lines.push("")
195 for (const line of summaryLines) {
196 lines.push(`- ${line}`)
197 }
198 }
199
200 if (flagged.length > 0) {
201 lines.push("")
202 lines.push("---")
203 lines.push("")
204 lines.push("### ⚠️ Feature flag warning")
205 lines.push("")
206 lines.push(
207 "The following events appear to be gated by a feature flag. " +
208 "**Confirm they have been fully rolled out before merging this PR:**"
209 )
210 lines.push("")
211 for (const f of flagged) {
212 lines.push(`- ${f}`)
213 }
214 }
215
216 const body = lines.join("\n")
217 const delimiter = "PR_BODY_EOF"
218 appendFileSync(process.env.GITHUB_OUTPUT, `TITLE=${title}\n`)
219 appendFileSync(process.env.GITHUB_OUTPUT, `BODY<<${delimiter}\n${body}\n${delimiter}\n`)
220 EOF
221
222 - name: Create Pull Request
223 if: steps.check.outputs.needs_docs == 'true' && steps.changes.outputs.has_changes == 'true'
224 uses: peter-evans/create-pull-request@38e0b6e68b4c852a5500a94740f0e535e0d7ba54 # v4
225 with:
226 token: ${{ steps.app-token.outputs.token }}
227 commit-message: "chore(docs): automated cloud webhooks documentation update"
228 base: develop
229 branch: docs/cloud-webhooks-docs
230 branch-suffix: timestamp
231 title: ${{ steps.pr-body.outputs.TITLE }}
232 labels: "type: docs"
233 add-paths: |
234 www/apps/cloud/app
235 www/apps/cloud/generated
236 www/apps/cloud/sidebar.mjs
237 body: ${{ steps.pr-body.outputs.BODY }}