DEV Community

Haripriya Veluchamy
Haripriya Veluchamy

Posted 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. ngrok Setup and Challenges

Why ngrok?
In development, my Flask server runs locally, but GitHub needs a public URL to send webhooks. ngrok creates a secure tunnel to my local server.

Initial Challenge:

ERROR: authentication failed: Usage of ngrok requires a verified account
Enter fullscreen mode Exit fullscreen mode

Solution:

  1. Created ngrok account in https://dashboard.ngrok.com
  2. Retrieved authtoken
  3. Configured ngrok:
ngrok config add-authtoken [my-token] # copy the token from  https://dashboard.ngrok.com/get-started/your-authtoken after the signup
ngrok http 5000
Enter fullscreen mode Exit fullscreen mode

4. GitHub Webhook Configuration

Critical Settings:

  • Payload URL: Used ngrok URL (for development)
  • Content Type: Changed to application/json (initially was form-urlencoded, causing issues)
  • Secret: Added the same secret from .env file

Image description
Production vs Development:

  • Development: Used ngrok URL (e.g., https://xxxx-xx-xxx-xxx-xx.ngrok.io)
  • Production: Would use:
    • Azure App Service URL
    • AWS EC2 public IP
    • Heroku app URL
    • Or any other cloud service endpoint

5. Debugging and Fixes

Initial Errors:

  1. 405 Method Not Allowed:

    • Root cause: Webhook endpoint mismatch
    • Solution: Updated route handling in Flask app
  2. Content Type Issues:

    • Problem: GitHub sent form-urlencoded data
    • Fix: Updated content type to application/json in webhook settings

6. Security Considerations

  • Implemented webhook signature verification
  • Used environment variables for sensitive data
  • Added proper error handling and logging


Image description

🎓 Key Learnings

  1. Environment Management: Virtual environments are crucial for Python projects
  2. Webhook Development: Local testing requires a tunnel service like ngrok
  3. Content Types Matter: Proper content type configuration is essential for webhook communication
  4. Security First: Always verify webhook signatures and protect sensitive data

🚧 Future Improvements I plan for

  1. Add more detailed PR information in Slack messages
  2. Implement PR status updates

🔗 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 webhook configuration, the end result is a reliable system that keeps our team informed about new PRs instantly.

Remember to replace the ngrok URL with your production URL when moving to production, and always keep your webhook secrets secure! 🔐

Top comments (0)