11======================
2- Python Github Webhooks
2+ Python GitHub Webhooks
33======================
44
5- Simple Python WSGI application to handle Github webhooks.
5+ Simple Python WSGI application to handle GitHub webhooks.
6+
7+
8+ Install
9+ =======
10+
11+ ::
12+ git clone git@github.com:carlos-jenkins/python-github-webhooks.git
13+ cd python-github-webhooks
14+
615
716Dependencies
817============
@@ -12,13 +21,33 @@ Dependencies
1221 sudo pip install -r requirements.txt
1322
1423
15- Install
16- =======
17-
18-
1924Setup
2025=====
2126
27+ You can configure what the application does by changing ``config.json ``:
28+
29+ ::
30+
31+ {
32+ "github_ips_only": false,
33+ "enforce_secret": "",
34+ "return_scripts_info": true
35+ }
36+
37+ :github_ips_only: Restrict application to be called only by GitHub IPs. IPs
38+ whitelist is obtained from
39+ `GitHub Meta <https://developer.github.com/v3/meta/ >`_
40+ (`endpoint <https://api.github.com/meta >`_).
41+ :enforce_secret: Enforce body signature with HTTP header ``X-Hub-Signature ``.
42+ See ``secret `` at
43+ `GitHub WebHooks Documentation <https://developer.github.com/v3/repos/hooks/ >`_.
44+ :return_scripts_info: Return a JSON with the ``stdout ``, ``stderr `` and exit
45+ code for each executed hook using the hook name as key.
46+
47+
48+ Adding Hooks
49+ ============
50+
2251This application will execute scripts in the hooks directory using the
2352following order:
2453
@@ -29,6 +58,62 @@ following order:
2958 hooks/{event}
3059 hooks/all
3160
61+ The application will pass to the hooks the JSON received as first argument.
62+ Hooks can be written in any scripting language as long as the file is executable
63+ and has a shebang. A simple example in Python could be:
64+
65+ ::
66+
67+ #!/usr/bin/env python
68+ # Python Example for Python GitHub Webhooks
69+
70+ import os
71+ import sys
72+ import json
73+ from tempfile import mkstemp
74+
75+ payload = json.loads(sys.argv[1])
76+ _, tmpfile = mkstemp()
77+
78+ ### Do something with the payload
79+ if payload['repository']['name'] == 'my-repo-name':
80+ f = open(tmpfile, 'w')
81+ f.write(json.dumps(payload))
82+ f.close()
83+
84+
85+ Test
86+ ====
87+
88+ The following will launch the Flask web server in debug mode at port ``5000 ``.
89+
90+ ::
91+
92+ python webhooks.py
93+
94+
95+ Deploy
96+ ======
97+
98+ To deploy in Apache, just add a ``WSGIScriptAlias `` directive to your
99+ VirtualHost file:
100+
101+ ::
102+
103+ <VirtualHost *:80>
104+ ServerAdmin you@my.site.com
105+ ServerName my.site.com
106+ DocumentRoot /var/www/site.com/my/htdocs/
107+
108+ # Handle Github webhook
109+ <Directory "/var/www/site.com/my/python-github-webhooks">
110+ Order deny,allow
111+ Allow from all
112+ </Directory>
113+ WSGIScriptAlias /webhooks /var/www/site.com/my/python-github-webhooks/webhooks.py
114+
115+ </VirtualHost>
116+
32117
33118License
34119=======
@@ -50,3 +135,13 @@ License
50135 specific language governing permissions and limitations
51136 under the License.
52137
138+
139+ Credits
140+ =======
141+
142+ This project is just the reinterpretation and merge of two approaches:
143+
144+ - `github-webhook-wrapper <https://github.com/datafolklabs/github-webhook-wrapper >`_.
145+ - `flask-github-webhook <https://github.com/razius/flask-github-webhook >`_.
146+
147+ Thanks.
0 commit comments