Skip to content

Latest commit

聽

History

History
112 lines (81 loc) 路 3.05 KB

README_github_bot.md

File metadata and controls

112 lines (81 loc) 路 3.05 KB

GitHub Commit Monitor Bot

A Python bot that monitors a GitHub repository for new commits and sends notifications to a Slack channel.

Features

  • 馃攳 Monitors GitHub repository for new commits
  • 馃摙 Sends formatted notifications to Slack
  • 馃捑 Tracks last seen commit to avoid duplicate notifications
  • 鈿欙笍 Configurable check interval
  • 馃摑 Detailed logging

Setup

1. Install Dependencies

pip install -r requirements.txt

2. Configure the Bot

Edit bot_config.json with your settings:

{
  "github_repo": "owner/repo-name",
  "github_token": "",
  "slack_webhook_url": "https://hooks.slack.com/services/YOUR/WEBHOOK/URL",
  "check_interval": 60,
  "state_file": "last_commit_state.json"
}

Configuration Options:

  • github_repo: GitHub repository in format owner/repo-name (e.g., octocat/Hello-World)
  • github_token: (Optional) GitHub personal access token for higher rate limits. Get one at https://github.com/settings/tokens
  • slack_webhook_url: Slack webhook URL for your channel. See Slack Webhook Setup below
  • check_interval: How often to check for new commits (in seconds). Default: 60
  • state_file: File to store the last seen commit SHA. Default: last_commit_state.json

3. Slack Webhook Setup

  1. Go to https://api.slack.com/apps
  2. Create a new app or select an existing one
  3. Go to "Incoming Webhooks" and activate it
  4. Click "Add New Webhook to Workspace"
  5. Select the channel where you want notifications
  6. Copy the webhook URL and paste it into bot_config.json

4. Run the Bot

python3 github_commit_bot.py

Or specify a custom config file:

python3 github_commit_bot.py my_config.json

How It Works

  1. The bot polls the GitHub API at the specified interval
  2. It compares the latest commit SHA with the last known commit
  3. If a new commit is detected, it sends a formatted message to Slack
  4. The bot saves the new commit SHA to avoid duplicate notifications

Example Slack Message

The bot sends formatted messages with:

  • Repository name
  • Commit SHA (short)
  • Author name
  • Commit date
  • Commit message
  • Link to view the commit on GitHub

Logging

The bot logs to both:

  • Console (stdout)
  • File: github_bot.log

Stopping the Bot

Press Ctrl+C to gracefully stop the bot.

Notes

  • Without a GitHub token, you're limited to 60 requests/hour per IP
  • With a GitHub token, you get 5,000 requests/hour
  • The bot creates a state file to track the last seen commit
  • On first run, the bot initializes with the latest commit (no notification sent)

Troubleshooting

No notifications received:

  • Check that your Slack webhook URL is correct
  • Verify the GitHub repository name is in the correct format
  • Check the logs in github_bot.log

Rate limit errors:

  • Add a GitHub personal access token to increase rate limits
  • Increase the check_interval to poll less frequently

Bot not detecting commits:

  • Verify the repository name is correct
  • Check that the repository is public or your token has access
  • Review the logs for API errors