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
Solution: Created a virtual environment to manage dependencies independently:
python3 -m venv venv
source venv/bin/activate
pip install flask requests python-dotenv
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:
- Went to api.slack.com/apps
- Created a new app
- Activated Incoming Webhooks which is present under the features
- Added webhook to my workspace channel
Creating GitHub Webhook Secret:
- Generated a secure random string
- 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
Solution:
- Created ngrok account in https://dashboard.ngrok.com
- Retrieved authtoken
- 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
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
- 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:
-
405 Method Not Allowed:
- Root cause: Webhook endpoint mismatch
- Solution: Updated route handling in Flask app
-
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
🎓 Key Learnings
- Environment Management: Virtual environments are crucial for Python projects
- Webhook Development: Local testing requires a tunnel service like ngrok
- Content Types Matter: Proper content type configuration is essential for webhook communication
- Security First: Always verify webhook signatures and protect sensitive data
🚧 Future Improvements I plan for
- Add more detailed PR information in Slack messages
- 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)