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

Gracefully handling an IRC lookup service outage #127

Merged
merged 1 commit into from
Apr 6, 2018
Merged
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
14 changes: 9 additions & 5 deletions highfive/newpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,15 @@ def get_irc_nick(gh_name):
if the user does not exist on the rustacean database,
or if the user has no `irc` field associated with their username
"""
data = urllib2.urlopen(rustaceans_api_url.format(username=gh_name))
if data.getcode() == 200:
rustacean_data = json.loads(data.read())
if rustacean_data:
return rustacean_data[0].get("irc")
try:
data = urllib2.urlopen(rustaceans_api_url.format(username=gh_name))
if data.getcode() == 200:
rustacean_data = json.loads(data.read())
if rustacean_data:
return rustacean_data[0].get("irc")
except urllib2.HTTPError:
pass

return None

def post_warnings(payload, config, diff, owner, repo, issue, token):
Expand Down
10 changes: 7 additions & 3 deletions highfive/tests/test_newpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ def test_find_reviewer(self):
)

def setup_get_irc_nick_mocks(self, mock_urllib2, status_code, data=None):
if status_code != 200:
mock_urllib2.side_effect = HTTPError(
None, status_code, None, None, None
)
return

mock_data = mock.Mock()
mock_data.getcode.return_value = status_code
mock_data.read.return_value = data
Expand All @@ -263,14 +269,12 @@ def setup_get_irc_nick_mocks(self, mock_urllib2, status_code, data=None):

@mock.patch('highfive.newpr.urllib2')
def test_get_irc_nick_non_200(self, mock_urllib2):
mock_data = self.setup_get_irc_nick_mocks(mock_urllib2, 300)
self.setup_get_irc_nick_mocks(mock_urllib2, 503)
self.assertIsNone(newpr.get_irc_nick('foo'))

mock_urllib2.urlopen.assert_called_with(
'http://www.ncameron.org/rustaceans/user?username=foo'
)
mock_data.getcode.assert_called()
mock_data.read.assert_not_called()

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