MINOR: Enhance Slack Link Monitoring Workflow and Script (#20604)

This commit is contained in:
Ayush Shah 2025-04-22 10:38:29 +05:30 committed by GitHub
parent e725807bfd
commit 1f81025f54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 95 additions and 51 deletions

View File

@ -12,64 +12,51 @@
name: monitor-slack-link
on:
schedule:
- cron: '0 */2 * * *'
# Run every 2 hours
- cron: '0 */2 * * *'
workflow_dispatch:
# Grant least privilege permissions needed
permissions:
id-token: write
contents: read
contents: read # Needed for checkout and reading requirements.txt
jobs:
monitor-slack-link:
runs-on: ubuntu-latest
strategy:
fail-fast: false # Ensure all matrix jobs run even if one fails
# Only run specific matrix job when manually triggered with selection
steps:
- name: Free Disk Space (Ubuntu)
uses: jlumbroso/free-disk-space@main
with:
tool-cache: false
android: true
dotnet: true
haskell: true
large-packages: false
docker-images: true
swap-storage: true
# Step 1: Checkout repository code
- name: Checkout
uses: actions/checkout@v4
- name: Set up Python 3.9
# Step 2: Set up Python environment with caching
- name: Set up Python 3.9 # Or consider a newer version like 3.11/3.12
uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.9 # Or e.g., '3.11'
cache: 'pip'
- name: Install Deps
# Step 3: Install dependencies
- name: Install Dependencies
run: |
python -m venv env
source env/bin/activate
pip install requests
pip install --upgrade pip requests types-requests slack_sdk==3.35.0
- name: Monitor Link
# Step 4: Run the monitoring script
- name: Monitor Slack Link
id: monitor
continue-on-error: true
env:
PYTHONUNBUFFERED: "1"
run: |
source env/bin/activate
python scripts/slack-link-monitor.py
- name: Slack on Failure
if: steps.monitor.outcome != 'success'
uses: slackapi/slack-github-action@v1.23.0
with:
payload: |
{
"text": "🔥 Slack invitation link has expired! 🔥"
}
continue-on-error: true # Allow subsequent steps (notification) even if this fails
env:
PYTHONUNBUFFERED: "1" # Recommended for immediate log output
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_MONITOR_SLACK_WEBHOOK }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
- name: Force failure
if: steps.monitor.outcome != 'success'
GITHUB_SERVER_URL: ${{ github.server_url }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_RUN_ID: ${{ github.run_id }}
run: |
exit 1
source env/bin/activate
python scripts/slack-link-monitor.py

View File

@ -11,17 +11,74 @@
import logging
import requests
from slack_sdk.webhook import WebhookClient
import os
from typing import Optional
try:
res = requests.post(
"https://linkmonitor.onrender.com/api/v1/validate",
headers={"Content-Type": "application/json"},
json={"url": "https://slack.open-metadata.org"},
)
if res.json().get("status") != "active":
raise RuntimeError("Expired status!")
except RuntimeError as err:
raise err
except Exception as err:
logging.error(f"Something went wrong fetching the data - [{err}]")
raise err
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
def get_required_env_var(var_name: str) -> str:
"""
Retrieves an environment variable by name, raising RuntimeError if it's not set.
"""
value: Optional[str] = os.getenv(var_name)
if value is None:
raise RuntimeError(f"Required environment variable '{var_name}' is not set.")
return value
# Get the variables using the helper function
slack_webhook_url: str = get_required_env_var("SLACK_WEBHOOK_URL")
slack_webhook_type: str = get_required_env_var("SLACK_WEBHOOK_TYPE")
github_server_url: str = get_required_env_var("GITHUB_SERVER_URL")
github_repository: str = get_required_env_var("GITHUB_REPOSITORY")
github_run_id: str = get_required_env_var("GITHUB_RUN_ID")
# Now you can use the variables
logger.info("Successfully loaded all required environment variables.")
logger.info(f"Repo: {github_repository}, Run ID: {github_run_id}")
def main():
slack_url_map = {
"open-metadata": "https://slack.open-metadata.org",
"free-tier-support": "https://free-tier-support.getcollate.io/",
}
for product_type, slack_url in slack_url_map.items():
res = None
try:
res = requests.post(
"https://linkmonitor.onrender.com/api/v1/validate",
headers={"Content-Type": "application/json"},
json={"url": slack_url},
)
logger.info(f"Status: {res.status_code} {res.text}")
if res.json().get("status") != "active":
raise RuntimeError("Expired status!")
except RuntimeError as err:
error_msg = f"{product_type} slack link is expired"
logger.error(error_msg)
slack_client = WebhookClient(
url=slack_webhook_url,
)
error_msg = f"🔥 {product_type} slack link is expired 🔥 \n Workflow run: {github_server_url}/{github_repository}/actions/runs/{github_run_id}"
slack_client.send(text=error_msg)
except Exception as err:
error_msg = f"Something went wrong fetching the data - [{err}]"
if res is not None:
error_msg += f" [{res.text}]"
logger.error(error_msg)
raise err
if __name__ == "__main__":
main()