DEV Community

Haripriya Veluchamy
Haripriya Veluchamy

Posted on • Edited on

🔄 Automating GitHub PR Notifications with Slack Integration: A Journey

Hey there, fellow developers! 👋

Today I want to share a cool automation I just built that's made my team's life so much easier. You know that feeling when you're deep in coding, and suddenly realize you missed a PR that's been sitting there for hours? Yeah, been there too many times. So I decided to fix this by setting up automatic PR notifications in our team's Slack channel.

The Problem I Was Trying to Solve 🤔

In my organization, we were constantly missing PRs or catching them late. It was becoming a real pain point - someone would create a PR and then have to manually ping the team in Slack. Not ideal, right? I wanted something that would automatically notify us as soon as a PR was created.

🚀 Step-by-Step Implementation

1. Setting Up the Development Environment

First, I encountered an issue with Python package installation:

error: externally-managed-environment
Enter fullscreen mode Exit fullscreen mode

Solution: Created a virtual environment to manage dependencies independently:

python3 -m venv venv
source venv/bin/activate
pip install flask requests python-dotenv
Enter fullscreen mode Exit fullscreen mode

2. Environment Configuration (.env)

Two crucial pieces needed:

  • SLACK_WEBHOOK_URL: For sending notifications to Slack
  • GITHUB_WEBHOOK_SECRET: For securing webhook endpoints

Getting Slack Webhook URL:

  1. Went to api.slack.com/apps
  2. Created a new app
  3. Activated Incoming Webhooks which is present under the features
  4. Added webhook to my workspace channel Image description

Creating GitHub Webhook Secret:

  1. Generated a secure random string
  2. Used this as the secret key for webhook verification

3. Moving from Development to Production

Initially, I started with ngrok for development, but for production, I've moved to a more robust setup using EC2 and Nginx. Here's how:

Setting Up EC2 Instance

  1. Created a dedicated EC2 instance
  2. Set up security groups:
   sudo ufw allow ssh
   sudo ufw allow http
   sudo ufw allow https
   sudo ufw enable
Enter fullscreen mode Exit fullscreen mode

Installing and Configuring Nginx

# Install Nginx
sudo apt update
sudo apt install nginx

# Create Nginx configuration
sudo nano /etc/nginx/sites-available/github-slack
Enter fullscreen mode Exit fullscreen mode

Nginx configuration:

server {
    listen 80;
    server_name your_ec2_public_ip;

    location / {
        proxy_pass http://localhost:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
Enter fullscreen mode Exit fullscreen mode

Enable the configuration:

sudo ln -s /etc/nginx/sites-available/github-slack /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

4. GitHub Webhook Configuration

Critical Settings:

  • Payload URL: Now using EC2 public IP (e.g., http://your-ec2-ip/webhook)
  • Content Type: application/json
  • Secret: Added the same secret from .env file

GitHub Webhook Configuration

Production Setup:

  • Using EC2 public IP or domain name
  • Configured proper security groups
  • Set up monitoring and logging

5. Making It Production-Ready

Created a systemd service for reliability:

[Unit]
Description=GitHub PR Notification Service
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/github-slack-automation
Environment="PATH=/home/ubuntu/github-slack-automation/venv/bin"
ExecStart=/home/ubuntu/github-slack-automation/venv/bin/python app.py
Restart=always

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Start and enable the service:

sudo systemctl daemon-reload
sudo systemctl start github-slack
sudo systemctl enable github-slack
Enter fullscreen mode Exit fullscreen mode

6. Security Considerations

  • Implemented webhook signature verification
  • Used environment variables for sensitive data
  • Added proper error handling and logging
  • Set up firewall rules
  • Regularly update system packages

🎓 Key Learnings

  1. Environment Management: Virtual environments are crucial for Python projects
  2. Production Deployment: Moving from ngrok to EC2/Nginx taught me about real-world deployment
  3. Security First: Always verify webhook signatures and protect sensitive data
  4. Monitoring Matters: Proper logging and monitoring are essential in production

🚧 Future Improvements I Plan For

  1. Add more detailed PR information in Slack messages
  2. Implement PR status updates
  3. Add SSL/HTTPS support using Let's Encrypt
  4. Set up automated backups
  5. Implement rate limiting

🔗 Code Repository

For the complete implementation, check out my GitHub repository: https://github.com/Haripriya2408/slack-pr-automation

📝 Conclusion

This automation project significantly improved our team's PR workflow. While setting it up had its challenges, particularly with environment management and production deployment, the end result is a reliable system that keeps our team informed about new PRs instantly.

The journey from a development setup with ngrok to a production-ready system with EC2 and Nginx taught me a lot about real-world deployment challenges and solutions.

Remember to:

  • Replace the EC2 IP with your actual production URL
  • Keep your webhook secrets secure! 🔐
  • Regularly monitor logs and update dependencies
  • Back up your configuration files

Happy coding! If you have any questions or suggestions, feel free to reach out! 👋

Debugging Tips

If you run into issues:

  • Check application logs: sudo journalctl -u github-slack -f
  • Check Nginx logs: sudo tail -f /var/log/nginx/error.log
  • Verify webhook delivery in GitHub repository settings
  • Ensure all environment variables are properly set
  • Check EC2 security group settings

Additional Resources

Top comments (0)