Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change auth to GitHub apps #163

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
install_requires=[
'singer-python==5.12.1',
'requests==2.20.0',
'backoff==1.8.0'
'backoff==1.8.0',
'PyJWT[crypto]==2.3.0'
],
extras_require={
'dev': [
Expand Down
36 changes: 35 additions & 1 deletion tap_github/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import requests
import backoff
import singer
import jwt

from singer import (bookmarks, metrics, metadata)
from simplejson import JSONDecodeError
Expand All @@ -15,7 +16,7 @@
# set default timeout of 300 seconds
REQUEST_TIMEOUT = 300

REQUIRED_CONFIG_KEYS = ['start_date', 'access_token', 'repository']
REQUIRED_CONFIG_KEYS = ['start_date', 'repository', 'github_app_id', 'github_installation_id', 'github_private_key']

KEY_PROPERTIES = {
'commits': ['sha'],
Expand Down Expand Up @@ -1172,6 +1173,36 @@ def do_sync(config, state, catalog):

singer.write_state(state)

def generate_jwt_token(github_app_id, github_private_key, expiration_time = 600, algorithm = 'RS256'):
actual_time = int(time.time())

payload = {
'iat': actual_time,
'exp': actual_time + expiration_time,
'iss': github_app_id
}

return jwt.encode(payload, github_private_key, algorithm=algorithm)

def generate_access_token(config):
jwt_token = generate_jwt_token(
config['github_app_id'],
config['github_private_key']
)

headers = {
"Authorization": "Bearer {}".format(jwt_token),
"Accept": "application/vnd.github.machine-man-preview+json"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this is still needed? I think the API change graduated. See google/go-github#1616

}

url = 'https://api.github.com/app/installations/{}/access_tokens'.format(config['github_installation_id'])
resp = requests.post(url, headers = headers)

if resp.status_code != 201:
raise_for_error(resp, url)

return resp.json()['token']

@singer.utils.handle_top_exception(logger)
def main():
args = singer.utils.parse_args(REQUIRED_CONFIG_KEYS)
Expand All @@ -1183,6 +1214,9 @@ def main():
global MAX_SLEEP_SECONDS #pylint: disable=global-statement
MAX_SLEEP_SECONDS = config_max_sleep if config_max_sleep else DEFAULT_SLEEP_SECONDS

# set access token from github app
args.config['access_token'] = generate_access_token(args.config)

if args.discover:
do_discover(args.config)
else:
Expand Down