Skip to content

Commit

Permalink
Merge pull request #84 from paulrzcz/develop
Browse files Browse the repository at this point in the history
Bitbucket with authorization and built with requests package
  • Loading branch information
ralphbean committed Nov 5, 2013
2 parents d2a7f66 + 5b25562 commit f25be82
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions bugwarrior/services/bitbucket.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from twiggy import log

from bugwarrior.services import IssueService
from bugwarrior.config import die
from bugwarrior.config import die, get_service_password

import datetime
import urllib2
import json
import requests


class BitbucketService(IssueService):
Expand All @@ -24,6 +23,16 @@ class BitbucketService(IssueService):
def __init__(self, *args, **kw):
super(BitbucketService, self).__init__(*args, **kw)

login = self.config.get(self.target, 'login')
password = self.config.get(self.target, 'passw')
if not password or password.startswith('@oracle:'):
username = self.config.get(self.target, 'username')
service = "bitbucket://%s@bitbucket.org/%s" % (login, username)
password = get_service_password(service, login, oracle=password,
interactive=self.config.interactive)

self.auth = (login, password)

@classmethod
def validate_config(cls, config, target):
if not config.has_option(target, 'username'):
Expand All @@ -36,21 +45,13 @@ def validate_config(cls, config, target):
# Note -- not actually rate limited, I think.
def pull(self, tag):
url = self.base_api + '/repositories/%s/issues/' % tag
try:
f = urllib2.urlopen(url)
except urllib2.HTTPError as e:
if '403' in str(e):
return []
else:
raise e
response = json.loads(f.read())
response = _getter(url, auth=self.auth)
return [(tag, issue) for issue in response['issues']]

def annotations(self, tag, issue):
url = self.base_api + '/repositories/%s/issues/%i/comments' % (
tag, issue['local_id'])
f = urllib2.urlopen(url)
response = json.loads(f.read())
response = _getter(url, auth=self.auth)
_parse = lambda s: datetime.datetime.strptime(s[:10], "%Y-%m-%d")
return dict([
self.format_annotation(
Expand All @@ -68,8 +69,7 @@ def issues(self):
user = self.config.get(self.target, 'username')

url = self.base_api + '/users/' + user + '/'
f = urllib2.urlopen(url)
response = json.loads(f.read())
response = _getter(url, auth=self.auth)
repos = [
repo.get('slug') for repo in response.get('repositories')
if repo.get('has_issues')
Expand Down Expand Up @@ -101,3 +101,20 @@ def issues(self):
),
**self.annotations(tag, issue)
) for tag, issue in issues]


def _getter(url, auth):
response = requests.get(url, auth=auth)

# And.. if we didn't get good results, just bail.
if response.status_code != 200:
raise IOError(
"Non-200 status code %r; %r; %r" % (
response.status_code, url, response.json))

if callable(response.json):
# Newer python-requests
return response.json()
else:
# Older python-requests
return response.json

0 comments on commit f25be82

Please sign in to comment.