[Rt-commit] rt branch, 4.4/search-watcher-by-group-name, created. rt-4.4.4-147-gbf41df16b9

? sunnavy sunnavy at bestpractical.com
Tue Nov 3 09:48:27 EST 2020


The branch, 4.4/search-watcher-by-group-name has been created
        at  bf41df16b95d1b4d7ea1cf65693d3c6b6266c6d6 (commit)

- Log -----------------------------------------------------------------
commit 1f23670e3e8521b176a0e0c078c20ccb8183d893
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Nov 3 05:00:16 2020 +0800

    Support to search user defined group names in watcher limit
    
    To search tickets that have a specified group in a role, previously we
    had to specify the group id like "AdminCc.id = 123", which is not
    convenient. Here we support group names to improve this, e.g.
    "AdminCc.Name = 'team foo'".
    
    In case the name in question is shared by a user and a group(which is
    weird and should be quite rare in real world), both will be respected.
    I.e. we return tickets that have either the user or the group in the
    role.

diff --git a/lib/RT/SearchBuilder/Role/Roles.pm b/lib/RT/SearchBuilder/Role/Roles.pm
index 5980882d31..3741b2ba9c 100644
--- a/lib/RT/SearchBuilder/Role/Roles.pm
+++ b/lib/RT/SearchBuilder/Role/Roles.pm
@@ -235,7 +235,7 @@ sub RoleLimit {
 
     my $column = $type ? $class->Role($type)->{Column} : undef;
 
-    # if it's equality op and search by Email or Name then we can preload user
+    # if it's equality op and search by Email or Name then we can preload user/group
     # we do it to help some DBs better estimate number of rows and get better plans
     if ( $args{OPERATOR} =~ /^!?=$/
              && (!$args{FIELD} || $args{FIELD} eq 'Name' || $args{FIELD} eq 'EmailAddress') ) {
@@ -245,8 +245,27 @@ sub RoleLimit {
             ? ($column ? 'Load' : 'LoadByEmail')
             : $args{FIELD} eq 'EmailAddress' ? 'LoadByEmail': 'Load';
         $o->$method( $args{VALUE} );
+        my @values;
+        @values = $o->Id if $o->Id;
+
+        if ( !$args{FIELD} || $args{FIELD} eq 'Name' ) {
+            my $group = RT::Group->new( $self->CurrentUser );
+            $group->LoadUserDefinedGroup( $args{VALUE} );
+            push @values, $group->Id if $group->Id;
+        }
+
         $args{FIELD} = 'id';
-        $args{VALUE} = $o->id || 0;
+        if ( @values == 1 ) {
+            $args{VALUE} = $values[0];
+        }
+        elsif ( @values > 1 ) {
+            RT->Logger->debug("Name $args{VALUE} is used in both user and group");
+            $args{VALUE} = \@values;
+            $args{OPERATOR} = $args{OPERATOR} =~ /!/ ? 'NOT IN' : 'IN';
+        }
+        else {
+            $args{VALUE} = 0;
+        }
     }
 
     if ( $column and $args{FIELD} and $args{FIELD} eq 'id' ) {

commit bf41df16b95d1b4d7ea1cf65693d3c6b6266c6d6
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Nov 3 14:38:48 2020 +0800

    Test ticket search by id/name of the group added in watchers

diff --git a/t/ticket/search_by_watcher_group.t b/t/ticket/search_by_watcher_group.t
index 9c522d5d0b..b02e2cc74a 100644
--- a/t/ticket/search_by_watcher_group.t
+++ b/t/ticket/search_by_watcher_group.t
@@ -20,6 +20,9 @@ my $group;
 my $root = RT::Test->load_or_create_user( Name => 'root', MemberOf => $group->id );
 ok $root && $root->id;
 
+my $test_user = RT::Test->load_or_create_user( Name => 'Test' );
+ok $test_user && $test_user->id;
+
 RT::Test->create_tickets(
     { Queue => $q, },
     { Subject => '-', },
@@ -27,14 +30,24 @@ RT::Test->create_tickets(
     { Subject => 'r', Requestor => $root->id },
     { Subject => 'c', Cc => $root->id },
     { Subject => 'a', AdminCc => $root->id },
+    { Subject => 'ag', AdminCc => $group->id },
+    { Subject => 'ru', Requestor => $test_user->id },
 );
 
 run_tests(
     'OwnerGroup = "Test"' => { '-' => 0, o => 1, r => 0, c => 0, a => 0 },
     'RequestorGroup = "Test"' => { '-' => 0, o => 0, r => 1, c => 0, a => 0 },
     'CCGroup = "Test"' => { '-' => 0, o => 0, r => 0, c => 1, a => 0 },
-    'AdminCCGroup = "Test"' => { '-' => 0, o => 0, r => 0, c => 0, a => 1 },
-    'WatcherGroup = "Test"' => { '-' => 0, o => 1, r => 1, c => 1, a => 1 },
+    'AdminCCGroup = "Test"' => { '-' => 0, o => 0, r => 0, c => 0, a => 1, ag => 1 },
+    'WatcherGroup = "Test"' => { '-' => 0, o => 1, r => 1, c => 1, a => 1, ag => 1 },
+    'Requestor.id = ' . $test_user->Id     => { '-' => 0, ru => 1 },
+    'Requestor.Name = "Test"'              => { '-' => 0, ru => 1 },
+    'AdminCc.id = ' . $group->Id           => { '-' => 0, ag => 1 },
+    'AdminCc = "Test"'                     => { '-' => 0, ag => 1 },
+    'AdminCc.Name = "Test"'                => { '-' => 0, ag => 1 },
+    'Watcher.id = ' . $test_user->Id       => { '-' => 0, ru => 1 },
+    'Watcher.id = ' . $group->Id           => { '-' => 0, ag => 1 },
+    'Watcher.Name = "Test"'                => { '-' => 0, ru => 1, ag => 1 },
 );
 
 warning_like {

-----------------------------------------------------------------------


More information about the rt-commit mailing list