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