Skip to content

Commit 984c9ae

Browse files
committed
NO-ISSUE Reminder for npm audit fix
1 parent 5d36a87 commit 984c9ae

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

.github/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

.github/workflows/npm-audit.yml

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

0 commit comments

Comments
 (0)