[Rt-commit] rt branch, 4.4/ticket-search-order-by-watcher-cfs, created. rt-4.4.4-83-g204fc28176

? sunnavy sunnavy at bestpractical.com
Tue Nov 26 16:30:12 EST 2019


The branch, 4.4/ticket-search-order-by-watcher-cfs has been created
        at  204fc28176a25279f4a49dc9d1713f1d4e8a8426 (commit)

- Log -----------------------------------------------------------------
commit d150146d18cdb6189735f557b3d3acaac8ceed31
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Nov 27 00:55:00 2019 +0800

    Support order by watcher's custom fields for ticket search

diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index e094c63ef6..15a2851fef 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1065,7 +1065,9 @@ Try and turn a custom role descriptor (e.g. C<CustomRole.{Engineer}>) into
 sub _CustomRoleDecipher {
     my ($self, $string) = @_;
 
-    my ($field, $column) = ($string =~ /^\{(.+)\}(?:\.(\w+))?$/);
+    # $column could be core fields like "EmailAddress" or CFs like
+    # "CustomField.{Department}", the CF format is used in OrderByCols.
+    my ($field, $column) = ($string =~ /^\{(.+?)\}(?:\.(.+))?$/);
 
     my $role;
 
@@ -1434,7 +1436,54 @@ sub OrderByCols {
                 $self->{_sql_u_watchers_alias_for_sort}{ $cache_key }
                     = $users = ( $self->_WatcherJoin( Name => $type, Class => "RT::" . $class ) )[2];
             }
-            push @res, { %$row, ALIAS => $users, FIELD => $column };
+
+            if ( $column =~ /^CustomField\.\{(.+)\}$/ ) {
+                my $cf_name = $1;
+                my $cf      = RT::CustomField->new( $self->CurrentUser );
+                $cf->LoadByCols( LookupType => RT::User->CustomFieldLookupType, Name => $cf_name );
+                if ( $cf->id ) {
+                    my $ocfvs = $self->NewAlias('ObjectCustomFieldValues');
+                    $self->Join(
+                        TYPE   => 'LEFT',
+                        ALIAS1 => $users,
+                        FIELD1 => 'id',
+                        ALIAS2 => $ocfvs,
+                        FIELD2 => 'ObjectId',
+                    );
+
+                    $self->Limit(
+                        LEFTJOIN        => $ocfvs,
+                        FIELD           => 'CustomField',
+                        VALUE           => $cf->id,
+                        ENTRYAGGREGATOR => 'AND',
+                    );
+
+                    # The following is copied from RT::SearchBuilder::_OrderByCF
+                    my $CFvs = $self->Join(
+                        TYPE   => 'LEFT',
+                        ALIAS1 => $ocfvs,
+                        FIELD1 => 'CustomField',
+                        TABLE2 => 'CustomFieldValues',
+                        FIELD2 => 'CustomField',
+                    );
+                    $self->Limit(
+                        LEFTJOIN        => $CFvs,
+                        FIELD           => 'Name',
+                        QUOTEVALUE      => 0,
+                        VALUE           => "$ocfvs.Content",
+                        ENTRYAGGREGATOR => 'AND'
+                    );
+                    push @res, { %$row, ALIAS => $CFvs, FIELD => 'SortOrder' },
+                        { %$row, ALIAS => $ocfvs, FIELD => 'Content' };
+                }
+                else {
+                    RT->Logger->warning("Couldn't load user custom field $cf_name");
+                    next;
+                }
+            }
+            else {
+                push @res, { %$row, ALIAS => $users, FIELD => $column };
+            }
        } elsif ( defined $meta->[0] && $meta->[0] eq 'CUSTOMFIELD' ) {
            my ($object, $field, $cf, $column) = $self->_CustomFieldDecipher( $subkey );
            my $cfkey = $cf ? $cf->id : "$object.$field";

commit 501ef560d5a7bc42b98aec08cb5667884f7aaa38
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Nov 27 00:55:45 2019 +0800

    Test order by watcher's custom fields for ticket search

diff --git a/t/ticket/sort-by-user.t b/t/ticket/sort-by-user.t
index 42f95fc258..9be9ca0a8e 100644
--- a/t/ticket/sort-by-user.t
+++ b/t/ticket/sort-by-user.t
@@ -1,5 +1,5 @@
 
-use RT::Test nodata => 1, tests => 52;
+use RT::Test nodata => 1, tests => undef;
 
 use strict;
 use warnings;
@@ -33,6 +33,7 @@ foreach my $u (qw(Z A)) {
     my $user = RT::User->new( RT->SystemUser );
     my ($uid) = $user->Create(
         Name => $name,
+        EmailAddress => $name . '@localhost',
         Privileged => 1,
     );
     ok $uid, "created user #$uid";
@@ -41,6 +42,8 @@ foreach my $u (qw(Z A)) {
     ok $status, "granted right";
     ($status, $msg) = $user->PrincipalObj->GrantRight( Right => 'CreateTicket', Object => $queue );
     ok $status, "granted right";
+    ($status, $msg) = $user->PrincipalObj->GrantRight( Right => 'SuperUser', Object => RT->System );
+    ok $status, "granted right";
 
     push @users, $user;
     push @uids, $user->id;
@@ -117,7 +120,7 @@ run_tests();
 run_tests();
 
 @data = (
-    { Subject => 'RT' },
+    { Subject => 'Nobody' },
     { Subject => 'Z', LastUpdatedBy => $uids[0] },
     { Subject => 'A', LastUpdatedBy => $uids[1] },
 );
@@ -127,4 +130,50 @@ run_tests();
 );
 run_tests();
 
+my $cf = RT::Test->load_or_create_custom_field(
+    Name  => 'Department',
+    Type  => 'FreeformSingle',
+    LookupType => RT::User->CustomFieldLookupType,
+);
+my ( $ret, $msg ) = $cf->AddToObject( RT::User->new( RT->SystemUser ) );
+ok( $ret, "Added CF globally: $msg" );
+
+( $ret, $msg ) = $users[0]->AddCustomFieldValue( Field => $cf, Value => 'Bar' );
+ok( $ret, "Added CF value 'Foo' to users[0]: $msg" );
+
+( $ret, $msg ) = $users[1]->AddCustomFieldValue( Field => $cf, Value => 'Foo' );
+ok( $ret, "Added CF value 'Bar' to users[1]: $msg" );
+
+ at data = (
+    { Subject => '-' },
+    { Subject => 'Z', Owner => $uids[1] },
+    { Subject => 'A', Owner => $uids[0] },
+);
+ at tickets = RT::Test->create_tickets( { Queue => $queue->id }, @data );
+ at test = (
+    { Order => "Owner.CustomField.{Department}" },
+);
+run_tests();
+
+my $cr = RT::CustomRole->new( RT->SystemUser );
+( $ret, $msg ) = $cr->Create(
+    Name      => 'Engineer',
+    MaxValues => 0,
+);
+ok( $ret, "Created custom role: $msg" );
+
+( $ret, $msg ) = $cr->AddToObject( ObjectId => $queue->id );
+ok( $ret, "Added CR to queue: $msg" );
+
+ at data = (
+    { Subject => '-' },
+    { Subject => 'Z', $cr->GroupType => $uids[1] },
+    { Subject => 'A', $cr->GroupType => $uids[0] },
+);
+ at tickets = RT::Test->create_tickets( { Queue => $queue->id }, @data );
+ at test = ( { Order => "CustomRole.{Engineer}.CustomField.{Department}" }, );
+run_tests();
+
 @tickets = ();
+
+done_testing;

commit 4343e70a2933b9e1533cc2b09643e8677b12fd50
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Nov 27 02:17:51 2019 +0800

    Add more watcher fields including cfs to OrderBy on search builder page
    
    Now besides user cfs, the following core fields are added: id, Name,
    EmailAddress, RealName, Organization, City and Country.
    
    The previous ambiguous "Owner" has been renamed to "Owner.Name".

diff --git a/share/html/Search/Elements/EditSort b/share/html/Search/Elements/EditSort
index 7b70b923e3..5aa6c43236 100644
--- a/share/html/Search/Elements/EditSort
+++ b/share/html/Search/Elements/EditSort
@@ -108,9 +108,19 @@ for my $field (keys %FieldDescriptions) {
     $fields{$field} = $field;
 }
 
-$fields{'Owner'} = 'Owner';
-$fields{ $_ . '.EmailAddress' } = $_ . '.EmailAddress'
-    for qw(Requestor Cc AdminCc);
+my @user_fields = qw/id Name EmailAddress Organization RealName City Country/;
+my $user_cfs    = RT::CustomFields->new( $session{CurrentUser} );
+$user_cfs->Limit( FIELD => 'LookupType', VALUE => RT::User->CustomFieldLookupType );
+while ( my $user_cf = $user_cfs->Next ) {
+    push @user_fields, join '.', 'CustomField', '{' . $user_cf->Name . '}';
+}
+
+for my $watcher (qw/Owner Requestor Cc AdminCc/) {
+    for my $user_field (@user_fields) {
+        my $field = join '.', $watcher, $user_field;
+        $fields{$field} = $field;
+    }
+}
 
 # Add all available CustomFields to the list of sortable columns.
 my @cfs = grep /^CustomField/, @{$ARGS{AvailableColumns}};
@@ -121,7 +131,10 @@ my @roles = grep /^CustomRole/, @{$ARGS{AvailableColumns}};
 for my $role (@roles) {
     my ($label) = $role =~ /^CustomRole.\{(.*)\}$/;
     my $value = $role;
-    $fields{$label . '.EmailAddress' } = $value . '.EmailAddress';
+
+    for my $user_field (@user_fields) {
+        $fields{ join '.', $label, $user_field } = join '.', $value, $user_field;
+    }
 }
 
 # Add PAW sort

commit 17f401b003636e1b65133b8ea8ff3a4e90263589
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Nov 27 02:22:14 2019 +0800

    Upgrade OrderBy "Owner" to new version "Owner.Name" in saved searchs
    
    As we added more watcher fields to OrderBy, previous ambiguous "Owner"
    has been removed.

diff --git a/etc/upgrade/4.4.5/content b/etc/upgrade/4.4.5/content
new file mode 100644
index 0000000000..3123332c46
--- /dev/null
+++ b/etc/upgrade/4.4.5/content
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+
+our @Initial = (
+    sub {
+        my $searches = RT::Attributes->new( RT->SystemUser );
+        $searches->Limit( FIELD => 'Name', VALUE => 'SavedSearch' );
+        $searches->OrderBy( FIELD => 'id' );
+
+        while ( my $search = $searches->Next ) {
+            my $content = $search->Content;
+            next unless ref $content eq 'HASH';
+
+            if ( $content->{OrderBy} ) {
+                my @order_by = split /\|/, $content->{OrderBy};
+                my @new_order_by;
+                my $changed;
+                for my $order_by (@order_by) {
+                    if ( $order_by eq 'Owner' ) {
+                        push @new_order_by, 'Owner.Name';
+                        $changed = 1;
+                    }
+                    else {
+                        push @new_order_by, $order_by;
+                    }
+                }
+                if ($changed) {
+                    $content->{OrderBy} = join '|', @new_order_by;
+                    my ( $ok, $msg ) = $search->SetContent($content);
+                    RT->Logger->error("Unable to upgrade saved chart #@{[$search->id]}: $msg")
+                        unless $ok;
+                }
+            }
+        }
+    }
+);

commit 204fc28176a25279f4a49dc9d1713f1d4e8a8426
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Nov 27 02:31:26 2019 +0800

    Update query builder tests as we added more watcher fields to order by

diff --git a/t/web/query_builder.t b/t/web/query_builder.t
index 89e89f5707..8c272fabe9 100644
--- a/t/web/query_builder.t
+++ b/t/web/query_builder.t
@@ -331,8 +331,20 @@ diag "make sure the list of columns available in the 'Order by' dropdowns are co
     $agent->get_ok($url . 'Search/Build.html');
 
     my @orderby = qw(
+        AdminCc.City
+        AdminCc.Country
         AdminCc.EmailAddress
+        AdminCc.Name
+        AdminCc.Organization
+        AdminCc.RealName
+        AdminCc.id
+        Cc.City
+        Cc.Country
         Cc.EmailAddress
+        Cc.Name
+        Cc.Organization
+        Cc.RealName
+        Cc.id
         Created
         Creator
         Custom.Ownership
@@ -341,10 +353,22 @@ diag "make sure the list of columns available in the 'Order by' dropdowns are co
         InitialPriority
         LastUpdated
         LastUpdatedBy
-        Owner
+        Owner.City
+        Owner.Country
+        Owner.EmailAddress
+        Owner.Name
+        Owner.Organization
+        Owner.RealName
+        Owner.id
         Priority
         Queue
+        Requestor.City
+        Requestor.Country
         Requestor.EmailAddress
+        Requestor.Name
+        Requestor.Organization
+        Requestor.RealName
+        Requestor.id
         Resolved
         SLA
         Started

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


More information about the rt-commit mailing list