Setup GitHub commit diff emails server

From RidgeRun Developer Wiki

Overview

GitHub commit notifications hook just got deprecated. This wiki explains how to set up a third-party server to send commit notifications with nice, colored diffs included.

Dependencies

Install the following dependencies:

  • Git
  • Python
  • Mail Utils
  • Sendmail
  • RubyGems
  • Sinatra (Ruby Gem)
  • GitNotifier (Manual clone)
  • GitDub (Manual clone)

In Debian based systems, it suffices to run:

sudo apt install git rubygems python mailutils sendmail
sudo gem install sinatra

#Change this to your desired location
GIT_NOTIFIER=~/github/git-notifier
git clone git://git.icir.org/git-notifier $GIT_NOTIFIER

#Change this to your desired location
GITDUB=~/github/gitdub
git clone https://github.com/mavam/gitdub.git $GITDUB

GitHub Setup

1. Create an ssh key for GitDub to download repos. Press enter (leave blank) when prompted for a password.

SSH_KEY=~/github/gitdub/gitdub.rsa
ssh-keygen -f $SSH_KEY -C gitdub

chmod 600 $SSH_KEY
chmod 644 ${SSH_KEY}.pub

Two new files should've been created: gitdub.rsa and gitdub.rsa.pub

2. Add the gitdub.rsa.pub to a user with access to the repositories to be monitored.

3. Configure the webhook for your organization

 Organization > Settings > Webhooks > Add Webhook
  • Payload URL: YOUR.PUBLIC.IP.ADDRESS:8888/
  • Content Type: application/x-www-form-urlencoded
  • Secret: (leave empty)
  • Which event would you like to trigger this webhook?: Just the push
 Note that if the server is running in an internal machine, you might need to forward these requests from your gateway to your machine.

Starting GitDub

Start GitDub as the following:

GIT_NOTIFIER=~/github/git-notifier
GITDUB=~/github/gitdub
SSH_KEY=$GITDUB/gitdub.rsa

PATH=$PATH:$GIT_NOTIFIER GIT_SSH_COMMAND="ssh -i $SSH_KEY" $GITDUB/gitdub $GITDUB/config.yml

The following config.yml has been modified to monitor every repo in a GitHub organization. You may use it as a reference for yours.

config.yml:

gitdub:
  # The directory where gitdub keeps its per-repository state.
  ####### MODIFY #########
  directory: /home/ridgerun/github/repositories

  # Bind to all addresses by default.
  bind: 0.0.0.0

  # The TCP port to listen on.
  port: 8888

  # SSL options. Relative paths names have to be specified relative to the
  # above directory.
  ssl:
    enable: false

  # Enable debugging. This prints the contents of the HTTP POST body to STDERR.
  debug: false

  # Only process POST requests from the these IP addresses (optioanl). If empty
  # or not set, gitdub processes requests from all addresses.
  #
  # Github only.
  allowed_sources: [207.97.227.253, 50.57.128.197, 108.171.174.178]

  # Flag that determines whether the first arriving data results in sending
  # emails. If false, gitdub sends out an email for every single commit since
  # the repository creation. If true, the first arriving data only marks all
  # existing commits as emailed without actually sending a notice.
  silent_init: true

  # The interval in seconds of how often to check the file for modifications.
  # If the modification time of the configuration file is new than the time it
  # was read last, gitdub re-reads all configuration values except for bind,
  # port, and ssl.
  monitor: 60

notifier:
  # The email sender. (Can be overriden for each repository.)
  ########## MODIFY ##############
  from: github-noreply@ridgerun.com

  # The list of email receivers. (Can be overriden for each repository.)
  ########## MODIFY ##############
  to: [XXXXX@ridgerun.com]

  # The email subject prefix. (Can be overriden for each repository.)
  subject: '[git][%r]'

  # This configuration users a valid gmail account to avoid emails going to spam
  mailserver: 'smtp.gmail.com:587'

  ########### MODIFY ##############
  mailserveruser: 'XXXXXXXX@ridgerun.com'
  mailserverpassword: 'XXXXXXXX'

# The github configuration. Each entry represents either a single repository or
# all repositories for a given user/organization name.
github:
  # A basic entry specifying settings for a single repository.
  #- id: mavam/gitdub
  #  protocol: ssh # Allowed protocols: git (default), ssh, https
  #  to: [vallentin@icir.org]
  #  subject: '[git/gitdub]'
  # Optional customization of repository URI displayed in email body.
  #  uri: ssh://git.gitdub.info/real-repo.git

  # A wildcard entry that specifies settings for all repositories of a user or
  # organization.
  - id: .*/.*
    protocol: ssh