Skip to content

Commit

Permalink
Merge pull request #464 from ssbarnea/develop
Browse files Browse the repository at this point in the history
Added authentication method detection to gerrit
  • Loading branch information
ralphbean authored Apr 10, 2017
2 parents 2170254 + faf9d4c commit 8d5db90
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
16 changes: 12 additions & 4 deletions bugwarrior/services/gerrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@ def __init__(self, *args, **kw):
'Accept-Encoding': 'gzip',
})

self.session.auth = requests.auth.HTTPDigestAuth(
self.username, self.password)

if self.ssl_ca_path != None:
if self.ssl_ca_path:
self.session.verify = os.path.expanduser(self.ssl_ca_path)

# uses digest authentication if supported by the server, fallback to basic
# gerrithub.io supports only basic
response = self.session.head(self.url + '/a/')
if 'digest' in response.headers.get('www-authenticate', '').lower():
self.session.auth = requests.auth.HTTPDigestAuth(
self.username, self.password)
else:
self.session.auth = requests.auth.HTTPBasicAuth(
self.username, self.password)

@staticmethod
def get_keyring_service(service_config):
base_uri = service_config.get('base_uri')
Expand All @@ -110,6 +117,7 @@ def issues(self):
'?q=is:open+is:reviewer' + \
'&o=MESSAGES&o=DETAILED_ACCOUNTS'
response = self.session.get(url)
response.raise_for_status()
# The response has some ")]}'" garbage prefixed.
body = response.text[4:]
changes = json.loads(body)
Expand Down
4 changes: 4 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ class ServiceTest(ConfigTest):
SERVICE_CONFIG = {
}

@classmethod
def setUpClass(cls):
cls.maxDiff = None

def get_mock_service(
self, service_class, section='unspecified',
config_overrides=None, general_overrides=None
Expand Down
13 changes: 9 additions & 4 deletions tests/test_gerrit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class TestGerritIssue(AbstractServiceTest, ServiceTest):
SERVICE_CONFIG = {
'gerrit.base_uri': 'https://one',
'gerrit.base_uri': 'https://one.com',
'gerrit.username': 'two',
'gerrit.password': 'three',
}
Expand All @@ -27,6 +27,11 @@ class TestGerritIssue(AbstractServiceTest, ServiceTest):

def setUp(self):
super(TestGerritIssue, self).setUp()

responses.add(
responses.HEAD,
self.SERVICE_CONFIG['gerrit.base_uri'] + '/a/',
adding_headers={'www-authenticate': 'digest'})
self.service = self.get_mock_service(GerritService)

def test_to_taskwarrior(self):
Expand Down Expand Up @@ -56,18 +61,18 @@ def test_to_taskwarrior(self):
@responses.activate
def test_issues(self):
self.add_response(
'https://one/a/changes/?q=is:open+is:reviewer&o=MESSAGES&o=DETAILED_ACCOUNTS',
'https://one.com/a/changes/?q=is:open+is:reviewer&o=MESSAGES&o=DETAILED_ACCOUNTS',
# The response has some ")]}'" garbage prefixed.
body=")]}'" + json.dumps([self.record]))

issue = next(self.service.issues())

expected = {
'annotations': [u'@Iam Author - is is a message'],
'description': u'(bw)PR#1 - this is a title .. https://one/#/c/1/',
'description': u'(bw)PR#1 - this is a title .. https://one.com/#/c/1/',
'gerritid': 1,
'gerritsummary': u'this is a title',
'gerriturl': 'https://one/#/c/1/',
'gerriturl': 'https://one.com/#/c/1/',
'gerritbranch': 'master',
'gerrittopic': 'test-topic',
'priority': 'M',
Expand Down

0 comments on commit 8d5db90

Please sign in to comment.