Skip to content

PR and Issue Activity Reminder via Slack #4

PR and Issue Activity Reminder via Slack

PR and Issue Activity Reminder via Slack #4

name: PR and Issue Activity Reminder via Slack
# on:
# schedule:
# - cron: '0 10 */2 * *' # Runs at 10:00 AM UTC every 2 days
# workflow_dispatch:
on:
schedule:
- cron: '*/5 * * * *' # Runs every 5 minutes
workflow_dispatch:
jobs:
send-reminders:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Debug Environment
run: |
echo "Current directory: $(pwd)"
echo "Directory contents:"
ls -la
echo "Python version:"
python --version
echo "Pip version:"
pip --version
echo "System information:"
uname -a
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install PyGithub requests
pip list
- name: Send PR and issue activity reminders via Slack
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ORG_NAME: ${{ secrets.ORG_NAME }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
#!/usr/bin/env python
import os
import json
import requests
from github import Github
print("Script started")
print(f"Current working directory: {os.getcwd()}")
print(f"Directory contents: {os.listdir()}")
def get_org_members(org_name, github_token):
print(f"Getting members for org: {org_name}")
g = Github(github_token)
org = g.get_organization(org_name)
members = list(org.get_members())
print(f"Found {len(members)} members")
return [member.login for member in members]
def get_pr_issue_activity(username, github_token):
print(f"Getting activity for user: {username}")
g = Github(github_token)
user = g.get_user(username)
pr_issue_activity = []
for notification in user.get_notifications(participating=True):
if notification.subject.type in ['PullRequest', 'Issue']:
pr_issue_activity.append({
'type': notification.subject.type,
'title': notification.subject.title,
'url': notification.subject.url.replace('api.github.com/repos', 'github.com')
})
print(f"Found {len(pr_issue_activity)} activities for {username}")
return pr_issue_activity
def send_slack_reminder(username, activities, webhook_url):
print(f"Sending Slack reminder for {username}")
if not activities:
print("No activities to send")
return
message = f"*Reminder for @{username}*\n"
message += f"You have {len(activities)} unread PR/issue notifications:\n"
for activity in activities:
message += f"• {activity['type']}: <{activity['url']}|{activity['title']}>\n"
payload = {
"text": message,
"username": "GitHub Notification Bot",
"icon_emoji": ":github:"
}
response = requests.post(webhook_url, json=payload)
print(f"Slack API response status code: {response.status_code}")
print(f"Slack API response content: {response.text}")
if response.status_code != 200:
print(f"Failed to send Slack message for {username}. Status code: {response.status_code}")
print("Getting environment variables")
github_token = os.environ['GITHUB_TOKEN']
org_name = os.environ['ORG_NAME']
slack_webhook_url = os.environ['SLACK_WEBHOOK_URL']
print("Starting main logic")
org_members = get_org_members(org_name, github_token)
for member in org_members:
activities = get_pr_issue_activity(member, github_token)
if activities:
send_slack_reminder(member, activities, slack_webhook_url)
print("Script execution completed.")