Skip to content

Commit 23715ff

Browse files
Sending alerts on devex_alerts channel (#48)
* added support for alerts on slack channel for PRs * added permissions for read-only to the alert-workflow * Fixed the alert description * added code to debug the pr details when it is unable to fetch it * fixed bugs and removed unnecessary code * added devex alerts secret * removed unnecessary code
1 parent 0e7da6c commit 23715ff

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

.github/workflows/alert-on-pr.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Alert on PR Changes
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
alert:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: '3.11'
21+
22+
- name: Install requests
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install requests
26+
27+
- name: Send Slack alert on PR changes
28+
env:
29+
SLACK_WEBHOOK: ${{ secrets.DEVEX_ALERTS_SECRET }}
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
PR_NUMBER: ${{ github.event.pull_request.number }}
32+
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')
37+
api_url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}"
38+
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}"
48+
else:
49+
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

0 commit comments

Comments
 (0)