Skip to content

Commit

Permalink
Enable github private repos.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoppert committed Jun 17, 2017
1 parent 9ea33a7 commit 60506cc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ MANIFEST
*.DS_Store
deb_dist
.fuse_*
*.swp
8 changes: 3 additions & 5 deletions bloom/commands/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

import argparse
import atexit
import base64
import datetime
import difflib
import getpass
Expand Down Expand Up @@ -73,8 +72,6 @@
from bloom.git import inbranch
from bloom.git import ls_tree

from bloom.github import auth_header_from_basic_auth
from bloom.github import auth_header_from_oauth_token
from bloom.github import Github
from bloom.github import GithubException

Expand Down Expand Up @@ -104,6 +101,7 @@
from bloom.util import safe_input
from bloom.util import temporary_directory
from bloom.util import to_unicode
from bloom.util import auth_header

try:
import vcstools
Expand Down Expand Up @@ -651,7 +649,7 @@ def get_github_interface(quiet=False):
token = config.get('oauth_token', None)
username = config.get('github_user', None)
if token and username:
return Github(username, auth=auth_header_from_oauth_token(token), token=token)
return Github(username, auth=auth_header(token=token), token=token)
if not os.path.isdir(os.path.dirname(oauth_config_path)):
os.makedirs(os.path.dirname(oauth_config_path))
if quiet:
Expand Down Expand Up @@ -679,7 +677,7 @@ def get_github_interface(quiet=False):
if not password:
error("No password was given, aborting.")
return None
gh = Github(username, auth=auth_header_from_basic_auth(username, password))
gh = Github(username, auth=auth_header(username=username, password=password))
try:
token = gh.create_new_bloom_authorization(update_auth=True)
with open(oauth_config_path, 'a') as f:
Expand Down
16 changes: 7 additions & 9 deletions bloom/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import datetime
import json
import socket
import os

from urlparse import urlunsplit
from urllib import urlencode
Expand All @@ -57,14 +58,7 @@
from urllib.request import Request, urlopen

import bloom


def auth_header_from_basic_auth(user, password):
return "Basic {0}".format(base64.b64encode('{0}:{1}'.format(user, password)))


def auth_header_from_oauth_token(token):
return "token " + token
from bloom.util import auth_header


def get_bloom_headers(auth=None):
Expand All @@ -88,6 +82,10 @@ def do_github_post_req(path, data=None, auth=None, site='api.github.com'):
else:
request = Request(url, data=json.dumps(data), headers=headers) # POST

if GITHUB_USER and GITHUB_PASSWORD:
authheader = 'Basic %s' % base64.b64encode(b'%s:%s' % (GITHUB_USER, GITHUB_PASSWORD))
request.add_header('Authorization', authheader)

try:
response = urlopen(request, timeout=120)
except HTTPError as e:
Expand Down Expand Up @@ -131,7 +129,7 @@ def create_new_bloom_authorization(self, note=None, note_url=None, scopes=None,
raise GithubException("Failed to create a new oauth authorization", resp)
token = resp_data['token']
if update_auth:
self.auth = auth_header_from_oauth_token(token)
self.auth = auth_header(token=token)
self.token = token
return token

Expand Down
28 changes: 25 additions & 3 deletions bloom/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import argparse
import os
import base64
import shutil
import socket
import sys
Expand All @@ -48,12 +49,12 @@
# Python2
from urllib2 import HTTPError
from urllib2 import URLError
from urllib2 import urlopen
from urllib2 import urlopen, Request
except ImportError:
# Python3
from urllib.error import HTTPError
from urllib.error import URLError
from urllib.request import urlopen
from urllib.request import urlopen, Request

from email.utils import formatdate

Expand All @@ -78,6 +79,23 @@
from bloom.logging import sanitize
from bloom.logging import warning


GITHUB_USER = os.getenv('GITHUB_USER', None)
GITHUB_PASSWORD = os.getenv('GITHUB_PASSWORD', None)


def auth_header(username=None, password=None, token=None):
if username and password:
if sys.version_info > (3, 0):
userandpass = base64.b64encode(bytes('%s:%s' % (username, password), 'utf-8'))
else:
userandpass = base64.b64encode('%s:%s' % (username, password))
userandpass = userandpass.decode('ascii')
return 'Basic %s' % userandpass
elif token:
return 'token %s' % token


try:
to_unicode = unicode
except NameError:
Expand Down Expand Up @@ -195,8 +213,12 @@ def load_url_to_file_handle(url, retry=2, retry_period=1, timeout=10):
:param timeout: timeout for opening the URL in seconds
:type timeout: float
"""
req = Request(url)
if GITHUB_USER and GITHUB_PASSWORD:
req.add_header('Authorization', auth_header(
username=GITHUB_USER, password=GITHUB_PASSWORD))
try:
fh = urlopen(url, timeout=timeout)
fh = urlopen(req, timeout=timeout)
except HTTPError as e:
if e.code == 503 and retry:
time.sleep(retry_period)
Expand Down

0 comments on commit 60506cc

Please sign in to comment.