Skip to content

Commit d9747bf

Browse files
added code to debug the pr details when it is unable to fetch it
1 parent 6d14567 commit d9747bf

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

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

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,39 @@ jobs:
2525
pip install pyyaml requests
2626
2727
- name: Parse context from yaml/couchbase.yaml and send Slack alert
28-
env:
29-
SLACK_WEBHOOK: ${{ secrets.DA_SLACK_WEBHOOK_URL }}
30-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31-
run: |
32-
import requests, os
33-
repo = os.environ.get('GITHUB_REPOSITORY')
34-
pr_number = os.environ.get('GITHUB_REF').split('/')[-1]
35-
token = os.environ.get('GITHUB_TOKEN')
36-
api_url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}"
37-
headers = {'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json'}
38-
pr_resp = requests.get(api_url, headers=headers)
39-
branch_name = os.environ.get('GITHUB_HEAD_REF', '')
40-
if pr_resp.status_code == 200:
41-
pr_data = pr_resp.json()
42-
pr_title = pr_data.get('title', '')
43-
pr_body = pr_data.get('body', '')
44-
pr_user = pr_data.get('user', {}).get('login', '')
45-
pr_url = pr_data.get('html_url', '')
46-
message = f"PR Alert!\nTitle: {pr_title}\nBranch: {branch_name}\nAuthor: {pr_user}\nURL: {pr_url}\nDescription: {pr_body}"
47-
else:
48-
message = f"PR Alert!\nUnable to fetch PR details."
49-
webhook = os.environ['SLACK_WEBHOOK']
50-
requests.post(webhook, json={"text": message})
51-
shell: python
28+
env:
29+
SLACK_WEBHOOK: ${{ secrets.DA_SLACK_WEBHOOK_URL }}
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
run: |
32+
import requests, os
33+
repo = os.environ.get('GITHUB_REPOSITORY')
34+
# Try to get PR number from GITHUB_REF (refs/pull/123/merge or refs/pull/123/head)
35+
ref = os.environ.get('GITHUB_REF', '')
36+
pr_number = ''
37+
if ref.startswith('refs/pull/'):
38+
pr_number = ref.split('/')[2]
39+
else:
40+
# fallback: try GITHUB_EVENT_PATH
41+
import json
42+
event_path = os.environ.get('GITHUB_EVENT_PATH')
43+
if event_path and os.path.exists(event_path):
44+
with open(event_path) as f:
45+
event = json.load(f)
46+
pr_number = str(event.get('number', ''))
47+
token = os.environ.get('GITHUB_TOKEN')
48+
api_url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}"
49+
headers = {'Authorization': f'token {token}', 'Accept': 'application/vnd.github.v3+json'}
50+
pr_resp = requests.get(api_url, headers=headers)
51+
branch_name = os.environ.get('GITHUB_HEAD_REF', '')
52+
if pr_resp.status_code == 200:
53+
pr_data = pr_resp.json()
54+
pr_title = pr_data.get('title', '')
55+
pr_body = pr_data.get('body', '')
56+
pr_user = pr_data.get('user', {}).get('login', '')
57+
pr_url = pr_data.get('html_url', '')
58+
message = f"PR Alert!\nTitle: {pr_title}\nBranch: {branch_name}\nAuthor: {pr_user}\nURL: {pr_url}\nDescription: {pr_body}"
59+
else:
60+
message = f"PR Alert!\nUnable to fetch PR details.\nAPI URL: {api_url}\nStatus: {pr_resp.status_code}\nResponse: {pr_resp.text}"
61+
webhook = os.environ['SLACK_WEBHOOK']
62+
requests.post(webhook, json={"text": message})
63+
shell: python

0 commit comments

Comments
 (0)