-
Notifications
You must be signed in to change notification settings - Fork 6
74 lines (61 loc) · 2.76 KB
/
Security-Notification.yml
File metadata and controls
74 lines (61 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
name: Security Vulnerability Slack Notification
on:
schedule:
- cron: '0 * * * *' # Runs every hour
workflow_dispatch:
jobs:
check-alerts:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Check for Recent Alerts
env:
GH_TOKEN: ${{ secrets.DEPENDABOT_PAT }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
# 1. Calculate time 65 minutes ago
TIME_THRESHOLD=$(date -u -d '65 minutes ago' +'%Y-%m-%dT%H:%M:%SZ')
echo "Checking for alerts created after: $TIME_THRESHOLD"
# 2. Fetch alerts using GitHub CLI
# FIX: Added [ ] wrapping to force output as an Array [{...}]
ALERTS=$(gh api "/repos/${{ github.repository }}/dependabot/alerts" \
--jq "[ .[] | select(.state == \"open\") | select(.created_at > \"$TIME_THRESHOLD\") | select(.security_advisory.severity == \"critical\" or .security_advisory.severity == \"high\") ]")
# 3. Check if any alerts were found
# Now valid because ALERTS is definitely an array
LENGTH=$(echo "$ALERTS" | jq 'length')
if [ "$LENGTH" -eq 0 ]; then
echo "No new alerts found in the last hour."
exit 0
fi
echo "New alerts detected! Sending notification..."
# 4. Extract details from the first alert found
# Now valid because ALERTS is an array, so .[0] exists
PACKAGE=$(echo "$ALERTS" | jq -r '.[0] | .dependency.package.name')
SEVERITY=$(echo "$ALERTS" | jq -r '.[0] | .security_advisory.severity')
REPO_NAME="${{ github.repository }}"
ISSUE_TITLE="${PACKAGE} (${SEVERITY})"
ISSUE_USER="Dependabot"
ISSUE_URL=$(echo "$ALERTS" | jq -r '.[0] | .html_url')
# 5. Build Message
MESSAGE_TEXT=$(jq -n \
--arg repo "$REPO_NAME" \
--arg title "$ISSUE_TITLE" \
--arg user "$ISSUE_USER" \
--arg url "$ISSUE_URL" \
--arg template "*📢 New Dependabot Alert ($REPO_NAME) 📢*\n\n*Issue Title:* $ISSUE_TITLE\n*Opened By:* $ISSUE_USER\n\n*View Issue:* $ISSUE_URL" \
'$template')
# 6. Build Payload
SLACK_PAYLOAD=$(jq -n \
--arg text "$MESSAGE_TEXT" \
'{
"channel": "#docs-devdocs-notifications",
"username": "Security Vulnerability Slack Notification",
"icon_emoji": ":rotating_light:",
"text": $text
}')
# 7. Send to Slack
curl -X POST \
-H 'Content-type: application/json' \
--data "$SLACK_PAYLOAD" \
"$SLACK_WEBHOOK_URL"