Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[affiliation] Fix subdomain affiliation when it's not top domain #806

Merged
merged 1 commit into from
Aug 6, 2023
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
8 changes: 8 additions & 0 deletions releases/unreleased/subdomain-affiliation-error.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: Sub-domain affiliation error
category: fixed
author: Eva Millán <evamillan@bitergia.com>
issue: 805
notes: |
The `affiliate` and `recommend affiliations` jobs no longer recommend
matches based on a domain's sub-domains if it is not marked as `top_domain`.
4 changes: 4 additions & 0 deletions sortinghat/core/recommendations/affiliation.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,21 @@ def _find_matching_domain(domain):
"""Look for domains and sub-domains that match with the given one."""

keep_looking = True
is_subdomain = False

# Splits the domain into root domains until
# is found in the database.
while keep_looking:
try:
result = find_domain(domain)
if is_subdomain and not result.is_top_domain:
result = None
keep_looking = False
except NotFoundError:
index = domain.find('.')
if index > -1:
domain = domain[index + 1:]
is_subdomain = True
else:
result = None
keep_looking = False
Expand Down
42 changes: 41 additions & 1 deletion tests/rec/test_affiliations.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def setUp(self):

api.add_organization(ctx, 'Bitergia')
api.add_domain(ctx, 'Bitergia', 'bitergia.com')
api.add_domain(ctx, 'Bitergia', 'bitergia.org')
api.add_domain(ctx, 'Bitergia', 'bitergia.org', is_top_domain=False)

api.add_organization(ctx, 'LibreSoft')

Expand Down Expand Up @@ -237,3 +237,43 @@ def test_not_found_individual(self):
recs = list(recommend_affiliations('FFFFFFFFFFFFFFFFFF'))

self.assertListEqual(recs, [])

def test_top_domain(self):
"""Check if recommendations are generated for subdomains only
when the domain is marked as top"""

ctx = SortingHatContext(self.user)

jsmith = api.add_identity(ctx,
source='scm',
email='jsmith@bitergia.org',
name='John Smith',
username='jsmith')
api.add_identity(ctx,
source='scm',
email='jsmith@subdomain.example.com',
uuid=jsmith.uuid)

jroe = api.add_identity(ctx,
source='scm',
email='jroe@example.com',
name='Jane Roe',
username='jroe')
api.add_identity(ctx,
source='scm',
email='jroe@subdomain.bitergia.org',
uuid=jroe.uuid)

# Test
uuids = [jsmith.uuid, jroe.uuid]
recs = list(recommend_affiliations(uuids))

self.assertEqual(len(recs), 2)

rec = recs[0]
self.assertEqual(rec[0], jsmith.uuid)
self.assertListEqual(rec[1], ['Bitergia', 'Example'])

rec = recs[1]
self.assertEqual(rec[0], jroe.uuid)
self.assertListEqual(rec[1], ['Example'])