Skip to content

Commit 35d465b

Browse files
authored
Merges #3 Closes #3
2 parents 7e7d1f4 + 45d03be commit 35d465b

3 files changed

Lines changed: 78 additions & 2 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: 5xx errors ignored
3+
category: added
4+
author: Santiago Dueñas <sduenas@bitergia.com>
5+
issue: null
6+
notes: >
7+
The importer won't fail when 5xx errors are returned
8+
by the server. These errors are normally due to invalid
9+
identities stored in the platform.

sortinghat/core/importer/backends/eclipse.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,20 @@ def get_individuals(self):
108108

109109
# Fetch accounts pages
110110
for account in client.fetch_accounts(epoch=epoch):
111-
ef_profile = client.fetch_account_profile(account['name'])
111+
try:
112+
ef_profile = client.fetch_account_profile(account['name'])
113+
except requests.exceptions.HTTPError as error:
114+
# Ignore 5xx errors
115+
if 500 <= error.response.status_code < 600:
116+
msg = (
117+
f"Unable to fetch {account['name']} profile."
118+
f"Server error: {error.response.status_code} - {error.response.reason}."
119+
"Skipping"
120+
)
121+
logger.error(msg)
122+
continue
123+
else:
124+
raise error
112125

113126
if not ef_profile['eca']['signed']:
114127
continue

tests/test_eclipse.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def read_file(filename, mode='r'):
3131
return content
3232

3333

34-
def setup_mock_server():
34+
def setup_mock_server(raise_5xx_errors=False):
3535
"""Configure a Mock server"""
3636

3737
def request_callback(request, uri, headers):
@@ -88,6 +88,17 @@ def request_callback(request, uri, headers):
8888
ECLIPSE_API_URL + "/account/profile/jrae/employment-history",
8989
body=read_file('data/eclipse_jrae_employment.json'))
9090

91+
# Overwrite jsmith and jdoe profile replies to raise 5xx errors
92+
if raise_5xx_errors:
93+
httpretty.register_uri(httpretty.GET,
94+
ECLIPSE_API_URL + "/account/profile/jsmith",
95+
body="",
96+
status=500)
97+
httpretty.register_uri(httpretty.GET,
98+
ECLIPSE_API_URL + "/account/profile/jdoe",
99+
body="",
100+
status=504)
101+
91102
return requests, bodies
92103

93104

@@ -153,6 +164,7 @@ def test_backend_name(self):
153164
@patch('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login', return_value="mocked_login")
154165
@patch('sortinghat.core.importer.backends.eclipse.datetime_utcnow', return_value=MOCK_DATETIME_NOW)
155166
def test_import_identities(self, mock_login, mock_datetime_now):
167+
"""Check if identities are imported"""
156168

157169
# Set up a mock HTTP server
158170
requests, bodies = setup_mock_server()
@@ -222,6 +234,8 @@ def test_import_identities(self, mock_login, mock_datetime_now):
222234
@patch('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login', return_value="mocked_login")
223235
@patch('sortinghat.core.importer.backends.eclipse.datetime_utcnow', return_value=MOCK_DATETIME_NOW)
224236
def test_import_no_identities(self, mock_login, mock_datetime_now):
237+
"""Check if all goes ok when there aren't identities to import"""
238+
225239
# Set up a mock HTTP server
226240
requests, bodies = setup_mock_server()
227241

@@ -236,10 +250,49 @@ def test_import_no_identities(self, mock_login, mock_datetime_now):
236250
# No identities
237251
self.assertEqual(n, 0)
238252

253+
@httpretty.activate
254+
@patch('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login', return_value="mocked_login")
255+
@patch('sortinghat.core.importer.backends.eclipse.datetime_utcnow', return_value=MOCK_DATETIME_NOW)
256+
def test_ignore_5xx_errors(self, mock_login, mock_datetime_now):
257+
"""Check if the importer ignores 5xx errors"""
258+
259+
# Set up a mock HTTP server
260+
requests, bodies = setup_mock_server(raise_5xx_errors=True)
261+
262+
importer = EclipseFoundationAccountsImporter(ctx=self.ctx, url=None, from_date=None)
263+
n = importer.import_identities()
264+
265+
# In total, only 1 individual and 2 identities will be imported.
266+
# Individuals 'jsmith' and 'jdoe' profiles return 5xx errors.
267+
self.assertEqual(n, 2)
268+
269+
individuals = Individual.objects.order_by('mk').all()
270+
271+
self.assertEqual(len(individuals), 1)
272+
273+
# Jane Rae
274+
jrae = individuals[0]
275+
self.assertEqual(jrae.profile.name, 'Jane Rae')
276+
self.assertEqual(jrae.profile.email, 'jrae@example.com')
277+
278+
ids = jrae.identities.order_by('uuid').all()
279+
280+
self.assertEqual(len(ids), 2)
281+
self.assertEqual(ids[0].name, 'Jane Rae')
282+
self.assertEqual(ids[0].email, 'jrae@example.com')
283+
self.assertEqual(ids[0].username, 'jrae')
284+
self.assertEqual(ids[0].source, 'eclipsefdn')
285+
286+
self.assertEqual(ids[1].name, 'Jane Rae')
287+
self.assertEqual(ids[1].email, 'jrae@example.com')
288+
self.assertEqual(ids[1].username, 'jrae')
289+
self.assertEqual(ids[1].source, 'github')
290+
239291
@httpretty.activate
240292
@patch('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login', return_value="mocked_login")
241293
@patch('sortinghat.core.importer.backends.eclipse.datetime_utcnow', return_value=MOCK_DATETIME_NOW)
242294
def test_import_merge_identities(self, mock_login, mock_datetime_now):
295+
"""Check if existing identities are merged"""
243296

244297
# Add individuals that share email and github handle
245298
api.add_identity(self.ctx, source='github', username='jsmith')
@@ -261,6 +314,7 @@ def test_import_merge_identities(self, mock_login, mock_datetime_now):
261314
@patch('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login', return_value="mocked_login")
262315
@patch('sortinghat.core.importer.backends.eclipse.datetime_utcnow', return_value=MOCK_DATETIME_NOW)
263316
def test_import_enrollments(self, mock_login, mock_datetime_now):
317+
"""Check if enrolments are imported"""
264318

265319
# Set up a mock HTTP server
266320
setup_mock_server()

0 commit comments

Comments
 (0)