|
1 | 1 | name: Security Vulnerability Slack Notification |
2 | 2 |
|
3 | 3 | on: |
4 | | - schedule: |
5 | | - - cron: "0 */2 * * *" # Every 2 hours |
6 | | - workflow_dispatch: # Allow manual trigger from GitHub Actions tab |
| 4 | + repository_vulnerability_alert: |
| 5 | + types: [create] |
7 | 6 |
|
8 | 7 | jobs: |
9 | | - notify_slack_on_alert: |
| 8 | + notify-slack: |
10 | 9 | runs-on: ubuntu-latest |
11 | | - |
12 | 10 | steps: |
13 | | - - name: Fetch Open Dependabot Alerts |
| 11 | + - name: Send Slack Notification |
14 | 12 | env: |
15 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
16 | | - SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} |
| 13 | + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
| 14 | + # Extract alert details from the event payload |
| 15 | + PACKAGE_NAME: ${{ github.event.alert.affected_package_name }} |
| 16 | + SEVERITY: ${{ github.event.alert.severity }} |
| 17 | + ALERT_URL: ${{ github.event.alert.html_url }} |
| 18 | + ECOSYSTEM: ${{ github.event.alert.affected_range }} |
17 | 19 | run: | |
18 | | - echo ":mag: Checking for open Dependabot vulnerability alerts..." |
19 | | - |
20 | | - # Fetch open alerts from GitHub API |
21 | | - RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ |
22 | | - "https://github.com/mongodb/docs-sample-apps/security/dependabot/alerts?state=open") |
23 | | -
|
24 | | - ALERT_COUNT=$(echo "$RESPONSE" | jq length) |
25 | | - echo "Found $ALERT_COUNT open alerts." |
26 | | -
|
27 | | - # Only send Slack message if there are alerts |
28 | | - if [ "$ALERT_COUNT" -gt 0 ]; then |
29 | | - echo ":warning: Sending Slack notification..." |
30 | | -
|
31 | | - MESSAGE_TEXT="*:rotating_light: Dependabot found $ALERT_COUNT open security alert(s) in ${{ github.repository }}!* :rotating_light:\n\n" |
32 | | -
|
33 | | - # Loop through each alert to include details |
34 | | - for i in $(seq 0 $(($ALERT_COUNT - 1))); do |
35 | | - PACKAGE=$(echo "$RESPONSE" | jq -r ".[$i].security_vulnerability.package.name") |
36 | | - ECOSYSTEM=$(echo "$RESPONSE" | jq -r ".[$i].security_vulnerability.package.ecosystem") |
37 | | - SEVERITY=$(echo "$RESPONSE" | jq -r ".[$i].security_vulnerability.severity") |
38 | | - URL=$(echo "$RESPONSE" | jq -r ".[$i].html_url") |
39 | | -
|
40 | | - MESSAGE_TEXT+="*Package:* ${PACKAGE} (${ECOSYSTEM})\n" |
41 | | - MESSAGE_TEXT+="*Severity:* ${SEVERITY}\n" |
42 | | - MESSAGE_TEXT+="*Details:* ${URL}\n\n" |
43 | | - done |
44 | | -
|
45 | | - # Build Slack payload |
46 | | - SLACK_PAYLOAD=$(jq -n \ |
47 | | - --arg text "$MESSAGE_TEXT" \ |
48 | | - '{ |
49 | | - "channel": "#docs-devdocs-notifications", |
50 | | - "username": "Dependabot Notifier", |
51 | | - "icon_emoji": ":lock:", |
52 | | - "text": $text |
53 | | - }') |
54 | | -
|
55 | | - # Send notification to Slack |
56 | | - curl -X POST \ |
57 | | - -H 'Content-type: application/json' \ |
58 | | - --data "$SLACK_PAYLOAD" \ |
59 | | - "$SLACK_WEBHOOK" |
60 | | -
|
61 | | - echo ":white_check_mark: Slack notification sent successfully!" |
| 20 | + # Map severity to an emoji for better visibility |
| 21 | + if [ "$SEVERITY" == "critical" ]; then |
| 22 | + EMOJI=":rotating_light:" |
| 23 | + elif [ "$SEVERITY" == "high" ]; then |
| 24 | + EMOJI=":warning:" |
62 | 25 | else |
63 | | - echo ":white_check_mark: No active alerts — no Slack message sent." |
| 26 | + EMOJI=":information_source:" |
64 | 27 | fi |
| 28 | +
|
| 29 | + # Construct the JSON payload |
| 30 | + PAYLOAD=$(cat <<EOF |
| 31 | + { |
| 32 | + "channel": "#docs-devdocs-notifications", |
| 33 | + "username": "Dependabot Alert", |
| 34 | + "icon_emoji": ":robot_face:", |
| 35 | + "attachments": [ |
| 36 | + { |
| 37 | + "color": "#D00000", |
| 38 | + "blocks": [ |
| 39 | + { |
| 40 | + "type": "section", |
| 41 | + "text": { |
| 42 | + "type": "mrkdwn", |
| 43 | + "text": "$EMOJI *New Vulnerability Alert Detected*" |
| 44 | + } |
| 45 | + }, |
| 46 | + { |
| 47 | + "type": "section", |
| 48 | + "fields": [ |
| 49 | + { |
| 50 | + "type": "mrkdwn", |
| 51 | + "text": "*Package:*\n$PACKAGE_NAME" |
| 52 | + }, |
| 53 | + { |
| 54 | + "type": "mrkdwn", |
| 55 | + "text": "*Severity:*\n$SEVERITY" |
| 56 | + } |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "type": "section", |
| 61 | + "text": { |
| 62 | + "type": "mrkdwn", |
| 63 | + "text": "<$ALERT_URL|View Alert Details on GitHub>" |
| 64 | + } |
| 65 | + } |
| 66 | + ] |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | + EOF |
| 71 | + ) |
| 72 | +
|
| 73 | + # Send the request |
| 74 | + curl -X POST -H 'Content-type: application/json' --data "$PAYLOAD" "$SLACK_WEBHOOK_URL" |
0 commit comments