Skip to content

Commit

Permalink
[enrich] Avoid "-- UNDEFINED --" values
Browse files Browse the repository at this point in the history
Avoid `-- UNDEFINED --` values for all SortingHat fields when
Mordred loses connection to the SortingHat server during the
enrichment or autorefresh execution. It will keep the values
of the `name`, `email`, `id` and `uuid` fields.

Signed-off-by: Quan Zhou <quan@bitergia.com>
  • Loading branch information
zhquan committed Sep 5, 2024
1 parent 7e0095c commit 1a5473f
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
11 changes: 7 additions & 4 deletions grimoire_elk/enriched/enrich.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,13 +826,16 @@ def get_sh_item_from_identity_cache(self, identity_tuple, backend_name):
email=iden['email'],
name=iden['name'],
username=iden['username'])
iden['uuid'] = identity_id
iden['id'] = identity_id
iden['enrollments'] = []

try:
individual = self.get_entity(identity_id)
if not individual:
msg = "Individual not found given the following identity: {}".format(identity_id)
logger.debug(msg)
return sh_item
return iden

for indv_identity in individual['identities']:
if indv_identity['uuid'] == identity_id:
Expand All @@ -842,9 +845,10 @@ def get_sh_item_from_identity_cache(self, identity_tuple, backend_name):
msg = "Identity {} not found in individual returned by SortingHat.".format(identity)
logger.error(msg)
return sh_item
except SortingHatClientError:
except SortingHatClientError as e:
msg = "None Identity found {}, identity: {}".format(backend_name, identity)
logger.debug(msg)
logger.error(msg)
raise SortingHatClientError(e)
return sh_item
except UnicodeEncodeError:
msg = "UnicodeEncodeError {}, identity: {}".format(backend_name, identity)
Expand Down Expand Up @@ -1054,7 +1058,6 @@ def get_item_sh(self, item, roles=None, date_field=None):
sh_fields = self.get_item_sh_fields(identity, item_date, rol=rol)
else:
sh_fields = self.get_item_no_sh_fields(identity, rol)

eitem_sh.update(sh_fields)

if not eitem_sh[rol + '_org_name']:
Expand Down
4 changes: 4 additions & 0 deletions grimoire_elk/enriched/sortinghat_gelk.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ def add_identities(cls, db, identities, backend):
except SortingHatClientError as ex:
for error in ex.errors:
msg = error['message']
status = error.get('status', None)
if 'already exists' in msg:
logger.debug("[sortinghat] {}".format(msg))
elif status and status == 502:
raise SortingHatClientError(ex)
else:
logger.warning("[sortinghat] {}".format(msg))

Expand Down Expand Up @@ -361,6 +364,7 @@ def get_entity(cls, db, id):
entity = result['data']['individuals']['entities'][0]
except SortingHatClientError as e:
logger.error("[sortinghat] Error get entities {}: {}".format(id, e.errors[0]['message']))
raise SortingHatClientError(e)
return entity

@classmethod
Expand Down
10 changes: 10 additions & 0 deletions releases/unreleased/avoid-'---undefined---'-values.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
title: Avoid '-- UNDEFINED --' values
category: fixed
author: Quan Zhou <quan@bitergia.com>
issue: null
notes: >
Avoid `-- UNDEFINED --` values for all SortingHat fields when
Mordred loses connection to the SortingHat server during the
enrichment or autorefresh execution. It will keep the values
of the `name`, `email`, `id` and `uuid` fields.
2 changes: 1 addition & 1 deletion tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def test_onion_study(self):
url = self.es_con + "/test_git_onion/_search?size=50"
response = requests.get(url, verify=False).json()
hits = response['hits']['hits']
self.assertEqual(len(hits), 16)
self.assertEqual(len(hits), 28)
for hit in hits:
source = hit['_source']
self.assertIn('timeframe', source)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def test_onion_study(self):
url = self.es_con + "/test_github_issues_onion/_search?size=20"
response = requests.get(url, verify=False).json()
hits = response['hits']['hits']
self.assertEqual(len(hits), 10)
self.assertEqual(len(hits), 12)
for hit in hits:
source = hit['_source']
self.assertIn('timeframe', source)
Expand All @@ -426,7 +426,7 @@ def test_onion_study(self):
url = self.es_con + "/test_github_prs_onion/_search?size=20"
response = requests.get(url, verify=False).json()
hits = response['hits']['hits']
self.assertEqual(len(hits), 10)
self.assertEqual(len(hits), 12)
for hit in hits:
source = hit['_source']
self.assertIn('timeframe', source)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def test_onion_study(self):
url = self.es_con + "/test_gitlab_onion/_search?size=50"
response = requests.get(url, verify=False).json()
hits = response['hits']['hits']
self.assertEqual(len(hits), 10)
self.assertEqual(len(hits), 22)
for hit in hits:
source = hit['_source']
self.assertIn('timeframe', source)
Expand Down

0 comments on commit 1a5473f

Please sign in to comment.