Skip to content

Commit f4b7b27

Browse files
authored
Reminder for npm audit fix (#202)
* NO-ISSUE Reminder for npm audit fix * NO-ISSUE update script * NO-ISSUE oops * NO-ISSUE Run script when pushing to master
1 parent 8ede15f commit f4b7b27

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

.github/scripts/npm-audit.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
dirs=()
5+
while IFS= read -r path; do
6+
dirs+=("$(dirname "$path")")
7+
done < <(
8+
find . \( -path '*/node_modules' -o -path '*/dist' \) -prune -o \
9+
\( -name package.json -o -name package-lock.json \) -print
10+
)
11+
12+
IFS=$'\n' dirs=($(printf '%s\n' "${dirs[@]}" | sort -u)); unset IFS
13+
14+
declare -a failed=()
15+
16+
for dir in "${dirs[@]}"; do
17+
printf '\n\n\033[1;34m==> %s\033[0m\n' "$dir"
18+
19+
pushd "$dir" >/dev/null
20+
if ! npm audit --audit-level moderate; then
21+
failed+=("$dir")
22+
fi
23+
popd >/dev/null
24+
done
25+
26+
if ((${#failed[@]})); then
27+
echo -e "\n\033[0;31mnpm audit reported vulnerabilities in:\033[0m"
28+
printf ' - %s\n' "${failed[@]}"
29+
echo "You can run 'npm audit fix' in these directories to resolve the issues."
30+
echo "If running 'npm audit fix' does not resolve the issues, you may need to manually update dependencies."
31+
exit 1
32+
else
33+
echo "npm audit passed: no vulnerabilities detected"
34+
exit 0
35+
fi

.github/workflows/npm-audit.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: "Reminder for 'run npm audit'"
2+
3+
on:
4+
schedule:
5+
- cron: '0 22 * * *'
6+
workflow_dispatch:
7+
push:
8+
branches:
9+
- 'master'
10+
11+
jobs:
12+
run-npm-audit:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
16+
issues: write
17+
if: github.repository == 'line/line-bot-mcp-server'
18+
steps:
19+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
20+
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
21+
with:
22+
node-version: '24'
23+
24+
- name: Run npm audit and check diff
25+
id: audit
26+
run: ./scripts/npm-audit.sh
27+
continue-on-error: true
28+
29+
- name: Create or update reminder issue
30+
if: steps.audit.outcome == 'failure'
31+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
32+
env:
33+
TZ: 'Asia/Tokyo'
34+
with:
35+
script: |
36+
const { owner, repo } = context.repo;
37+
const title = 'Reminder: run npm audit';
38+
const securityURL = `https://github.com/${owner}/${repo}/security`;
39+
const baseBody = [
40+
'Fix all vulnerabilities. You can check with `.github/scripts/npm-audit.sh` locally, then send a PR with the fixes.',
41+
`After fixing, make sure the vulnerabilities count in **${securityURL}** is **0**.`
42+
].join('\n\n');
43+
44+
const { data: result } = await github.rest.search.issuesAndPullRequests({
45+
q: `repo:${owner}/${repo} is:issue is:open in:title "${title}"`
46+
});
47+
48+
const today = new Date();
49+
50+
if (result.total_count === 0) {
51+
await github.rest.issues.create({
52+
owner,
53+
repo,
54+
title,
55+
body: `${baseBody}\n\n0 days have passed.`
56+
});
57+
} else {
58+
const issue = result.items[0];
59+
const created = new Date(issue.created_at);
60+
const diffDays = Math.floor((today - created) / 86_400_000);
61+
await github.rest.issues.update({
62+
owner,
63+
repo,
64+
issue_number: issue.number,
65+
body: `${baseBody}\n\n${diffDays} days have passed.`
66+
});
67+
}

0 commit comments

Comments
 (0)