@@ -3,6 +3,8 @@ name: Alert on PR Changes
33on :
44 pull_request :
55 types : [opened, synchronize, reopened]
6+ pull_request_target :
7+ types : [opened, synchronize, reopened]
68
79permissions :
810 contents : read
1113 alert :
1214 runs-on : ubuntu-latest
1315 steps :
14- - name : Checkout code
15- uses : actions/checkout@v4
16-
1716 - name : Set up Python
1817 uses : actions/setup-python@v5
1918 with :
@@ -29,24 +28,33 @@ jobs:
2928 SLACK_WEBHOOK : ${{ secrets.DEVEX_ALERTS_SECRET }}
3029 GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
3130 PR_NUMBER : ${{ github.event.pull_request.number }}
31+ REPO : ${{ github.repository }}
32+ shell : python
3233 run : |
33- import requests, os
34- repo = os.environ.get('GITHUB_REPOSITORY')
35- pr_number = os.environ.get( 'PR_NUMBER')
36- token = os.environ.get( 'GITHUB_TOKEN')
34+ import os, requests
35+ repo = os.environ['REPO']
36+ pr_number = os.environ[ 'PR_NUMBER']
37+ token = os.environ[ 'GITHUB_TOKEN']
3738 api_url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}"
3839 headers = {'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json'}
39- pr_resp = requests.get(api_url, headers=headers)
40- branch_name = os.environ.get('GITHUB_HEAD_REF', '')
41- if pr_resp.status_code == 200:
42- pr_data = pr_resp.json()
43- pr_title = pr_data.get('title', '')
44- pr_body = pr_data.get('body', '')
45- pr_user = pr_data.get('user', {}).get('login', '')
46- pr_url = pr_data.get('html_url', '')
47- message = f"PR Alert!\nTitle: {pr_title}\nBranch: {branch_name}\nAuthor: {pr_user}\nURL: {pr_url}\nDescription: {pr_body}"
40+ pr_resp = requests.get(api_url, headers=headers, timeout=15)
41+ if pr_resp.ok:
42+ pr = pr_resp.json()
43+ branch_name = pr.get('head', {}).get('ref') or os.environ.get('GITHUB_HEAD_REF', '')
44+ pr_title = pr.get('title', '')
45+ pr_body = pr.get('body', '')
46+ pr_user = pr.get('user', {}).get('login', '')
47+ pr_url = pr.get('html_url', '')
48+ prefix = "PR Alert from Forked Repo!" if pr.get('head', {}).get('repo', {}).get('fork') else "PR Alert!"
49+ message = f"{prefix}\nTitle: {pr_title}\nBranch: {branch_name}\nAuthor: {pr_user}\nURL: {pr_url}\nDescription: {pr_body}"
4850 else:
4951 message = f"PR Alert!\nUnable to fetch PR details.\nStatus Code: {pr_resp.status_code}\nResponse: {pr_resp.text}"
50- webhook = os.environ['SLACK_WEBHOOK']
51- requests.post(webhook, json={"text": message})
52- shell : python
52+ webhook = os.environ.get('SLACK_WEBHOOK', '').strip()
53+ if webhook and webhook.startswith(("http://", "https://")):
54+ try:
55+ resp = requests.post(webhook, json={"text": message}, timeout=15)
56+ resp.raise_for_status()
57+ except Exception as e:
58+ print(f"Slack notification failed: {e}")
59+ else:
60+ print("SLACK_WEBHOOK missing or invalid; skipping Slack notification.")
0 commit comments