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 name: monitor-slack-link
on: on:
schedule: schedule:
# Run every 2 hours
- cron: '0 */2 * * *' - cron: '0 */2 * * *'
workflow_dispatch: workflow_dispatch:
# Grant least privilege permissions needed
permissions: permissions:
id-token: write contents: read # Needed for checkout and reading requirements.txt
contents: read
jobs: jobs:
monitor-slack-link: monitor-slack-link:
runs-on: ubuntu-latest 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: steps:
- name: Free Disk Space (Ubuntu) # Step 1: Checkout repository code
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
- name: Checkout - name: Checkout
uses: actions/checkout@v4 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 uses: actions/setup-python@v5
with: 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: | run: |
python -m venv env python -m venv env
source env/bin/activate 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 id: monitor
continue-on-error: true continue-on-error: true # Allow subsequent steps (notification) even if this fails
env: env:
PYTHONUNBUFFERED: "1" PYTHONUNBUFFERED: "1" # Recommended for immediate log output
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_MONITOR_SLACK_WEBHOOK }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
GITHUB_SERVER_URL: ${{ github.server_url }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_RUN_ID: ${{ github.run_id }}
run: | run: |
source env/bin/activate source env/bin/activate
python scripts/slack-link-monitor.py 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! 🔥"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_MONITOR_SLACK_WEBHOOK }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
- name: Force failure
if: steps.monitor.outcome != 'success'
run: |
exit 1

View File

@ -11,17 +11,74 @@
import logging import logging
import requests import requests
from slack_sdk.webhook import WebhookClient
import os
from typing import Optional
# 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: try:
res = requests.post( res = requests.post(
"https://linkmonitor.onrender.com/api/v1/validate", "https://linkmonitor.onrender.com/api/v1/validate",
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
json={"url": "https://slack.open-metadata.org"}, json={"url": slack_url},
) )
logger.info(f"Status: {res.status_code} {res.text}")
if res.json().get("status") != "active": if res.json().get("status") != "active":
raise RuntimeError("Expired status!") raise RuntimeError("Expired status!")
except RuntimeError as err: except RuntimeError as err:
raise 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: except Exception as err:
logging.error(f"Something went wrong fetching the data - [{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 raise err
if __name__ == "__main__":
main()