Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 6556187

Browse files
author
mikespub
committed
update to python 3.7 + add tests
1 parent 759b67e commit 6556187

6 files changed

Lines changed: 176 additions & 14 deletions

File tree

.gitignore

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
.hypothesis/
51+
.pytest_cache/
52+
53+
# Translations
54+
*.mo
55+
*.pot
56+
57+
# Django stuff:
58+
*.log
59+
local_settings.py
60+
db.sqlite3
61+
62+
# Flask stuff:
63+
instance/
64+
.webassets-cache
65+
66+
# Scrapy stuff:
67+
.scrapy
68+
69+
# Sphinx documentation
70+
docs/_build/
71+
72+
# PyBuilder
73+
target/
74+
75+
# Jupyter Notebook
76+
.ipynb_checkpoints
77+
78+
# IPython
79+
profile_default/
80+
ipython_config.py
81+
82+
# pyenv
83+
.python-version
84+
85+
# celery beat schedule file
86+
celerybeat-schedule
87+
88+
# SageMath parsed files
89+
*.sage.py
90+
91+
# Environments
92+
.env
93+
.venv
94+
env/
95+
venv/
96+
ENV/
97+
env.bak/
98+
venv.bak/
99+
100+
# Spyder project settings
101+
.spyderproject
102+
.spyproject
103+
104+
# Rope project settings
105+
.ropeproject
106+
107+
# mkdocs documentation
108+
/site
109+
110+
# mypy
111+
.mypy_cache/
112+
.dmypy.json
113+
dmypy.json
114+
115+
# Pyre type checker
116+
.pyre/

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FROM python:2.7-alpine
2-
MAINTAINER "Matjaž Finžgar" <matjaz@finzgar.net>
1+
FROM python:3.7-alpine
2+
#FROM python:3.7-slim
33

44
WORKDIR /app
55

@@ -9,4 +9,4 @@ RUN pip install -r requirements.txt
99
COPY . /app
1010

1111
EXPOSE 5000
12-
CMD ["python", "webhooks.py"]
12+
CMD ["python3", "webhooks.py"]

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ License
230230
Credits
231231
=======
232232

233-
This project is just the reinterpretation and merge of two approaches:
233+
This project is just the reinterpretation and merge of two approaches and a modification of Carlos Jenkins' work:
234234

235235
- `github-webhook-wrapper <https://github.com/datafolklabs/github-webhook-wrapper>`_.
236236
- `flask-github-webhook <https://github.com/razius/flask-github-webhook>`_.
237+
- `python-github-webhooks <https://github.com/carlos-jenkins/python-github-webhooks>`_.
237238

238239
Thanks.

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Flask==0.12.2
2-
ipaddress==1.0.18
3-
requests==2.18.2
1+
Flask==1.0.2
2+
ipaddress==1.0.22
3+
requests==2.21.0

tests/test_webhooks.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
import os
3+
import sys
4+
# import pytest
5+
6+
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
7+
from webhooks import application as app
8+
9+
10+
class WebhooksTest(unittest.TestCase):
11+
12+
def setUp(self):
13+
app.config['TESTING'] = True
14+
self.app = app.test_client()
15+
self.output = False
16+
17+
def show_response(self, response):
18+
if not self.output:
19+
return
20+
print('Status Code: %s' % response.status_code)
21+
print(response.headers)
22+
print(response.data)
23+
24+
def test_unit_get(self):
25+
response = self.app.get('/')
26+
self.show_response(response)
27+
self.assertEqual(response.status_code, 405)
28+
29+
def test_unit_post(self):
30+
response = self.app.post('/')
31+
self.show_response(response)
32+
self.assertEqual(response.status_code, 200)
33+
self.assertEqual(response.data, b'{"msg": "pong"}')
34+
35+
def test_unit_notfound(self):
36+
response = self.app.get('/notfound')
37+
self.show_response(response)
38+
self.assertEqual(response.status_code, 404)

webhooks.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
1817
import logging
1918
from sys import stderr, hexversion
20-
logging.basicConfig(stream=stderr)
2119

2220
import hmac
23-
from hashlib import sha1
2421
from json import loads, dumps
2522
from subprocess import Popen, PIPE
2623
from tempfile import mkstemp
@@ -31,6 +28,7 @@
3128
from ipaddress import ip_address, ip_network
3229
from flask import Flask, request, abort
3330

31+
logging.basicConfig(stream=stderr)
3432

3533
application = Flask(__name__)
3634

@@ -43,13 +41,22 @@ def index():
4341

4442
path = normpath(abspath(dirname(__file__)))
4543

46-
# Only POST is implemented
44+
# Only POST is implemented - same effect as removing 'GET' in methods above
4745
if request.method != 'POST':
48-
abort(501)
46+
abort(405)
4947

5048
# Load config
51-
with open(join(path, 'config.json'), 'r') as cfg:
52-
config = loads(cfg.read())
49+
if isfile(join(path, 'config.json')):
50+
with open(join(path, 'config.json'), 'r') as cfg:
51+
config = loads(cfg.read())
52+
else:
53+
# abort(503, 'Configuration file config.json is missing.')
54+
config = {
55+
"github_ips_only": False,
56+
"enforce_secret": "",
57+
"return_scripts_info": False,
58+
"hooks_path": "/missing"
59+
}
5360

5461
hooks = config.get('hooks_path', join(path, 'hooks'))
5562

0 commit comments

Comments
 (0)