Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.

Commit e81f5eb

Browse files
authored
Merge pull request #127 from davidalber/irc-lookup-outage-protection
Gracefully handling an IRC lookup service outage
2 parents be72156 + 71663c8 commit e81f5eb

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

highfive/newpr.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,15 @@ def get_irc_nick(gh_name):
327327
if the user does not exist on the rustacean database,
328328
or if the user has no `irc` field associated with their username
329329
"""
330-
data = urllib2.urlopen(rustaceans_api_url.format(username=gh_name))
331-
if data.getcode() == 200:
332-
rustacean_data = json.loads(data.read())
333-
if rustacean_data:
334-
return rustacean_data[0].get("irc")
330+
try:
331+
data = urllib2.urlopen(rustaceans_api_url.format(username=gh_name))
332+
if data.getcode() == 200:
333+
rustacean_data = json.loads(data.read())
334+
if rustacean_data:
335+
return rustacean_data[0].get("irc")
336+
except urllib2.HTTPError:
337+
pass
338+
335339
return None
336340

337341
def post_warnings(payload, config, diff, owner, repo, issue, token):

highfive/tests/test_newpr.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ def test_find_reviewer(self):
255255
)
256256

257257
def setup_get_irc_nick_mocks(self, mock_urllib2, status_code, data=None):
258+
if status_code != 200:
259+
mock_urllib2.side_effect = HTTPError(
260+
None, status_code, None, None, None
261+
)
262+
return
263+
258264
mock_data = mock.Mock()
259265
mock_data.getcode.return_value = status_code
260266
mock_data.read.return_value = data
@@ -263,14 +269,12 @@ def setup_get_irc_nick_mocks(self, mock_urllib2, status_code, data=None):
263269

264270
@mock.patch('highfive.newpr.urllib2')
265271
def test_get_irc_nick_non_200(self, mock_urllib2):
266-
mock_data = self.setup_get_irc_nick_mocks(mock_urllib2, 300)
272+
self.setup_get_irc_nick_mocks(mock_urllib2, 503)
267273
self.assertIsNone(newpr.get_irc_nick('foo'))
268274

269275
mock_urllib2.urlopen.assert_called_with(
270276
'http://www.ncameron.org/rustaceans/user?username=foo'
271277
)
272-
mock_data.getcode.assert_called()
273-
mock_data.read.assert_not_called()
274278

275279
@mock.patch('highfive.newpr.urllib2')
276280
def test_get_irc_nick_no_data(self, mock_urllib2):

0 commit comments

Comments
 (0)