Skip to content

Commit 91d2761

Browse files
Consolidate test and production scripts with test mode
- Add --test flag to notify-slack-on-merge.sh for testing without Slack - Remove duplicate test-slack-notification.sh script (196 lines eliminated) - Test mode uses sample diff and doesn't require SLACK_WEBHOOK_URL - Updated documentation in README.md and SLACK_NOTIFICATION_EXAMPLES.md - Script size: 252 lines (reasonable, not too large) - All 10 workflow tests still passing Benefits: - Single source of truth (no code duplication) - Easier maintenance and updates - Test behavior matches production exactly - Simpler to understand and use Co-authored-by: paolomainardi <8747+paolomainardi@users.noreply.github.com>
1 parent 04f3ab2 commit 91d2761

4 files changed

Lines changed: 153 additions & 273 deletions

File tree

.github/workflows/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ Tests for the SparkJust task runner.
4141
5. Copy the webhook URL
4242
6. Add it as a repository secret named `SLACK_WEBHOOK_URL`
4343

44+
#### Testing the Notification Script
45+
You can test the notification logic without sending to Slack:
46+
47+
```bash
48+
# Test mode (uses sample diff, doesn't send to Slack)
49+
export ANTHROPIC_API_KEY="your-key"
50+
./bin/notify-slack-on-merge.sh --test
51+
```
52+
53+
This allows you to:
54+
- Verify Claude AI integration
55+
- Test message generation
56+
- Validate Slack payload structure
57+
- All without needing `SLACK_WEBHOOK_URL` or git history
58+
4459
#### Message Format
4560
The workflow generates messages using Claude AI to:
4661
- Focus on user-facing benefits

bin/notify-slack-on-merge.sh

Lines changed: 117 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,101 @@
66
# notifications to Slack for significant feature releases.
77
#
88
# 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
1014
#
1115
# Environment variables required:
1216
# 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)
1418
#
1519

1620
set -euo pipefail
1721

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
2035
echo "Usage: $0 <changelog_file> <commit_sha> <commit_url> <author>"
36+
echo " or: $0 --test"
2137
exit 1
2238
fi
2339

24-
CHANGELOG_FILE="$1"
25-
COMMIT_SHA="$2"
26-
COMMIT_URL="$3"
27-
AUTHOR="$4"
28-
2940
# Check required environment variables
3041
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}"
4343
exit 1
4444
fi
4545

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 ""
53102
fi
54103

55-
echo "Changelog changes detected, analyzing with Claude AI..."
56-
57104
# Create a prompt for Claude to analyze the changelog
58105
PROMPT="You are analyzing a CHANGELOG.md diff for a macOS development environment provisioner called Sparkdock.
59106
@@ -120,12 +167,27 @@ SHOULD_NOTIFY=$(echo "${CLAUDE_RESPONSE}" | jq -r '.should_notify')
120167
MESSAGE=$(echo "${CLAUDE_RESPONSE}" | jq -r '.message // ""')
121168

122169
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
124176
exit 0
125177
fi
126178

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
129191

130192
# Create Slack message payload with blocks for better formatting
131193
PAYLOAD=$(jq -n \
@@ -164,17 +226,27 @@ PAYLOAD=$(jq -n \
164226
]
165227
}')
166228

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}"
176237
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
180252
fi

0 commit comments

Comments
 (0)