Skip to content

Commit 48cb91b

Browse files
authored
Reminder for npm audit fix (#1357)
Most npm package alerts can be fixed automatically with `npm audit fix --force` (it is not perfect, so some manual work is still required). Because we cannot use a GitHub App token, a reminder should be enough. Automatically creating PRs would be pointless, because the CI jobs will not run, and it may be old(=merging may not resolve all issues) For now, let's create a reminder as an issue. We review issues regularly, so this should be sufficient.
1 parent 0073d94 commit 48cb91b

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

.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-sdk-nodejs'
18+
steps:
19+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
20+
- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
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 `./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+
}

scripts/npm-audit.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
IFS=$'\n'
5+
locks=($(find . -path '*/node_modules' -prune -o -name package-lock.json -print))
6+
unset IFS
7+
8+
declare -a failed=()
9+
10+
for lock in "${locks[@]}"; do
11+
dir=$(dirname "$lock")
12+
printf '\n\n\033[1;34m==> %s\033[0m\n' "$dir"
13+
14+
pushd "$dir" >/dev/null
15+
if ! npm audit --audit-level moderate; then
16+
failed+=("$dir")
17+
fi
18+
popd >/dev/null
19+
done
20+
21+
if ((${#failed[@]})); then
22+
echo -e "\n\033[0;31mnpm audit reported vulnerabilities in:\033[0m"
23+
printf ' - %s\n' "${failed[@]}"
24+
exit 1
25+
else
26+
echo "npm audit passed: no vulnerabilities detected"
27+
exit 0
28+
fi

0 commit comments

Comments
 (0)