Skip to content

Commit 3c509de

Browse files
committed
Surface exact ticket id match first in ticket autocomplete
Typing a known ticket number into a link field often failed to offer that ticket: the autocomplete ran a single query capped at ten rows with no exact-match priority, so when many tickets carried the number in their Subject the one ticket the user actually wanted could fall outside the window and never appear.
1 parent 9352b04 commit 3c509de

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

  • share/html/Helpers/Autocomplete

share/html/Helpers/Autocomplete/Tickets

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,35 @@ $max //= 10;
101101
if ( $limit ) {
102102
$sql = "($sql) AND ($limit)";
103103
}
104+
my @suggestions;
105+
my %seen;
106+
107+
# Surface an exact ticket id match first. The main query below is capped by
108+
# RowsPerPage, so an exact id can sort past $max when many tickets match the
109+
# term in their Subject. A dedicated indexed lookup guarantees it appears.
110+
if ( $term =~ /^\d+$/ && !grep { $_ eq $term } @excludes ) {
111+
my $exact_sql = "id = '$term'";
112+
$exact_sql = "($exact_sql) AND ($limit)" if $limit;
113+
114+
my $exact = RT::Tickets->new( $CurrentUser );
115+
$exact->FromSQL($exact_sql);
116+
if ( my $ticket = $exact->First ) { # id is unique, so at most one match
117+
my $formatted = loc("#[_1]: [_2]", $ticket->Id, $ticket->Subject);
118+
push @suggestions, { label => $formatted, value => $ticket->$return };
119+
$seen{ $ticket->Id } = 1;
120+
}
121+
}
122+
123+
# Fill the remaining slots with the existing search, in its current order,
124+
# skipping the exact match already emitted above.
104125
$tickets->FromSQL($sql);
105126
$tickets->RowsPerPage( $max );
106127

107-
my @suggestions;
108-
109128
while ( my $ticket = $tickets->Next ) {
129+
next if $seen{ $ticket->Id };
110130
my $formatted = loc("#[_1]: [_2]", $ticket->Id, $ticket->Subject);
111131
push @suggestions, { label => $formatted, value => $ticket->$return };
132+
last if @suggestions >= $max;
112133
}
113134
return @suggestions if defined wantarray;
114135

0 commit comments

Comments
 (0)