Raw
1 name: Kapa Doc Fixes
2
3 on:
4 workflow_call:
5 inputs:
6 indices:
7 description: "JSON array of item indices to process"
8 type: string
9 required: true
10 sha:
11 description: "Commit SHA to base the changes on"
12 type: string
13 required: true
14 branch:
15 description: "Branch to open the PR from"
16 type: string
17 required: true
18 prompt:
19 description: "Claude prompt for a single item. The item is at /tmp/kapa-item.md and the result must be written to /tmp/kapa-result.md"
20 type: string
21 required: true
22 commit_prefix:
23 description: "Commit message prefix, followed by the item title"
24 type: string
25 required: true
26 pr_title:
27 description: "Pull request title"
28 type: string
29 required: true
30 pr_intro:
31 description: "Markdown shown at the top of the pull request body"
32 type: string
33 required: true
34 secrets:
35 CLAUDE_CODE_API_TOKEN:
36 required: true
37 MEDUSA_APP_ID:
38 required: true
39 MEDUSA_APP_PRIVATE_KEY:
40 required: true
41
42 # Callers must upload a `kapa-items` artifact with:
43 # - items.json: [{ "index": number, "title": string, "thread_count": number }]
44 # - items/<index>.md: the details Claude reads for each item
45 jobs:
46 fix-item:
47 runs-on: ubuntu-latest
48 permissions:
49 contents: read
50 strategy:
51 fail-fast: false
52 max-parallel: 5
53 matrix:
54 index: ${{ fromJSON(inputs.indices) }}
55 steps:
56 - name: Checkout repository
57 uses: actions/checkout@v4
58 with:
59 ref: ${{ inputs.sha }}
60
61 - name: Download items
62 uses: actions/download-artifact@v4
63 with:
64 name: kapa-items
65 path: /tmp/kapa-items
66
67 - name: Write item details
68 env:
69 INDEX: ${{ matrix.index }}
70 run: |
71 cp "/tmp/kapa-items/items/$INDEX.md" /tmp/kapa-item.md
72 cat /tmp/kapa-item.md
73
74 - name: Run Claude Code to fix the docs
75 uses: anthropics/claude-code-base-action@132be24009ca6f292db66a3d1d5a17249cf68a5d # main, installs Claude Code 2.1.282
76 with:
77 anthropic_api_key: ${{ secrets.CLAUDE_CODE_API_TOKEN }}
78 claude_args: >-
79 --model claude-opus-5-5
80 --max-turns 60
81 --allowedTools "Skill,Read,Write,Edit,Glob,Grep,Bash(git diff:*),Bash(git status:*),Bash(ls:*),Bash(find:*),Bash(cat:*)"
82 prompt: ${{ inputs.prompt }}
83
84 - name: Create patch
85 run: |
86 mkdir -p /tmp/kapa-result
87 git add www/apps/book www/apps/resources www/apps/user-guide www/apps/ui www/apps/cloud
88 git reset -q -- www/apps/resources/references www/apps/ui/specs/components
89 git diff --staged --binary --full-index > /tmp/kapa-result/changes.patch
90 if [ -s /tmp/kapa-result.md ]; then
91 cp /tmp/kapa-result.md /tmp/kapa-result/result.md
92 fi
93
94 - name: Upload result
95 uses: actions/upload-artifact@v4
96 with:
97 name: kapa-result-${{ matrix.index }}
98 path: /tmp/kapa-result
99 retention-days: 7
100
101 open-pr:
102 needs: fix-item
103 if: always()
104 runs-on: ubuntu-latest
105 permissions:
106 contents: write
107 pull-requests: write
108 steps:
109 - name: Generate GitHub App token
110 id: app-token
111 uses: actions/create-github-app-token@v1
112 with:
113 app-id: ${{ secrets.MEDUSA_APP_ID }}
114 private-key: ${{ secrets.MEDUSA_APP_PRIVATE_KEY }}
115
116 - name: Checkout repository
117 uses: actions/checkout@v4
118 with:
119 ref: ${{ inputs.sha }}
120 token: ${{ steps.app-token.outputs.token }}
121
122 - name: Configure git
123 env:
124 BRANCH: ${{ inputs.branch }}
125 run: |
126 git config user.name "github-actions[bot]"
127 git config user.email "github-actions[bot]@users.noreply.github.com"
128 git checkout -b "$BRANCH"
129
130 - name: Download items
131 uses: actions/download-artifact@v4
132 with:
133 name: kapa-items
134 path: /tmp/kapa-items
135
136 - name: Download results
137 uses: actions/download-artifact@v4
138 with:
139 pattern: kapa-result-*
140 path: /tmp/kapa-results
141
142 - name: Apply patches
143 id: apply
144 env:
145 COMMIT_PREFIX: ${{ inputs.commit_prefix }}
146 run: |
147 FIXED=0
148 : > /tmp/pr-items.md
149
150 for i in $(jq -r '.[].index' /tmp/kapa-items/items.json); do
151 TITLE=$(jq -r --argjson i "$i" '.[] | select(.index == $i) | .title' /tmp/kapa-items/items.json)
152 THREADS=$(jq -r --argjson i "$i" '.[] | select(.index == $i) | .thread_count' /tmp/kapa-items/items.json)
153 DIR="/tmp/kapa-results/kapa-result-$i"
154 RESULT=""
155 if [ -s "$DIR/result.md" ]; then
156 RESULT=$(cat "$DIR/result.md")
157 fi
158
159 if [ ! -d "$DIR" ]; then
160 STATUS="Failed"
161 RESULT="The Claude run for this item failed. Check the workflow logs."
162 elif [ ! -s "$DIR/changes.patch" ]; then
163 STATUS="Skipped"
164 elif git apply --3way "$DIR/changes.patch"; then
165 git commit -q -m "$COMMIT_PREFIX: $TITLE"
166 STATUS="Fixed"
167 FIXED=$((FIXED + 1))
168 else
169 git reset -q --hard HEAD
170 STATUS="Conflict"
171 RESULT="$RESULT"$'\n\n'"_The changes conflicted with another item's changes and weren't applied._"
172 fi
173
174 {
175 echo "### $STATUS: $TITLE"
176 echo ""
177 echo "Threads: $THREADS"
178 echo ""
179 echo "${RESULT:-_No summary was generated._}"
180 echo ""
181 } >> /tmp/pr-items.md
182 done
183
184 echo "fixed=$FIXED" >> "$GITHUB_OUTPUT"
185 echo "Applied $FIXED fixes."
186
187 - name: Push branch
188 if: steps.apply.outputs.fixed != '0'
189 env:
190 BRANCH: ${{ inputs.branch }}
191 run: git push origin "$BRANCH"
192
193 - name: Create Pull Request
194 if: steps.apply.outputs.fixed != '0'
195 env:
196 GH_TOKEN: ${{ steps.app-token.outputs.token }}
197 REPO: ${{ github.repository }}
198 BRANCH: ${{ inputs.branch }}
199 PR_TITLE: ${{ inputs.pr_title }}
200 PR_INTRO: ${{ inputs.pr_intro }}
201 run: |
202 BODY_FILE=$(mktemp)
203
204 {
205 echo "$PR_INTRO"
206 echo ""
207 cat /tmp/pr-items.md
208 echo "> Review carefully before merging. Claude may have missed context or made"
209 echo "> incorrect assumptions."
210 } > "$BODY_FILE"
211
212 PR_NUMBER=$(gh api "repos/$REPO/pulls" \
213 --method POST \
214 --field title="$PR_TITLE" \
215 --field body="$(cat "$BODY_FILE")" \
216 --field base="develop" \
217 --field head="$BRANCH" \
218 --jq '.number')
219 gh api "repos/$REPO/issues/$PR_NUMBER/labels" \
220 --method POST \
221 --field 'labels[]=type: docs'
222 echo "Created PR #$PR_NUMBER"