Skip to content

Instantly share code, notes, and snippets.

@adimania
Created October 15, 2015 18:10
Show Gist options
  • Save adimania/522166fda638ccb59f49 to your computer and use it in GitHub Desktop.
Save adimania/522166fda638ccb59f49 to your computer and use it in GitHub Desktop.
Detect push in default branch in Github
from flask import Flask, request
import smtplib
from email.mime.text import MIMEText
import json
import ConfigParser
import os.path
app = Flask(__name__)
app.debug=True
config = ConfigParser.ConfigParser()
config.read(os.path.expanduser('~/.github-consumer'))
owners = json.loads(config.get("config", "owners", raw=True))
def send_mail(user,message):
s = smtplib.SMTP('localhost')
message=MIMEText(message,"html")
message['Subject']='Push to master detected'
s.sendmail('github.alert@adi.im',user+"@example.com",message.as_string())
s.quit()
@app.route('/', methods=['POST'])
def api_consumer():
if request.method == 'POST':
# try:
data = json.loads(request.get_data())
try:
branch = data['ref'].split('/')[2]
default_branch = data['repository']['default_branch']
head_committer = data['head_commit']['committer']['name']
commit_msg = data['head_commit']['message']
repo = data['repository']['name']
print '**** New Commit ****'
print head_committer
print branch
print repo
print owners[repo]
if commit_msg[:18] != 'Merge pull request' and branch == default_branch :
message = "%s committed on %s. URL: %s" %(head_committer, default_branch, data['head_commit']['url'])
print message
send_mail(owners[repo], message)
except Exception as e:
print e
print data
return "The Github dude!"
if __name__ == "__main__":
app.run(host='0.0.0.0',port=8081)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment