GithubのDependabotで通知を送る

● GithubのDependabotで通知を送る

GitHubのDependabotを活用して、npmやcomposerなどのパッケージの脆弱性を監視し、通知を受け取る設定は次のように行えます。

なお、脆弱性データベースはこちらを参照しています。

https://github.com/advisories

1.リポジトリにDependabotを有効化する

リポジトリのSettings > ( Security ) Code security and analysis  を選択し
「Dependabot alerts」
「Enable Dependabot alerts」と「Enable Dependabot security updates」の両方にチェックを入れます。

オプションの説明はこちらです。

2.脆弱性の通知設定を行う

Settings > Security > Code security and analysis > Dependabot alerts で通知設定を行います。
「Add advisors...」から通知を受け取る対象のメンバーやTeamを追加
「Add more paths」から監視対象のディレクトリ(package.jsonがある場所)を指定

デフォルトでは脆弱性が見つかった際に新しいアラートが作成されたときにメールが送信されます。

● GitHubのDependabotの通知をSlackの指定したチャンネルに送る

・1. dependabotアラート の json を取得する

curl -s \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  https://api.github.com/repos/OWNER/REPO/dependabot/alerts \
  > alerts.json

YOUR_TOKEN , OWNER , REPO は適宜設定してください。

あとは jq で好きなように整形できます。

・2. GitHub Action ワークフローを作成する

.github/workflows/dependabot-alerts.yml

name: Dependabot Alerts to Slack

on:
  schedule:
    - cron: '0 0 * * *'  # 毎日実行(必要に応じて調整)
  push:
    branches:
      - main  # メインブランチにプッシュされたときに実行

jobs:
  dependabot_alerts:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3  # Node.js 20対応

      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '20'

      - name: Fetch Dependabot alerts
        id: fetch_alerts
        run: |
          curl -s \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${{ secrets.DEPENDABOT_PAT }}" \
            https://api.github.com/repos/${{ github.repository }}/dependabot/alerts \
            | jq '.' > alerts.json
          count=$(jq length alerts.json)
          echo "count=$count" >> $GITHUB_ENV
          echo "Number of alerts: $count"

      - name: Debug JSON
        run: cat alerts.json

      - name: Send to Slack
        run: |
          <加工してslackに送信するスクリプト>

以下のシークレットが必要なので、プロジェクトの「Settings」 → 「Secrets and variables」 から登録します。

DEPENDABOT_PAT: パーソナルアクセストークンをGithubの対象プロジェクトのシークレットに登録します。
SLACK_DEPENDABOT_WEBHOOK_URL: SlackのフックURLを発行してGithubの対象プロジェクトのシークレットに登録します。

● .github/dependabot.yml で対象エコシステムを設定する

https://tech.macloud.jp/entry/2022/07/27/182109

GitHub ActionsでSlack通知するなら8398a7/action-slackが機能豊富かつお手軽で超便利

添付ファイル1
添付ファイル2
No.2490
07/16 09:00

edit

添付ファイル