Skip to content

Commit 47d6476

Browse files
committed
Tweak Jira assignee matching
1 parent e930121 commit 47d6476

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

Rover_Lookup/lookup.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@ def github_username_to_emails(
6363
try:
6464
# Create LDAP server connection; if credentials were not provided, the connection
6565
# will use an anonymous binding.
66-
conn = Connection(ldap_server, ldap_bind_dn, ldap_password, auto_bind=True)
66+
conn = Connection(
67+
ldap_server,
68+
ldap_bind_dn,
69+
ldap_password,
70+
auto_bind=True,
71+
raise_exceptions=True,
72+
)
6773
except LDAPException as e:
6874
msg = f"Error connecting to LDAP server {ldap_server!r}: {str(e)}"
6975
if "redhat.com" in ldap_server and "invalid server address" in str(e):
@@ -76,8 +82,8 @@ def github_username_to_emails(
7682
)
7783
return None
7884

79-
logger.debug(f"Searching for GitHub username: {github_username}")
80-
logger.debug(f"LDAP filter: {ldap_filter}")
85+
logger.debug("Searching for GitHub username: %r", github_username)
86+
logger.debug("LDAP filter: %r", ldap_filter)
8187

8288
try:
8389
# Perform the LDAP search
@@ -89,21 +95,23 @@ def github_username_to_emails(
8995
)
9096

9197
if not success:
92-
logger.warning(f"LDAP search failed for GitHub username: {github_username}")
98+
logger.info("LDAP search failed for GitHub username: %r", github_username)
9399
return None
94100

95101
entries = conn.entries
96-
logger.debug(f"Found {len(entries)} LDAP entries")
102+
logger.debug("Found %d LDAP entries", len(entries))
97103

98104
if not entries:
99-
logger.info(f"No LDAP entries found for GitHub username: {github_username}")
105+
logger.info(
106+
"No LDAP entries found for GitHub username: %r", github_username
107+
)
100108
return []
101109

102110
# Extract email addresses from all entries
103111
email_addresses = set() # Use set to automatically handle uniqueness
104112

105113
for entry in entries:
106-
logger.debug(f"Processing LDAP entry: {entry.entry_dn}")
114+
logger.debug("Processing LDAP entry: %r", entry.entry_dn)
107115

108116
# Check each email field
109117
for attr_name in attributes:
@@ -124,9 +132,11 @@ def github_username_to_emails(
124132
result_emails = sorted(list(email_addresses))
125133

126134
logger.debug(
127-
f"Found {len(result_emails)} unique email addresses for GitHub username: {github_username}"
135+
"Found %d unique email addresses for GitHub username, %r: %s",
136+
len(result_emails),
137+
github_username,
138+
result_emails,
128139
)
129-
logger.debug(f"Email addresses: {result_emails}")
130140

131141
return result_emails
132142

Rover_Lookup/tests/test_lookup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def test_ldap_search_failure(self, mock_connection_class, caplog):
170170
mock_connection_class.return_value = mock_conn
171171
mock_conn.search.return_value = False # Search failed
172172

173-
with caplog.at_level(logging.WARNING):
173+
with caplog.at_level(logging.INFO):
174174
result = github_username_to_emails("test-user")
175175

176176
assert result is None
@@ -269,6 +269,7 @@ def test_custom_ldap_parameters(self, mock_connection_class):
269269
"cn=bind,dc=custom,dc=com",
270270
"secret",
271271
auto_bind=True,
272+
raise_exceptions=True,
272273
)
273274

274275
# Verify custom base DN was used in search

sync2jira/downstream_issue.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,29 @@ def match_user(
385385

386386
if not users:
387387
continue
388-
if len(users) > 1:
389-
log.warning("Found multiple Jira users for %r", email)
390-
return users[0].name # TODO: We should probably return the ID here, instead.
388+
389+
if len(users) == 1:
390+
# TODO: We should probably return the ID here, instead.
391+
return users[0].name
392+
393+
limit = 5
394+
log.warning(
395+
"Found %d Jira users for %r: %s%s",
396+
len(users),
397+
email,
398+
", ".join(u.name for u in users[0:limit]),
399+
"..." if len(users) > limit else "",
400+
)
401+
for user in users:
402+
# This condition _should_ be true for *all* entries returned, in
403+
# which case we'll just return the first entry; however it appears
404+
# that sometimes Jira returns _all_ assignable users, so do our own
405+
# filtering.
406+
if user.emailAddress == email:
407+
log.info("Found matching user: %r", user.name)
408+
return user.name
409+
else:
410+
log.warning("Found no Jira user which matches %r", email)
391411

392412
return None
393413

@@ -409,6 +429,7 @@ def assign_user(
409429
if remove_all:
410430
# Update the issue to have no assignees
411431
downstream.update(assignee={"name": ""})
432+
log.info("Cleared assignment of %s.", downstream.key)
412433
return
413434

414435
# JIRA only supports one assignee; if we have more than one (i.e., from
@@ -426,6 +447,7 @@ def assign_user(
426447
if match_name:
427448
# Assign the downstream issue to the matched user
428449
downstream.update({"assignee": {"name": match_name}})
450+
log.info("Assigned %s to %r", downstream.key, match_name)
429451
return
430452

431453
if issue.assignee:
@@ -501,7 +523,7 @@ def _get_preferred_issue_types(config, issue):
501523
In all cases, a list of one item is returned, except when the upstream
502524
issue has multiple tags which match multiple entries in the configured
503525
mapping, in which case multiple entries are returned, sorted in ascending
504-
lexographical order.
526+
lexicographical order.
505527
506528
:param Dict config: Config dict
507529
:param sync2jira.intermediary.Issue issue: Issue object
@@ -907,7 +929,6 @@ def _update_assignee(client, existing, issue, overwrite):
907929

908930
if update:
909931
assign_user(client, issue, existing, remove_all=clear)
910-
log.info("%s assignee", "Cleared" if clear else "Updated")
911932

912933

913934
def _update_jira_labels(issue, labels):

tests/test_downstream_issue.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,13 @@ def test_assign_user_multiple(self, mock_client, mock_rover_lookup):
292292
# Set up return values
293293
mock_user = MagicMock()
294294
mock_user.displayName = "mock_assignee"
295+
mock_user.name = "mock_assignee_n"
296+
mock_user.emailAddress = "wrong_mock_user@redhat.com"
295297
mock_user.key = "mock_user_key"
296298
mock_user2 = MagicMock()
297299
mock_user2.displayName = "mock_assignee2"
300+
mock_user2.name = "mock_assignee2_n"
301+
mock_user.emailAddress = "mock_user@redhat.com"
298302
mock_user2.key = "mock_user_key2"
299303
mock_client.search_assignable_users_for_issues.return_value = [
300304
mock_user,

0 commit comments

Comments
 (0)