如何配置Git Hook实现Pull Request评论通知及Slack PR集成?
Awesome question! Let's tackle this—first, a quick heads-up: regular local Git hooks can't monitor comments on remote Pull Requests (since PRs are a feature of platforms like GitHub/GitLab/Bitbucket, not core Git itself). But don't worry, there are straightforward ways to get instant notifications, plus we'll cover Slack PR integration too.
Option 1: Use Your Code Platform's Webhooks
Most code hosting platforms let you set up webhooks to trigger actions when a PR gets a comment. Here's how it works for GitHub (GitLab/Bitbucket follow similar logic):
- Go to your repo’s Settings > Webhooks > Add webhook
- Enter a Payload URL (this can be a server you control, or a no-code service that forwards notifications)
- Set the Content type to
application/json - Under "Which events would you like to trigger this webhook?", select Issue comments (PRs are treated as issues under the hood for comment events)
- Save the webhook, then build a small script or use a tool to send notifications (email, Slack, etc.) when the webhook receives a comment event.
Option 2: Use Platform Automation (e.g., GitHub Actions)
For flexibility without running your own server, use built-in automation tools. Here’s a GitHub Action example that sends a Slack notification when a PR gets a comment:
name: PR Comment Alert on: issue_comment: types: [created] # Only trigger for PR comments (not regular issues) if: github.event.issue.pull_request jobs: send-slack-alert: runs-on: ubuntu-latest steps: - name: Notify Slack channel uses: slackapi/slack-github-action@v1 with: payload: | { "text": "🚨 New comment on PR #${{ github.event.issue.number }}!\nAuthor: ${{ github.event.comment.user.login }}\nComment: ${{ github.event.comment.body }}\nLink: ${{ github.event.comment.html_url }}" } env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
Add this to .github/workflows/pr-comment-alert.yml in your repo, then add your Slack webhook URL as a repo secret to get it working.
Option 3: Platform Built-In Notifications
If you just want basic alerts without extra setup, check your platform’s notification settings. For GitHub:
- Go to Settings > Notifications
- Under "Pull requests", ensure "Comments on your pull requests" is enabled
- You can choose emails, browser notifications, or even SMS alerts (depending on your plan)
1. Official Platform Slack Apps
The easiest way is to use the official Slack app for your code platform:
- GitHub for Slack: Install it from the Slack Marketplace, link your GitHub account, then use commands like
/github subscribe your-org/your-repo commentsto get PR comment alerts in a channel. You can also set up notifications for PR creation, reviews, merges, etc. - GitLab for Slack: Similar workflow—install the app, connect your GitLab instance, and configure which events get sent to your channels.
2. Custom Webhooks
For full control over notification content, use Slack's Incoming Webhooks:
- Create an incoming webhook in Slack (Settings > Manage apps > Incoming Webhooks) and copy the URL
- In your code platform’s webhook settings, set the payload URL to your Slack webhook URL
- Customize the JSON payload to send exactly what you want (e.g., PR number, comment author, direct link)
3. No-Code Tools (Zapier/Make)
If you don’t want to write code, use tools like Zapier or Make to connect your code platform and Slack:
- Create a zap that triggers when a PR comment is added
- Configure the action to send a message to your desired Slack channel
- Customize the message content with details like PR link, comment text, and author
内容的提问来源于stack exchange,提问作者olafsadventures

