Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Mac files
.DS_Store

# Python artifacts
__pycache__

# Application logs
events_log
pyghee.log
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ It takes care of:
`PyGHee` depends on a couple of Python libraries:

* [Flask](https://pypi.org/project/Flask), a simple framework for building complex web applications;
* [PyGithub](https://pypi.org/project/PyGithub), a Python library to access the [GitHub REST API](https://docs.github.com/en/rest);
* [requests](https://pypi.org/project/requests/), a simple HTTP library that provides a CaseInsensitiveDict;
* [waitress](https://pypi.org/project/waitress), a production-quality pure-Python [WSGI](https://www.python.org/dev/peps/pep-3333) server;

For more specific information, like required versions, see [requirements.txt](requirements.txt).

In addition:
* a [GitHub Personal Access Token (PAT)](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) must be available via the `$GITHUB_TOKEN` environment variable;
* the [GitHub app secret token](https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks) must be available via the `$GITHUB_APP_SECRET_TOKEN` environment variable;

## Installation
Expand Down Expand Up @@ -75,10 +74,10 @@ The `PyGHee` log file is named `pyghee.log` is located in the directory where th

Event data is logged in JSON format in a directory named `events_log` that is located in the directory where the GitHub App is started.

The logs are organised hierarchically, by *event type*, *event action*, *date* (in that order).
The logs are organized hierarchically, by *event type*, *event action*, *date* (in that order).

For each incoming event, two JSON files are created, one for:
* the request headers including high-level information like the timestamp on which the event occured, etc.
* the request headers including high-level information like the timestamp on which the event occurred, etc.
* the request body including the actual event information (which depends on the event type).

Here's an example of a single event that got logged: an issue commented that was created on 20 Feb 2022 at 14:23:27:
Expand Down Expand Up @@ -122,9 +121,8 @@ if __name__ == '__main__':

To run your GitHub App:

* Define environment variables for [GitHub Personal Access Token (PAT)](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) and [GitHub app secret token](https://docs.github.com/en/developers/webhooks-and-events/securing-your-webhooks):
* Define an environment variable for the [GitHub app secret token](https://docs.github.com/en/developers/webhooks-and-events/securing-your-webhooks):
```
export GITHUB_TOKEN=...
export GITHUB_APP_SECRET_TOKEN=...
```

Expand Down
16 changes: 5 additions & 11 deletions pyghee/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import datetime
import flask
import hmac
import github
import json
import os
import threading
Expand Down Expand Up @@ -42,7 +41,7 @@ def get_event_info(request):
'raw_request_headers': dict(request.headers),
})

timestamp = datetime.datetime.utcfromtimestamp(int(event_info['timestamp_raw'])/1000.)
timestamp = datetime.datetime.fromtimestamp(int(event_info['timestamp_raw'])/1000., tz=datetime.UTC)
event_info['timestamp'] = timestamp
event_info['date'] = timestamp.isoformat().split('T')[0]
event_info['time'] = timestamp.isoformat().split('T')[1].split('.')[0].replace(':', '-')
Expand Down Expand Up @@ -70,14 +69,6 @@ def __init__(self, *args, **kwargs):
"""
super(PyGHee, self).__init__('PyGHee', *args, **kwargs)

github_token = os.getenv('GITHUB_TOKEN')
if github_token is None:
error("GitHub token is not available via $GITHUB_TOKEN!")
else:
del os.environ['GITHUB_TOKEN']

self.gh = github.Github(github_token)

# see https://docs.github.com/en/developers/webhooks-and-events/securing-your-webhooks
self.github_app_secret_token = os.getenv('GITHUB_APP_SECRET_TOKEN')
if self.github_app_secret_token is None:
Expand Down Expand Up @@ -172,7 +163,7 @@ def verify_request(self, event_info, abort_function, log_file=None):
abort_function(403)
else:
# we only know how to verify a SHA1 signature
log_warning("Uknown type of signature (%s) => 501" % signature_type, log_file=log_file)
log_warning("Unknown type of signature (%s) => 501" % signature_type, log_file=log_file)
abort_function(501)
else:
log_warning("Type of signature not specified (%s) => 501" % header_signature, log_file=log_file)
Expand Down Expand Up @@ -201,6 +192,9 @@ def process_event(self, request, abort_function,
tb_txt = ''.join(traceback.format_exception(None, err, err.__traceback__))
log_warning("A crash occurred!\n" + tb_txt, log_file=log_file)

@property
def gh(self):
raise Exception("The gh property and PyGithub dependency have been removed from PyGHee. To create and authenticate a github.GitHub object, follow PyGithub's documentation here: https://pygithub.readthedocs.io/en/stable/examples/Authentication.html")

def create_app(klass=None):
"""
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask<3,>=2.0.3
PyGithub<2,>=1.55
requests>=2.14.0
waitress<3,>=2.0.0
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
version="0.1.0",
author="Kenneth Hoste",
author_email="kenneth.hoste@ugent.be",
description="PyGHee (pronounced as 'piggy') is the GitHub Event Executor, a Python library to facilitate creating a GitHub App implemented in Python to process [events from GitHub",
description="PyGHee (pronounced as 'piggy') is the GitHub Event Executor, a Python library to facilitate creating a GitHub App implemented in Python to process events from GitHub",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/boegel/pyghee",
Expand All @@ -24,7 +24,7 @@
python_requires=">=3.6",
install_requires=[
"Flask",
"PyGithub",
"requests",
"waitress",
],
)
2 changes: 0 additions & 2 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def test_process_event(tmpdir):
events_log_dir = os.path.join(tmpdir, 'events_log_dir')
log_file = os.path.join(tmpdir, 'pyghee.log')

os.environ['GITHUB_TOKEN'] = 'fake_token'
os.environ['GITHUB_APP_SECRET_TOKEN'] = 'fake_app_secret_token'
pyghee = ExamplePyGHee()

Expand Down Expand Up @@ -121,7 +120,6 @@ def test_verify_request(tmpdir):
"""
log_file = os.path.join(tmpdir, 'pyghee.log')

os.environ['GITHUB_TOKEN'] = 'fake_token'
os.environ['GITHUB_APP_SECRET_TOKEN'] = TEST_SECRET_TOKEN

pyghee = ExamplePyGHee()
Expand Down