Skip to content

Commit 6513fb2

Browse files
authored
Merge branch 'handle-500-errors' of 'https://github.com/sduenas/sortinghat-eclipse-foundation'
Merges #4 Closes #4
2 parents e69ef37 + b0a59c8 commit 6513fb2

3 files changed

Lines changed: 53 additions & 29 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 handled for every request
3+
category: added
4+
author: Santiago Dueñas <sduenas@bitergia.com>
5+
issue: null
6+
notes: >
7+
Errors were only handled when profiles were fetched
8+
and not in other cases.
9+

sortinghat/core/importer/backends/eclipse.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,9 @@ def get_individuals(self):
108108

109109
# Fetch accounts pages
110110
for account in client.fetch_accounts(epoch=epoch):
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
125-
126-
if not ef_profile['eca']['signed']:
111+
ef_profile = client.fetch_account_profile(account['name'])
112+
113+
if not ef_profile:
127114
continue
128115

129116
individual = Individual(uuid=ef_profile['uid'])
@@ -154,21 +141,22 @@ def get_individuals(self):
154141
)
155142
individual.identities.append(idt)
156143

157-
employment_history = client.fetch_employment_history(account['name'])
158-
159144
# Fetch enrollments for the identity. If no enrollment is set
160145
# use the organization field from the profile, if set.
161-
for employment in employment_history:
162-
org = Organization(name=employment['organization_name'])
163-
start, end = None, None
146+
employment_history = client.fetch_employment_history(account['name'])
164147

165-
if employment['start']:
166-
start = str_to_datetime(employment['start'])
167-
if employment['end']:
168-
end = str_to_datetime(employment['end'])
148+
if employment_history:
149+
for employment in employment_history:
150+
org = Organization(name=employment['organization_name'])
151+
start, end = None, None
169152

170-
enr = Enrollment(org, start=start, end=end)
171-
individual.enrollments.append(enr)
153+
if employment['start']:
154+
start = str_to_datetime(employment['start'])
155+
if employment['end']:
156+
end = str_to_datetime(employment['end'])
157+
158+
enr = Enrollment(org, start=start, end=end)
159+
individual.enrollments.append(enr)
172160

173161
if not individual.enrollments:
174162
company = ef_profile.get('org', None)
@@ -292,8 +280,21 @@ def fetch_employment_history(self, eclipsefdn_id):
292280
def _fetch(self, url, params=None):
293281
"""Generic query to Eclipse usr API."""
294282

295-
response = requests.get(url, params=params, auth=self.token)
296-
response.raise_for_status()
283+
try:
284+
response = requests.get(url, params=params, auth=self.token)
285+
response.raise_for_status()
286+
except requests.exceptions.HTTPError as error:
287+
# Ignore 5xx errors
288+
if 500 <= error.response.status_code < 600:
289+
msg = (
290+
f"Unable to fetch {url}"
291+
f"Server error: {error.response.status_code} - {error.response.reason}."
292+
"Skipping"
293+
)
294+
logger.error(msg)
295+
return None
296+
else:
297+
raise error
297298

298299
return response.json()
299300

tests/test_eclipse.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ def request_callback(request, uri, headers):
9898
ECLIPSE_API_URL + "/account/profile/jdoe",
9999
body="",
100100
status=504)
101+
httpretty.register_uri(httpretty.GET,
102+
ECLIPSE_API_URL + "/account/profile/jrae/employment-history",
103+
body="",
104+
status=504)
101105

102106
return requests, bodies
103107

@@ -264,6 +268,8 @@ def test_ignore_5xx_errors(self, mock_login, mock_datetime_now):
264268

265269
# In total, only 1 individual and 2 identities will be imported.
266270
# Individuals 'jsmith' and 'jdoe' profiles return 5xx errors.
271+
# Employment info for 'jrae' also returns an error, so the affiliation
272+
# info is taken from the profile page setting default dates.
267273
self.assertEqual(n, 2)
268274

269275
individuals = Individual.objects.order_by('mk').all()
@@ -288,6 +294,14 @@ def test_ignore_5xx_errors(self, mock_login, mock_datetime_now):
288294
self.assertEqual(ids[1].username, 'jrae')
289295
self.assertEqual(ids[1].source, 'github')
290296

297+
enrollments = jrae.enrollments.all()
298+
self.assertEqual(len(enrollments), 1)
299+
300+
rol = enrollments[0]
301+
self.assertEqual(rol.group.name, "ACME")
302+
self.assertEqual(rol.start, MIN_PERIOD_DATE)
303+
self.assertEqual(rol.end, MAX_PERIOD_DATE)
304+
291305
@httpretty.activate
292306
@patch('sortinghat.core.importer.backends.eclipse.EclipseFoundationAPIClient.login', return_value="mocked_login")
293307
@patch('sortinghat.core.importer.backends.eclipse.datetime_utcnow', return_value=MOCK_DATETIME_NOW)

0 commit comments

Comments
 (0)