Skip to content

Commit

Permalink
[IMP] connector_jira: improved user binding
Browse files Browse the repository at this point in the history
- specify key/value when user binding fails
- resolve by login (which is unique) first, then my potentially-conflicting email
- if multiple JIRA users are returned, merge them by key as it's unique
  • Loading branch information
alexey-pelykh committed Aug 13, 2019
1 parent 79e2276 commit 02c22ce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
7 changes: 7 additions & 0 deletions connector_jira/models/jira_backend/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright: 2015 LasLabs, Inc.
# Copyright 2016-2019 Camptocamp SA
# Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import binascii
Expand Down Expand Up @@ -503,6 +504,12 @@ def import_res_users(self):
).render({'backend': self, 'result': bknd_result})
return True

@api.multi
def get_user_resolution_order(self):
""" User resolution should happen by login first as it's unique, while
resolving by email is likely to give false positives """
return ['login', 'email']

@api.multi
def import_issue_type(self):
self.env['jira.issue.type'].import_batch(self)
Expand Down
35 changes: 25 additions & 10 deletions connector_jira/models/res_users/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Copyright 2016-2019 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
# Copyright 2019 Brainbean Apps (https://brainbeanapps.com)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)

from itertools import groupby

from odoo import _, api, exceptions, fields, models
from odoo.addons.component.core import Component
Expand Down Expand Up @@ -54,20 +57,24 @@ def link_with_jira(self, backends=None, raise_if_mismatch=False):
for user in self:
if binder.to_external(user, wrap=True):
continue
jira_user = adapter.search(fragment=user.email)
if not jira_user:
jira_user = adapter.search(fragment=user.login)
jira_user = None
for resolve_by in backend.get_user_resolution_order():
resolve_by_key = resolve_by
resolve_by_value = user[resolve_by]
jira_user = adapter.search(fragment=resolve_by_value)
if jira_user:
break
if not jira_user:
continue
elif len(jira_user) > 1:
if raise_if_mismatch:
raise exceptions.UserError(_(
'Several users found for %s. '
'Several users found with "%s" set to "%s". '
'Set it manually.'
) % user.login)
) % (resolve_by_key, resolve_by_value))
bknd_result['error'].append({
'key': 'login',
'value': user.login,
'key': resolve_by_key,
'value': resolve_by_value,
'error': 'multiple_found',
'detail': [x.key for x in jira_user]
})
Expand All @@ -82,8 +89,8 @@ def link_with_jira(self, backends=None, raise_if_mismatch=False):
])
if existing:
bknd_result['error'].append({
'key': 'login',
'value': user.login,
'key': resolve_by_key,
'value': resolve_by_value,
'error': 'other_user_bound',
'detail': 'linked with %s' % (existing.login,)
})
Expand Down Expand Up @@ -127,4 +134,12 @@ def search(self, fragment=None):
users = self.client.search_users(fragment, maxResults=None,
includeActive=True,
includeInactive=True)

# User 'key' is unique and if same key appears several times, it means
# that same user is present in multiple User Directories
users = list(map(
lambda group: list(group[1])[0],
groupby(users, key=lambda user: user.key)
))

return users

0 comments on commit 02c22ce

Please sign in to comment.