mirror of
				https://github.com/open-metadata/OpenMetadata.git
				synced 2025-11-04 04:29:13 +00:00 
			
		
		
		
	MINOR: Enhance Slack Link Monitoring Workflow and Script (#20604)
This commit is contained in:
		
							parent
							
								
									e725807bfd
								
							
						
					
					
						commit
						1f81025f54
					
				
							
								
								
									
										63
									
								
								.github/workflows/monitor-slack-link.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										63
									
								
								.github/workflows/monitor-slack-link.yml
									
									
									
									
										vendored
									
									
								
							@ -12,64 +12,51 @@
 | 
				
			|||||||
name: monitor-slack-link
 | 
					name: monitor-slack-link
 | 
				
			||||||
on:
 | 
					on:
 | 
				
			||||||
  schedule:
 | 
					  schedule:
 | 
				
			||||||
    - cron:  '0 */2 * * *'
 | 
					    # Run every 2 hours
 | 
				
			||||||
 | 
					    - 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:
 | 
					 | 
				
			||||||
        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! 🔥"
 | 
					 | 
				
			||||||
          }
 | 
					 | 
				
			||||||
      env:
 | 
					      env:
 | 
				
			||||||
 | 
					        PYTHONUNBUFFERED: "1" # Recommended for immediate log output
 | 
				
			||||||
        SLACK_WEBHOOK_URL: ${{ secrets.SLACK_MONITOR_SLACK_WEBHOOK }}
 | 
					        SLACK_WEBHOOK_URL: ${{ secrets.SLACK_MONITOR_SLACK_WEBHOOK }}
 | 
				
			||||||
        SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
 | 
					        SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
 | 
				
			||||||
 | 
					        GITHUB_SERVER_URL: ${{ github.server_url }}
 | 
				
			||||||
    - name: Force failure
 | 
					        GITHUB_REPOSITORY: ${{ github.repository }}
 | 
				
			||||||
      if: steps.monitor.outcome != 'success'
 | 
					        GITHUB_RUN_ID: ${{ github.run_id }}
 | 
				
			||||||
      run: |
 | 
					      run: |
 | 
				
			||||||
        exit 1
 | 
					        source env/bin/activate
 | 
				
			||||||
 | 
					        python scripts/slack-link-monitor.py
 | 
				
			||||||
@ -11,17 +11,74 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import requests
 | 
					import requests
 | 
				
			||||||
 | 
					from slack_sdk.webhook import WebhookClient
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					from typing import Optional
 | 
				
			||||||
 | 
					
 | 
				
			||||||
try:
 | 
					
 | 
				
			||||||
    res = requests.post(
 | 
					# Configure logging
 | 
				
			||||||
        "https://linkmonitor.onrender.com/api/v1/validate",
 | 
					logging.basicConfig(
 | 
				
			||||||
        headers={"Content-Type": "application/json"},
 | 
					    level=logging.INFO,
 | 
				
			||||||
        json={"url": "https://slack.open-metadata.org"},
 | 
					    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
 | 
				
			||||||
    )
 | 
					)
 | 
				
			||||||
    if res.json().get("status") != "active":
 | 
					logger = logging.getLogger(__name__)
 | 
				
			||||||
        raise RuntimeError("Expired status!")
 | 
					
 | 
				
			||||||
except RuntimeError as err:
 | 
					
 | 
				
			||||||
    raise err
 | 
					def get_required_env_var(var_name: str) -> str:
 | 
				
			||||||
except Exception as err:
 | 
					    """
 | 
				
			||||||
    logging.error(f"Something went wrong fetching the data - [{err}]")
 | 
					    Retrieves an environment variable by name, raising RuntimeError if it's not set.
 | 
				
			||||||
    raise err
 | 
					    """
 | 
				
			||||||
 | 
					    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()
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user