Skip to content

Commit 0c2a195

Browse files
committed
feat: added npm audit fix to dependabot PRs
1 parent 18ccfe8 commit 0c2a195

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

.github/WORKFLOWS.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ This is the main orchestrator workflow that runs analysis tasks in parallel, the
1414

1515
**Stage 1: Parallel Analysis** (All run simultaneously, no build required)
1616

17-
- **Lint & Code Style** - XO linting, Markdown linting, Package checks by publint, Prettier checks
17+
- **Lint & Code Style** - XO linting, Markdown linting, Package checks by publint, Prettier checks, Spell checking with codespell
1818
- **Quality Analysis** - Test coverage, quality metrics (embedded quality checks)
19-
- **Security Analysis** - CodeQL security scanning (embedded security checks)
19+
- **Security Analysis** - CodeQL security scanning (calls `codeql.yml`)
20+
- **Audit Fix for Dependabot** - Automatically suggests `npm audit fix` for Dependabot PRs (calls `audit-fix-pr.yml`, conditional)
2021

2122
**Stage 2: Build & Test** (Requires all Stage 1 to pass)
2223

@@ -135,6 +136,35 @@ _Note: Comprehensive linting has been moved to default.yml to avoid duplication_
135136

136137
_Note: This workflow remains independent as it handles specialized release processes_
137138

139+
### 9. **Audit Fix for Dependabot PRs** (`.github/workflows/audit-fix-pr.yml`)
140+
141+
**Triggers:** Dependabot PRs, Workflow calls from default.yml
142+
143+
**Features:**
144+
145+
- Automatically runs `npm audit fix` on Dependabot PRs
146+
- Creates follow-up PRs with security fixes when audit issues are found
147+
- Branches off the Dependabot PR for seamless integration
148+
- Adds appropriate labels (`security`, `dependabot`) for easy tracking
149+
- Only runs when Dependabot is the actor, minimizing unnecessary executions
150+
151+
**Workflow:**
152+
153+
1. Dependabot creates a PR with dependency updates
154+
2. Audit-fix workflow detects it's a Dependabot PR
155+
3. Runs `npm audit fix` to resolve any security vulnerabilities
156+
4. If changes are found, creates a new PR based on the Dependabot branch
157+
5. The new PR includes the audit fixes on top of the dependency updates
158+
159+
**Benefits:**
160+
161+
- **Proactive Security**: Catches and fixes security issues introduced by dependency updates
162+
- **Modular Design**: Separate workflow file maintains clean separation of concerns
163+
- **Automated Resolution**: Reduces manual intervention for common security fixes
164+
- **Clear Tracking**: Separate PRs make it easy to review security changes independently
165+
166+
_Note: This workflow can run independently on PRs or be called from default.yml as part of the main pipeline_
167+
138168
## Workflow Architecture
139169

140170
### Parallel + Sequential Design
@@ -146,7 +176,8 @@ Default.yml (Orchestrator)
146176
├── Stage 1: Parallel Analysis (simultaneous)
147177
│ ├── Lint & Code Style
148178
│ ├── Quality Analysis (with embedded testing)
149-
│ └── Security Analysis (CodeQL)
179+
│ ├── Security Analysis (CodeQL)
180+
│ └── Audit Fix for Dependabot (conditional)
150181
├── Stage 2: CI Tests & Build (requires all Stage 1)
151182
├── Stage 3: Performance Tests (conditional, requires Stage 2)
152183
├── Stage 4: Deploy (main only, requires Stages 2-3)
@@ -258,6 +289,7 @@ All workflows use npm caching with `actions/setup-node@v4` to speed up dependenc
258289
- **CodeQL Analysis**: Automated security scanning
259290
- **npm audit**: Dependency vulnerability checking
260291
- **Dependabot**: Automated dependency updates
292+
- **Audit Fix Automation**: Automatically suggests `npm audit fix` for Dependabot PRs by creating follow-up PRs with security fixes
261293
- **Private security reporting**: Configured in issue templates
262294

263295
## Usage Examples

.github/workflows/audit-fix-pr.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Suggest npm audit fix for Dependabot PRs
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize]
6+
branches: [main]
7+
workflow_call:
8+
9+
jobs:
10+
audit-fix:
11+
if: github.actor == 'dependabot[bot]'
12+
runs-on: ubuntu-24.04
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
with:
18+
ref: ${{ github.head_ref }}
19+
token: ${{ secrets.GITHUB_TOKEN }}
20+
21+
- name: Set up Node.js
22+
uses: actions/setup-node@v4
23+
with:
24+
node-version-file: ".nvmrc"
25+
cache: "npm"
26+
27+
- name: Install dependencies
28+
run: npm ci
29+
30+
- name: Run npm audit fix
31+
run: npm audit fix
32+
33+
- name: Check if only package-lock.json changed
34+
id: changes
35+
run: |
36+
git diff --name-only > changed_files.txt
37+
cat changed_files.txt
38+
39+
if grep -Fxq "package-lock.json" changed_files.txt && [ "$(wc -l < changed_files.txt)" -eq 1 ]; then
40+
echo "changed=true" >> "$GITHUB_OUTPUT"
41+
else
42+
echo "changed=false" >> "$GITHUB_OUTPUT"
43+
fi
44+
45+
- name: Create PR with audit fix
46+
if: steps.changes.outputs.changed == 'true'
47+
uses: peter-evans/create-pull-request@v6
48+
with:
49+
token: ${{ secrets.GITHUB_TOKEN }}
50+
commit-message: "chore: npm audit fix"
51+
branch: "refactor-auditfix-${{ github.head_ref }}"
52+
title: "chore: Apply npm audit fix on top of #${{ github.event.pull_request.number }}"
53+
labels: security, dependabot
54+
body: |
55+
This PR applies `npm audit fix` on top of the Dependabot PR #${{ github.event.pull_request.number }}.
56+
57+
It is created automatically to suggest applying security updates that `npm audit fix` can resolve.
58+
base: ${{ github.head_ref }}
59+
60+
- name: Approve PR
61+
env:
62+
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
63+
run: |
64+
gh pr review ${{ steps.create-pr.outputs.pull-request-url }} --approve
65+
66+
- name: Enable Auto-Merge
67+
env:
68+
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
69+
run: |
70+
gh pr merge ${{ steps.create-pr.outputs.pull-request-url }} --auto --squash

.github/workflows/default.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,19 @@ jobs:
101101
security-events: write
102102
packages: read
103103

104+
audit-fix:
105+
name: Suggest npm audit fix for Dependabot PRs
106+
uses: ./.github/workflows/audit-fix-pr.yml
107+
secrets: inherit
108+
permissions:
109+
contents: write
110+
pull-requests: write
111+
104112
# Stage 2: Build and comprehensive testing (requires all analysis to pass)
105113
ci:
106114
name: CI Tests & Build
107115
needs: [lint, quality, security]
116+
if: always() && (needs.lint.result == 'success') && (needs.quality.result == 'success') && (needs.security.result == 'success')
108117
uses: ./.github/workflows/ci.yml
109118
secrets: inherit
110119
permissions:

0 commit comments

Comments
 (0)