forked from kalimatas/gitdiff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgitdiff.py
68 lines (59 loc) · 1.9 KB
/
gitdiff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from flask import Flask
from flask import request
from flask import abort
from flask import render_template
from lib.author import Author
from lib.commit import Commit
from lib.sender import Sender
import json
import yaml
import logging
import sys
import os
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format='"%(asctime)s %(levelname)8s %(name)s - %(message)s"',
datefmt='%H:%M:%S'
)
def parse_request(payload):
"""Parses POST requst from Github and
returns list of Commit objects.
"""
payload = json.loads(payload, encoding='utf-8')
commits = []
for commitInfo in payload['commits']:
author = Author(name=str(commitInfo['author']['name']),
email=str(commitInfo['author']['email']))
commit = Commit(commitId=str(commitInfo['id']),
author=author,
message=str(commitInfo['message']),
date=str(commitInfo['timestamp']),
url=str(commitInfo['url']))
commits.append(commit)
return commits
app = Flask(__name__)
@app.route('/', methods=['POST'])
def process_github_push():
try:
if not 'payload' in request.form:
raise ValueError
commitList = parse_request(request.form['payload'])
sender = Sender(commitList)
template = open('templates/layout.html').read() % sender.body
sender.send(template)
return 'Ok'
except Exception, e:
logging.error(e.message)
abort(500)
@app.errorhandler(404)
def not_found(error):
return render_template('404.html'), 404
@app.errorhandler(500)
def not_found(error):
return render_template('500.html'), 500
if __name__ == '__main__':
config = yaml.load(open('config.yaml'))
web_config = config.get('web')
port = int(os.environ.get('PORT', int(web_config['port'])))
app.run(host=web_config['host'], port=port)