diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d6a3b89..e801692 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -187,3 +187,13 @@ jobs: source: SHASUM_UBUNTU_UNATTENDED.txt,./images/Dappnode-*-ubuntu-*-unattended.iso target: ${{ secrets.ISO_SSH_PATH }} overwrite: true + + - name: Send Telegram notification + if: success() + run: | + chmod +x ./scripts/telegram_notify.sh + RELEASE_URL="https://github.com/dappnode/DAppNode/releases/tag/${{ github.event.inputs.core }}" + ./scripts/telegram_notify.sh "${{ github.event.inputs.core }}" "${RELEASE_URL}" + env: + TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} + TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d7c85e1..bd7d034 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,7 +25,7 @@ jobs: sudo /bin/bash ./scripts/dappnode_install.sh - name: Show installation logs run: | - cat /usr/src/dappnode/logs/install.log + cat /usr/src/dappnode/logs/install.log cat /usr/src/dappnode/logs/dappnode_install.log - name: Uninstall DAppNode run: | @@ -64,3 +64,16 @@ jobs: - name: verify Ubuntu ISO run: | ls -lrt images/Dappnode-ubuntu-*.iso + + telegram: + name: test Telegram notification + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Run Telegram notification tests + run: | + chmod +x ./test/test_telegram_notify.sh + ./test/test_telegram_notify.sh diff --git a/scripts/telegram_notify.sh b/scripts/telegram_notify.sh new file mode 100755 index 0000000..61f5b58 --- /dev/null +++ b/scripts/telegram_notify.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +# Telegram notification script for DAppNode releases +# This script sends a notification to a Telegram chat when a new release is created + +set -e + +# Function to display usage +show_usage() { + echo "Usage: $0 " + echo "Example: $0 v0.2.51 https://github.com/dappnode/DAppNode/releases/tag/v0.2.51" + exit 1 +} + +# Function to send telegram message +send_telegram_message() { + local message="$1" + local bot_token="$TELEGRAM_BOT_TOKEN" + local chat_id="$TELEGRAM_CHAT_ID" + + if [ -z "$bot_token" ]; then + echo "Error: TELEGRAM_BOT_TOKEN environment variable is not set" + exit 1 + fi + + if [ -z "$chat_id" ]; then + echo "Error: TELEGRAM_CHAT_ID environment variable is not set" + exit 1 + fi + + # Send the message using curl + curl -s -X POST "https://api.telegram.org/bot${bot_token}/sendMessage" \ + -H "Content-Type: application/json" \ + -d "{ + \"chat_id\": \"${chat_id}\", + \"text\": \"${message}\", + \"parse_mode\": \"Markdown\", + \"disable_web_page_preview\": false + }" > /dev/null + + local exit_code=$? + if [ $exit_code -eq 0 ]; then + echo "Telegram notification sent successfully" + else + echo "Failed to send Telegram notification (exit code: $exit_code)" + exit 1 + fi +} + +# Check if required arguments are provided +if [ $# -ne 2 ]; then + echo "Error: Missing required arguments" + show_usage +fi + +RELEASE_VERSION="$1" +RELEASE_URL="$2" + +# Validate release version format +if [[ ! $RELEASE_VERSION =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "Error: Release version must be in format vX.Y.Z (e.g., v0.2.51)" + exit 1 +fi + +# Validate release URL +if [[ ! $RELEASE_URL =~ ^https://github\.com/dappnode/DAppNode/releases/tag/.+ ]]; then + echo "Error: Invalid release URL format" + exit 1 +fi + +# Create the message +MESSAGE="🚀 *New DAppNode Release Available!* + +📦 **Version:** \`${RELEASE_VERSION}\` +🔗 **Download:** [${RELEASE_VERSION}](${RELEASE_URL}) + +The latest DAppNode release is now available with updated core packages and improvements. Visit the release page to download the installation ISOs and view the complete changelog. + +#DAppNode #Release #Decentralized" + +# Send the notification +echo "Sending Telegram notification for release ${RELEASE_VERSION}..." +send_telegram_message "$MESSAGE" \ No newline at end of file diff --git a/test/test_telegram_notify.sh b/test/test_telegram_notify.sh new file mode 100755 index 0000000..1e781fb --- /dev/null +++ b/test/test_telegram_notify.sh @@ -0,0 +1,88 @@ +#!/bin/bash + +# Test script for telegram_notify.sh +# Tests the validation logic and message formatting + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +TELEGRAM_SCRIPT="${SCRIPT_DIR}/../scripts/telegram_notify.sh" + +echo "Testing Telegram notification script..." + +# Test 1: No arguments +echo "Test 1: No arguments" +if $TELEGRAM_SCRIPT 2>/dev/null; then + echo "❌ FAILED: Should have failed with no arguments" + exit 1 +else + echo "✅ PASSED: Correctly failed with no arguments" +fi + +# Test 2: Invalid version format +echo "Test 2: Invalid version format" +if $TELEGRAM_SCRIPT "0.2.51" "https://github.com/dappnode/DAppNode/releases/tag/v0.2.51" 2>/dev/null; then + echo "❌ FAILED: Should have failed with invalid version format" + exit 1 +else + echo "✅ PASSED: Correctly failed with invalid version format" +fi + +# Test 3: Invalid URL format +echo "Test 3: Invalid URL format" +if $TELEGRAM_SCRIPT "v0.2.51" "https://invalid-url.com" 2>/dev/null; then + echo "❌ FAILED: Should have failed with invalid URL format" + exit 1 +else + echo "✅ PASSED: Correctly failed with invalid URL format" +fi + +# Test 4: Valid arguments but missing environment variables +echo "Test 4: Valid arguments but missing environment variables" +if $TELEGRAM_SCRIPT "v0.2.51" "https://github.com/dappnode/DAppNode/releases/tag/v0.2.51" 2>/dev/null; then + echo "❌ FAILED: Should have failed with missing environment variables" + exit 1 +else + echo "✅ PASSED: Correctly failed with missing environment variables" +fi + +# Test 5: Valid arguments with mock environment variables (dry run) +echo "Test 5: Valid arguments with test environment variables" +export TELEGRAM_BOT_TOKEN="test_token_123456" +export TELEGRAM_CHAT_ID="test_chat_id" + +# Create a mock curl command that doesn't actually send anything +create_mock_curl() { + cat > /tmp/mock_curl.sh << 'EOF' +#!/bin/bash +# Mock curl command for testing +if [[ "$*" == *"sendMessage"* ]]; then + echo "Mock: Telegram message would be sent" + exit 0 +fi +exec /usr/bin/curl "$@" +EOF + chmod +x /tmp/mock_curl.sh +} + +# Test the script with a dry run by temporarily replacing curl +create_mock_curl +export PATH="/tmp:$PATH" + +# Temporarily modify the telegram script to use our mock curl +sed 's|curl -s|/tmp/mock_curl.sh -s|g' $TELEGRAM_SCRIPT > /tmp/telegram_notify_test.sh +chmod +x /tmp/telegram_notify_test.sh + +if /tmp/telegram_notify_test.sh "v0.2.51" "https://github.com/dappnode/DAppNode/releases/tag/v0.2.51" 2>/dev/null; then + echo "✅ PASSED: Script executed successfully with valid arguments" +else + echo "❌ FAILED: Script should have succeeded with valid arguments and environment variables" + exit 1 +fi + +# Clean up +rm -f /tmp/mock_curl.sh /tmp/telegram_notify_test.sh +unset TELEGRAM_BOT_TOKEN TELEGRAM_CHAT_ID + +echo "" +echo "🎉 All tests passed! The Telegram notification script is working correctly." \ No newline at end of file