|
6 | 6 | # notifications to Slack for significant feature releases. |
7 | 7 | # |
8 | 8 | # Usage: |
9 | | -# notify-slack-on-merge.sh <changelog_file> <commit_sha> <commit_url> <author> |
| 9 | +# Production mode: |
| 10 | +# notify-slack-on-merge.sh <changelog_file> <commit_sha> <commit_url> <author> |
| 11 | +# |
| 12 | +# Test mode (uses sample diff, doesn't send to Slack): |
| 13 | +# notify-slack-on-merge.sh --test |
10 | 14 | # |
11 | 15 | # Environment variables required: |
12 | 16 | # ANTHROPIC_API_KEY - API key for Claude AI |
13 | | -# SLACK_WEBHOOK_URL - Slack webhook URL for notifications |
| 17 | +# SLACK_WEBHOOK_URL - Slack webhook URL (not required in test mode) |
14 | 18 | # |
15 | 19 |
|
16 | 20 | set -euo pipefail |
17 | 21 |
|
18 | | -# Check required arguments |
19 | | -if [ $# -ne 4 ]; then |
| 22 | +# Colors for output |
| 23 | +RED='\033[0;31m' |
| 24 | +GREEN='\033[0;32m' |
| 25 | +YELLOW='\033[1;33m' |
| 26 | +NC='\033[0m' # No Color |
| 27 | + |
| 28 | +# Parse command line arguments |
| 29 | +TEST_MODE=false |
| 30 | +if [ $# -eq 1 ] && [ "$1" = "--test" ]; then |
| 31 | + TEST_MODE=true |
| 32 | + echo "=== Slack Notification Test Mode ===" |
| 33 | + echo "" |
| 34 | +elif [ $# -ne 4 ]; then |
20 | 35 | echo "Usage: $0 <changelog_file> <commit_sha> <commit_url> <author>" |
| 36 | + echo " or: $0 --test" |
21 | 37 | exit 1 |
22 | 38 | fi |
23 | 39 |
|
24 | | -CHANGELOG_FILE="$1" |
25 | | -COMMIT_SHA="$2" |
26 | | -COMMIT_URL="$3" |
27 | | -AUTHOR="$4" |
28 | | - |
29 | 40 | # Check required environment variables |
30 | 41 | if [ -z "${ANTHROPIC_API_KEY:-}" ]; then |
31 | | - echo "Error: ANTHROPIC_API_KEY environment variable is required" |
32 | | - exit 1 |
33 | | -fi |
34 | | - |
35 | | -if [ -z "${SLACK_WEBHOOK_URL:-}" ]; then |
36 | | - echo "Error: SLACK_WEBHOOK_URL environment variable is required" |
37 | | - exit 1 |
38 | | -fi |
39 | | - |
40 | | -# Check if changelog file exists |
41 | | -if [ ! -f "${CHANGELOG_FILE}" ]; then |
42 | | - echo "Error: Changelog file not found: ${CHANGELOG_FILE}" |
| 42 | + echo -e "${RED}Error: ANTHROPIC_API_KEY environment variable is required${NC}" |
43 | 43 | exit 1 |
44 | 44 | fi |
45 | 45 |
|
46 | | -# Get the changelog diff from the previous commit |
47 | | -DIFF=$(git diff HEAD~1 HEAD -- "${CHANGELOG_FILE}") |
48 | | - |
49 | | -# Check if there are any changes |
50 | | -if [ -z "${DIFF}" ]; then |
51 | | - echo "No changelog changes detected" |
52 | | - exit 0 |
| 46 | +if [ "${TEST_MODE}" = "false" ]; then |
| 47 | + # Production mode - check all requirements |
| 48 | + CHANGELOG_FILE="$1" |
| 49 | + COMMIT_SHA="$2" |
| 50 | + COMMIT_URL="$3" |
| 51 | + AUTHOR="$4" |
| 52 | + |
| 53 | + if [ -z "${SLACK_WEBHOOK_URL:-}" ]; then |
| 54 | + echo -e "${RED}Error: SLACK_WEBHOOK_URL environment variable is required${NC}" |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + |
| 58 | + # Check if changelog file exists |
| 59 | + if [ ! -f "${CHANGELOG_FILE}" ]; then |
| 60 | + echo -e "${RED}Error: Changelog file not found: ${CHANGELOG_FILE}${NC}" |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + |
| 64 | + # Get the changelog diff from the previous commit |
| 65 | + DIFF=$(git diff HEAD~1 HEAD -- "${CHANGELOG_FILE}") |
| 66 | + |
| 67 | + # Check if there are any changes |
| 68 | + if [ -z "${DIFF}" ]; then |
| 69 | + echo "No changelog changes detected" |
| 70 | + exit 0 |
| 71 | + fi |
| 72 | + |
| 73 | + echo "Changelog changes detected, analyzing with Claude AI..." |
| 74 | +else |
| 75 | + # Test mode - use sample data |
| 76 | + echo -e "${GREEN}✅ ANTHROPIC_API_KEY is set${NC}" |
| 77 | + echo "" |
| 78 | + |
| 79 | + COMMIT_SHA="abc1234" |
| 80 | + COMMIT_URL="https://github.com/sparkfabrik/sparkdock/commit/abc1234567890" |
| 81 | + AUTHOR="test-user" |
| 82 | + |
| 83 | + # Sample changelog diff for testing |
| 84 | + DIFF='--- a/CHANGELOG.md |
| 85 | ++++ b/CHANGELOG.md |
| 86 | +@@ -8,6 +8,7 @@ |
| 87 | + ## [Unreleased] |
| 88 | +
|
| 89 | + ### Added |
| 90 | ++- Added automated Slack notifications for significant feature releases merged to master branch (using Claude AI to analyze changelog and generate user-friendly announcements for #tech channel) |
| 91 | + - Added Visual Studio Code Insiders to default package list for early access to new VSCode features |
| 92 | +
|
| 93 | + ### Fixed' |
| 94 | + |
| 95 | + echo "Sample changelog diff:" |
| 96 | + echo "---" |
| 97 | + echo "${DIFF}" |
| 98 | + echo "---" |
| 99 | + echo "" |
| 100 | + echo -e "${YELLOW}Calling Claude API...${NC}" |
| 101 | + echo "" |
53 | 102 | fi |
54 | 103 |
|
55 | | -echo "Changelog changes detected, analyzing with Claude AI..." |
56 | | - |
57 | 104 | # Create a prompt for Claude to analyze the changelog |
58 | 105 | PROMPT="You are analyzing a CHANGELOG.md diff for a macOS development environment provisioner called Sparkdock. |
59 | 106 |
|
@@ -120,12 +167,27 @@ SHOULD_NOTIFY=$(echo "${CLAUDE_RESPONSE}" | jq -r '.should_notify') |
120 | 167 | MESSAGE=$(echo "${CLAUDE_RESPONSE}" | jq -r '.message // ""') |
121 | 168 |
|
122 | 169 | if [ "${SHOULD_NOTIFY}" != "true" ]; then |
123 | | - echo "No significant features detected - skipping notification" |
| 170 | + if [ "${TEST_MODE}" = "true" ]; then |
| 171 | + echo -e "${YELLOW}ℹ Claude determined no notification should be sent${NC}" |
| 172 | + echo "This is expected for minor changes or bug fixes only" |
| 173 | + else |
| 174 | + echo "No significant features detected - skipping notification" |
| 175 | + fi |
124 | 176 | exit 0 |
125 | 177 | fi |
126 | 178 |
|
127 | | -echo "Significant features detected, sending Slack notification..." |
128 | | -echo "Message: ${MESSAGE}" |
| 179 | +if [ "${TEST_MODE}" = "true" ]; then |
| 180 | + echo -e "${GREEN}✅ Claude determined this should trigger a notification${NC}" |
| 181 | + echo "" |
| 182 | + echo "Generated message:" |
| 183 | + echo "---" |
| 184 | + echo "${MESSAGE}" |
| 185 | + echo "---" |
| 186 | + echo "" |
| 187 | +else |
| 188 | + echo "Significant features detected, sending Slack notification..." |
| 189 | + echo "Message: ${MESSAGE}" |
| 190 | +fi |
129 | 191 |
|
130 | 192 | # Create Slack message payload with blocks for better formatting |
131 | 193 | PAYLOAD=$(jq -n \ |
@@ -164,17 +226,27 @@ PAYLOAD=$(jq -n \ |
164 | 226 | ] |
165 | 227 | }') |
166 | 228 |
|
167 | | -# Send to Slack |
168 | | -HTTP_STATUS=$(curl -s -o /tmp/slack-response.txt -w "%{http_code}" \ |
169 | | - -X POST \ |
170 | | - -H 'Content-Type: application/json' \ |
171 | | - -d "${PAYLOAD}" \ |
172 | | - "${SLACK_WEBHOOK_URL}") |
173 | | - |
174 | | -if [ "${HTTP_STATUS}" = "200" ]; then |
175 | | - echo "✅ Slack notification sent successfully" |
| 229 | +if [ "${TEST_MODE}" = "true" ]; then |
| 230 | + echo "Generated Slack payload:" |
| 231 | + echo "${PAYLOAD}" | jq . |
| 232 | + echo "" |
| 233 | + echo -e "${YELLOW}⚠ Test mode - not sending to Slack${NC}" |
| 234 | + echo "To test actual Slack integration, set SLACK_WEBHOOK_URL and run in production mode" |
| 235 | + echo "" |
| 236 | + echo -e "${GREEN}=== Test completed successfully ===${NC}" |
176 | 237 | else |
177 | | - echo "❌ Failed to send Slack notification. HTTP status: ${HTTP_STATUS}" |
178 | | - cat /tmp/slack-response.txt |
179 | | - exit 1 |
| 238 | + # Send to Slack |
| 239 | + HTTP_STATUS=$(curl -s -o /tmp/slack-response.txt -w "%{http_code}" \ |
| 240 | + -X POST \ |
| 241 | + -H 'Content-Type: application/json' \ |
| 242 | + -d "${PAYLOAD}" \ |
| 243 | + "${SLACK_WEBHOOK_URL}") |
| 244 | + |
| 245 | + if [ "${HTTP_STATUS}" = "200" ]; then |
| 246 | + echo "✅ Slack notification sent successfully" |
| 247 | + else |
| 248 | + echo "❌ Failed to send Slack notification. HTTP status: ${HTTP_STATUS}" |
| 249 | + cat /tmp/slack-response.txt |
| 250 | + exit 1 |
| 251 | + fi |
180 | 252 | fi |
0 commit comments