1 name: Triage Dependabot Alerts
2
3 # Dismisses open Dependabot alerts on the root yarn.lock whose vulnerable package
4 # is NOT in the production dependency tree of any published @medusajs/* package
5 # (i.e. build tooling, integration-tests, devDependencies). Runs weekly and on demand.
6
7 on:
8 schedule:
9 - cron: "0 3 * * *" # Every Day at 03:00 UTC
10 workflow_dispatch: # Allow manual trigger
11 inputs:
12 dismiss:
13 description: "Actually dismiss irrelevant alerts (false = dry run)"
14 type: boolean
15 default: true
16
17 permissions:
18 contents: read
19
20 jobs:
21 triage:
22 name: Triage published-package alerts
23 runs-on: ubuntu-latest
24 steps:
25 - name: Checkout repository
26 uses: actions/checkout@v4
27
28 - name: Setup Node.js
29 uses: actions/setup-node@v4
30 with:
31 node-version: 22
32 cache: yarn
33
34 # node_modules must be present so the script can resolve each dependency's
35 # package.json when computing the production closure. Skip build scripts:
36 # we only need the installed manifests, not compiled output.
37 - name: Install dependencies
38 run: yarn install --immutable --mode=skip-build
39
40 - name: Triage alerts
41 # Schedule always dismisses; manual runs dismiss unless "dismiss" input is false.
42 run: node scripts/triage-published-alerts.mjs ${{ (github.event_name == 'workflow_dispatch' && inputs.dismiss == false) && '' || '--dismiss' }}
43 env:
44 # Built-in GITHUB_TOKEN cannot write Dependabot alerts (PATCH 403s).
45 # Provide a PAT / App token with "Dependabot alerts: write" as this secret.
46 GH_TOKEN: ${{ secrets.DEPENDABOT_TRIAGE_TOKEN }}
47
48 # After triage has dismissed the irrelevant alerts, whatever remains open is a
49 # real vulnerability reaching a published package. This job asks Claude to
50 # diagnose each remaining alert with the diagnosing-dependabot-alerts skill and
51 # apply the least-invasive fix, then opens a PR with the changes. It short-
52 # circuits when no open alerts remain.
53 fix:
54 name: Fix remaining alerts
55 needs: triage
56 runs-on: ubuntu-latest
57 permissions:
58 contents: write
59 pull-requests: write
60 steps:
61 # Bot App token, used for checkout/push/PR and for reading Dependabot
62 # alerts. NOTE: the Medusa App installation must grant "Dependabot alerts:
63 # read" for the alert queries below to succeed. As an App (not the built-in
64 # GITHUB_TOKEN), pushes from it also trigger downstream workflows (CI).
65 - name: Generate GitHub App token
66 id: app-token
67 uses: actions/create-github-app-token@v1
68 with:
69 app-id: ${{ secrets.MEDUSA_APP_ID }}
70 private-key: ${{ secrets.MEDUSA_APP_PRIVATE_KEY }}
71
72 - name: Checkout repository
73 uses: actions/checkout@v4
74 with:
75 fetch-depth: 0
76 token: ${{ steps.app-token.outputs.token }}
77
78 - name: Configure git
79 run: |
80 git config user.name "github-actions[bot]"
81 git config user.email "github-actions[bot]@users.noreply.github.com"
82
83 - name: Setup Node.js
84 uses: actions/setup-node@v4
85 with:
86 node-version: 22
87 cache: yarn
88
89 # Working manifests + lockfile must be present so Claude can trace the
90 # dependency chain and refresh the lockfile. Skip build scripts.
91 - name: Install dependencies
92 run: yarn install --immutable --mode=skip-build
93
94 # Query the alerts that survived triage and pre-fetch their details into a
95 # file for Claude to read. The built-in GITHUB_TOKEN cannot read Dependabot
96 # alerts, so use the Bot App token (needs alerts:read). Doing the fetch here
97 # means Claude never needs the GitHub API (or the App token) itself.
98 - name: Check for remaining open alerts
99 id: alerts
100 env:
101 GH_TOKEN: ${{ steps.app-token.outputs.token }}
102 REPO: ${{ github.repository }}
103 run: |
104 mkdir -p /tmp/dependabot-fix
105 # --paginate merges the array pages into one JSON array.
106 gh api --paginate \
107 "repos/$REPO/dependabot/alerts?state=open&per_page=100" \
108 | jq '[.[] | {
109 number, state,
110 package: .dependency.package.name,
111 scope: .dependency.scope,
112 relationship: .dependency.relationship,
113 manifest: .dependency.manifest_path,
114 ghsa: .security_advisory.ghsa_id,
115 severity: .security_advisory.severity,
116 summary: .security_advisory.summary,
117 vulnerable_range: .security_vulnerability.vulnerable_version_range,
118 first_patched: .security_vulnerability.first_patched_version.identifier,
119 all_ranges: [.security_advisory.vulnerabilities[]
120 | {range: .vulnerable_version_range, patched: .first_patched_version.identifier}]
121 }]' > /tmp/dependabot-fix/alerts.json
122
123 COUNT=$(jq 'length' /tmp/dependabot-fix/alerts.json)
124 if [ "$COUNT" -eq 0 ]; then
125 echo "has_alerts=false" >> "$GITHUB_OUTPUT"
126 echo "No open Dependabot alerts remain after triage. Nothing to fix."
127 else
128 echo "has_alerts=true" >> "$GITHUB_OUTPUT"
129 echo "Remaining open alerts: $(jq -r '[.[].number] | join(", ")' /tmp/dependabot-fix/alerts.json)"
130 fi
131
132 - name: Create fix branch
133 if: steps.alerts.outputs.has_alerts == 'true'
134 id: branch
135 run: |
136 BRANCH="chore/dependabot-security-fixes-$(date -u +%Y%m%d%H%M%S)"
137 git checkout -B "$BRANCH" origin/develop
138 echo "name=$BRANCH" >> "$GITHUB_OUTPUT"
139
140 - name: Run Claude Code to fix alerts
141 if: steps.alerts.outputs.has_alerts == 'true'
142 uses: anthropics/claude-code-base-action@132be24009ca6f292db66a3d1d5a17249cf68a5d # main, installs Claude Code 2.1.282
143 with:
144 anthropic_api_key: ${{ secrets.CLAUDE_CODE_API_TOKEN }}
145 # Deliberately NO GitHub API access: the alert details are pre-fetched
146 # into a file, so Claude needs no token and can only edit files + refresh
147 # the lockfile. npm view / yarn are registry ops that need no auth.
148 claude_args: >-
149 --model claude-sonnet-4-6
150 --max-turns 60
151 --allowedTools "Skill,Read,Write,Edit,Glob,Grep,Bash(npm view:*),Bash(yarn install:*),Bash(yarn up:*),Bash(node:*),Bash(grep:*),Bash(git diff:*),Bash(git status:*),Bash(git log:*),Bash(cat:*),Bash(ls:*),Bash(find:*),Bash(mkdir:*),Bash(head:*),Bash(tail:*),Bash(sed:*),Bash(wc:*),Bash(sort:*),Bash(column:*),Bash(paste:*)"
152 prompt: |
153 Load and follow the diagnosing-dependabot-alerts skill before doing anything else, by reading this file with the Read tool: .claude/skills/diagnosing-dependabot-alerts/SKILL.md
154 Also read the reference file it points to: .claude/skills/diagnosing-dependabot-alerts/reference/remediation-strategies.md
155
156 You are running in CI. The Dependabot alerts still open after triage have already been fetched for you into `/tmp/dependabot-fix/alerts.json` — read that file with the Read tool. Each entry has the alert number, package, scope, manifest, GHSA id, severity, vulnerable_range, first_patched, and all_ranges. You do NOT have GitHub API access; work only from that file. Skip step 1 (the `gh api` fetch) of the skill and use this pre-fetched data instead.
157
158 For EACH alert in that file:
159 1. Follow the skill: trace the vulnerable package to the affected workspace package(s), assess impact, and choose the least-invasive fix on the remediation ladder (prefer an in-range transitive refresh or a direct-dep bump inside the affected package; treat a root resolutions override as a last resort).
160 2. Apply the fix by editing the relevant `package.json` file(s) with the Edit tool and refreshing the lockfile with `yarn install --mode update-lockfile` (or `yarn up -R <pkg> --mode update-lockfile`).
161
162 CRITICAL rules:
163 - Only make changes with the Edit/Write tools and `yarn` for lockfile refresh. NEVER run git commit, git push, git worktree, or open a PR yourself — the workflow handles that.
164 - Do NOT apply a fix that requires a breaking change (a major-version bump of a declared dependency, a peer-dependency conflict, or changed API usage). If the only available fix for an alert is breaking, or no patched version exists, SKIP that alert and leave its dependency unchanged.
165 - After finishing, VERIFY that no version matching each alert's vulnerable range remains in `yarn.lock`.
166
167 After you finish, write your output for the workflow:
168 - If you applied one or more fixes, create the directory `/tmp/dependabot-fix` (mkdir -p) and write:
169 - `/tmp/dependabot-fix/pr-title.txt` — a single-line PR title in conventional-commit form, e.g. `chore(deps): bump <pkg> to fix security alerts`. No trailing newline beyond one.
170 - `/tmp/dependabot-fix/pr-body.md` — a markdown PR description covering, per fixed alert: the advisory/GHSA id and package, the affected workspace package, the exact remediation applied and why it is the least-invasive option, and any alerts you SKIPPED with the reason. Do NOT commit; just write these files.
171 - If you applied NO fixes at all (every alert was unfixable without a breaking change), make no file edits and do NOT create the /tmp/dependabot-fix files.
172
173 - name: Detect changes
174 if: steps.alerts.outputs.has_alerts == 'true'
175 id: changes
176 run: |
177 # Dependency fixes only modify already-tracked files (package.json, yarn.lock).
178 git add -u
179 if git diff --staged --quiet; then
180 echo "has_changes=false" >> "$GITHUB_OUTPUT"
181 echo "Claude applied no fixes (all remaining alerts were unfixable without a breaking change)."
182 else
183 echo "has_changes=true" >> "$GITHUB_OUTPUT"
184 git diff --staged --stat
185 fi
186
187 - name: Read PR metadata from Claude output
188 if: steps.changes.outputs.has_changes == 'true'
189 id: pr-meta
190 run: |
191 TITLE_FILE=/tmp/dependabot-fix/pr-title.txt
192 BODY_FILE=/tmp/dependabot-fix/pr-body.md
193
194 if [ -s "$TITLE_FILE" ]; then
195 TITLE=$(head -n 1 "$TITLE_FILE")
196 else
197 TITLE="chore(deps): apply Dependabot security fixes"
198 fi
199 echo "title=$TITLE" >> "$GITHUB_OUTPUT"
200
201 # Assemble the body: Claude's description (or a fallback) plus a caveat.
202 OUT=/tmp/pr-body-final.md
203 if [ -s "$BODY_FILE" ]; then
204 cp "$BODY_FILE" "$OUT"
205 else
206 echo "Automated Dependabot security fixes applied by Claude following the diagnosing-dependabot-alerts skill." > "$OUT"
207 fi
208 {
209 echo ""
210 echo "---"
211 echo "> Generated by Claude via the \`diagnosing-dependabot-alerts\` skill. Review carefully before merging — Claude may have missed context."
212 } >> "$OUT"
213 echo "body_file=$OUT" >> "$GITHUB_OUTPUT"
214
215 - name: Commit and push
216 if: steps.changes.outputs.has_changes == 'true'
217 run: |
218 git commit -m "${{ steps.pr-meta.outputs.title }}"
219 # Unique timestamped branch each run, so a plain push suffices.
220 git push origin "${{ steps.branch.outputs.name }}"
221
222 - name: Create or update Pull Request
223 if: steps.changes.outputs.has_changes == 'true'
224 env:
225 GH_TOKEN: ${{ steps.app-token.outputs.token }}
226 REPO: ${{ github.repository }}
227 BRANCH: ${{ steps.branch.outputs.name }}
228 TITLE: ${{ steps.pr-meta.outputs.title }}
229 BODY_FILE: ${{ steps.pr-meta.outputs.body_file }}
230 run: |
231 REPO_OWNER="${REPO%%/*}"
232 PR_NUMBER=$(gh api "repos/$REPO/pulls?head=${REPO_OWNER}:${BRANCH}&state=open" \
233 --jq '.[0].number' 2>/dev/null || echo "")
234
235 if [ -n "$PR_NUMBER" ] && [ "$PR_NUMBER" != "null" ]; then
236 gh api "repos/$REPO/pulls/$PR_NUMBER" \
237 --method PATCH \
238 --field title="$TITLE" \
239 --field body="$(cat "$BODY_FILE")"
240 echo "Updated PR #$PR_NUMBER"
241 else
242 PR_NUMBER=$(gh api "repos/$REPO/pulls" \
243 --method POST \
244 --field title="$TITLE" \
245 --field body="$(cat "$BODY_FILE")" \
246 --field base="develop" \
247 --field head="$BRANCH" \
248 --jq '.number')
249 gh api "repos/$REPO/issues/$PR_NUMBER/labels" \
250 --method POST \
251 --field 'labels[]=dependencies' >/dev/null || true
252 echo "Created PR #$PR_NUMBER"
253 fi