[Rt-commit] rt branch, 5.0/rest2-collection-role-fields, created. rt-5.0.1-22-g91e996c28e

? sunnavy sunnavy at bestpractical.com
Thu May 27 11:07:19 EDT 2021


The branch, 5.0/rest2-collection-role-fields has been created
        at  91e996c28e167829186aeece72412478c10653b7 (commit)

- Log -----------------------------------------------------------------
commit 3906e946db20bcddbad18fa987e8510f66115b77
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 04:55:38 2021 +0800

    Support core roles for ticket search result in REST2

diff --git a/lib/RT/REST2/Resource/Tickets.pm b/lib/RT/REST2/Resource/Tickets.pm
index 3bdccaf8cd..b8060935e5 100644
--- a/lib/RT/REST2/Resource/Tickets.pm
+++ b/lib/RT/REST2/Resource/Tickets.pm
@@ -64,7 +64,7 @@ sub dispatch_rules {
 }
 
 use Encode qw( decode_utf8 );
-use RT::REST2::Util qw( error_as_json );
+use RT::REST2::Util qw( error_as_json expand_uid );
 use RT::Search::Simple;
 
 has 'query' => (
@@ -114,6 +114,25 @@ sub limit_collection {
     return 1;
 }
 
+sub expand_field {
+    my $self         = shift;
+    my $item         = shift;
+    my $field        = shift;
+    my $param_prefix = shift;
+    if ( $field =~ /^(Requestor|AdminCc|Cc)/ ) {
+        my $role    = $1;
+        my $members = [];
+        if ( my $group = $item->RoleGroup($role) ) {
+            my $gms = $group->MembersObj;
+            while ( my $gm = $gms->Next ) {
+                push @$members, expand_uid( $gm->MemberObj->Object->UID );
+            }
+        }
+        return $members;
+    }
+    return $self->SUPER::expand_field( $item, $field, $param_prefix );
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;

commit 1877ce95e23ec1d4cde128a3ded60934bfc46c28
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 05:33:57 2021 +0800

    Test core role fields for ticket search result in REST2

diff --git a/t/rest2/tickets.t b/t/rest2/tickets.t
index 1fdce910f2..d6676c21a6 100644
--- a/t/rest2/tickets.t
+++ b/t/rest2/tickets.t
@@ -712,4 +712,34 @@ my $json = JSON->new->utf8;
     is( $value->LargeContent, $img_content, 'image file content');
 }
 
+# Ticket Search - Role Fields
+{
+
+    my $payload = {
+        Subject   => 'Ticket creation using REST',
+        Queue     => 'General',
+        Content   => 'Testing ticket creation using REST API.',
+        Requestor => 'alice at example.com',
+        Cc        => 'alice at example.com, bob at example.com',
+        AdminCc   => 'root at example.com',
+    };
+
+    my $res = $mech->post_json( "$rest_base_path/ticket", $payload, 'Authorization' => $auth, );
+    is( $res->code, 201 );
+    ok( my $ticket_url = $res->header('location') );
+    ok( my ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
+
+    $res = $mech->get( "$rest_base_path/tickets?query=id=$ticket_id&fields=Requestor,Cc,AdminCc",
+        'Authorization' => $auth, );
+    is( $res->code, 200 );
+    my $content = $mech->json_response;
+    is( scalar @{ $content->{items} }, 1 );
+
+    my $ticket = $content->{items}->[0];
+    is( $ticket->{Requestor}[0]{id}, 'alice at example.com', 'Requestor id in search result' );
+    is( $ticket->{Cc}[0]{id},        'alice at example.com', 'Cc id in search result' );
+    is( $ticket->{Cc}[1]{id},        'bob at example.com',   'Cc id in search result' );
+    is( $ticket->{AdminCc}[0]{id},   'root at example.com',  'AdminCc id in search result' );
+}
+
 done_testing;

commit cc30db0aec5e66f35c7998273e370c5427756453
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 04:58:44 2021 +0800

    Support core roles for asset search result in REST2

diff --git a/lib/RT/REST2/Resource/Assets.pm b/lib/RT/REST2/Resource/Assets.pm
index 0b988d05ed..5c32dac76d 100644
--- a/lib/RT/REST2/Resource/Assets.pm
+++ b/lib/RT/REST2/Resource/Assets.pm
@@ -63,6 +63,28 @@ sub dispatch_rules {
     )
 }
 
+use RT::REST2::Util qw( expand_uid );
+
+sub expand_field {
+    my $self         = shift;
+    my $item         = shift;
+    my $field        = shift;
+    my $param_prefix = shift;
+    if ( $field =~ /^(Owner|HeldBy|Contact)/ ) {
+        my $role    = $1;
+        my $members = [];
+        if ( my $group = $item->RoleGroup($role) ) {
+            my $gms = $group->MembersObj;
+            while ( my $gm = $gms->Next ) {
+                push @$members, expand_uid( $gm->MemberObj->Object->UID );
+            }
+            $members = shift @$members if $group->SingleMemberRoleGroup;
+        }
+        return $members;
+    }
+    return $self->SUPER::expand_field( $item, $field, $param_prefix );
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;

commit e9cdc843efc267e029d7bf0a0f93575414e6499c
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 05:48:50 2021 +0800

    Test core role fields for asset search result in REST2

diff --git a/t/rest2/assets.t b/t/rest2/assets.t
index 1f1712bfed..0c47374aac 100644
--- a/t/rest2/assets.t
+++ b/t/rest2/assets.t
@@ -253,4 +253,38 @@ my ($asset_url, $asset_id);
     }
 }
 
+# Asset Search - Role Fields
+{
+
+    my $payload = {
+        Name        => 'Asset creation using REST',
+        Description => 'Asset description',
+        Catalog     => 'General assets',
+        Content     => 'Testing asset creation using REST API.',
+        Owner       => 'root at localhost',
+        HeldBy      => 'root at example.com',
+        Contact     => 'alice at example.com, bob at example.com',
+    };
+
+    my $res = $mech->post_json( "$rest_base_path/asset", $payload, 'Authorization' => $auth, );
+    is( $res->code, 201 );
+    ok( my $asset_url = $res->header('location') );
+    ok( my ($asset_id) = $asset_url =~ qr[/asset/(\d+)] );
+
+    $res = $mech->post_json(
+        "$rest_base_path/assets?fields=Owner,HeldBy,Contact",
+        [ { field => 'id', operator => '=', value => $asset_id } ],
+        'Authorization' => $auth,
+    );
+    is( $res->code, 200 );
+    my $content = $mech->json_response;
+    is( scalar @{ $content->{items} }, 1 );
+
+    my $asset = $content->{items}->[0];
+    is( $asset->{Owner}{id},      'root',              'Owner id in search result' );
+    is( $asset->{HeldBy}[0]{id},  'root at example.com',  'HeldBy id in search result' );
+    is( $asset->{Contact}[0]{id}, 'alice at example.com', 'Contact id in search result' );
+    is( $asset->{Contact}[1]{id}, 'bob at example.com',   'Contact id in search result' );
+}
+
 done_testing;

commit 0dc1ba6ee3d871463decfa628ecd80c9383ea2c9
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 04:59:08 2021 +0800

    Support custom roles for search result in REST2

diff --git a/lib/RT/REST2/Resource.pm b/lib/RT/REST2/Resource.pm
index 1159ba7802..1472d57b1d 100644
--- a/lib/RT/REST2/Resource.pm
+++ b/lib/RT/REST2/Resource.pm
@@ -110,6 +110,49 @@ sub expand_field {
 
             push @{ $result }, values %values if %values;
         }
+    } elsif ($field eq 'CustomRoles') {
+        if ( $item->DOES("RT::Record::Role::Roles") ) {
+            my %data;
+            for my $role ( $item->Roles( ACLOnly => 0 ) ) {
+                next unless $role =~ /^RT::CustomRole-/;
+                $data{$role} = [];
+
+                my $group = $item->RoleGroup($role);
+                if ( !$group->Id ) {
+                    $data{$role} = expand_uid( RT->Nobody->UserObj->UID ) if $item->_ROLES->{$role}{Single};
+                    next;
+                }
+
+                my $gms = $group->MembersObj;
+                while ( my $gm = $gms->Next ) {
+                    push @{ $data{$role} }, expand_uid( $gm->MemberObj->Object->UID );
+                }
+
+                # Avoid the extra array ref for single member roles
+                $data{$role} = shift @{$data{$role}} if $group->SingleMemberRoleGroup;
+            }
+            return \%data;
+        }
+
+    } elsif ($field =~ /^RT::CustomRole-\d+$/) {
+        if ( $item->DOES("RT::Record::Role::Roles") ) {
+            my $result = [];
+
+            my $group = $item->RoleGroup($field);
+            if ( !$group->Id ) {
+                $result = expand_uid( RT->Nobody->UserObj->UID ) if $item->_ROLES->{$field}{Single};
+                next;
+            }
+
+            my $gms = $group->MembersObj;
+            while ( my $gm = $gms->Next ) {
+                push @$result, expand_uid( $gm->MemberObj->Object->UID );
+            }
+
+            # Avoid the extra array ref for single member roles
+            $result = shift @$result if $group->SingleMemberRoleGroup;
+            return $result;
+        }
     } elsif ($field eq 'ContentLength' && $item->can('ContentLength')) {
         $result = $item->ContentLength;
     } elsif ($item->can('_Accessible') && $item->_Accessible($field => 'read')) {

commit ae8fa4fdbed55bc073facd72aecf5455c09d20af
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 06:00:59 2021 +0800

    Test custom role fields for ticket search result in REST2

diff --git a/t/rest2/ticket-customroles.t b/t/rest2/ticket-customroles.t
index 0433911ea6..40d03bcf24 100644
--- a/t/rest2/ticket-customroles.t
+++ b/t/rest2/ticket-customroles.t
@@ -563,5 +563,45 @@ $user->PrincipalObj->GrantRight( Right => $_ )
     }, 'Later Single Member is Nobody');
 }
 
+# Ticket Search
+{
+
+    my $payload = {
+        Subject            => 'Ticket creation using REST',
+        Queue              => 'General',
+        Content            => 'Testing ticket creation using REST API.',
+        $single->GroupType => 'single2 at example.com',
+        $multi->GroupType  => 'multi at example.com, multi2 at example.com',
+    };
+
+    my $res = $mech->post_json( "$rest_base_path/ticket", $payload, 'Authorization' => $auth, );
+    is( $res->code, 201 );
+    ok( my $ticket_url = $res->header('location') );
+    ok( my ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
+
+    $res = $mech->get(
+        "$rest_base_path/tickets?query=id=$ticket_id&fields=" . join( ',', $single->GroupType, $multi->GroupType ),
+        'Authorization' => $auth,
+    );
+    is( $res->code, 200 );
+    my $content = $mech->json_response;
+    is( scalar @{ $content->{items} }, 1 );
+
+    my $ticket = $content->{items}->[0];
+    is( $ticket->{ $single->GroupType }{id},   'single2 at example.com', 'Single Member id in search result' );
+    is( $ticket->{ $multi->GroupType }[0]{id}, 'multi at example.com',   'Multi Member id in search result' );
+    is( $ticket->{ $multi->GroupType }[1]{id}, 'multi2 at example.com',  'Multi Member id in search result' );
+
+    $res = $mech->get( "$rest_base_path/tickets?query=id=$ticket_id&fields=CustomRoles", 'Authorization' => $auth, );
+    is( $res->code, 200 );
+    $content = $mech->json_response;
+    is( scalar @{ $content->{items} }, 1 );
+
+    $ticket = $content->{items}->[0];
+    is( $ticket->{CustomRoles}{ $single->GroupType }{id}, 'single2 at example.com',  'Single Member id in search result' );
+    is( $ticket->{CustomRoles}{ $multi->GroupType }[0]{id}, 'multi at example.com',  'Multi Member id in search result' );
+    is( $ticket->{CustomRoles}{ $multi->GroupType }[1]{id}, 'multi2 at example.com', 'Multi Member id in search result' );
+}
+
 done_testing;
 

commit 994e4c06e9b7817277e71f94725939df3dfc7dfe
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 07:05:33 2021 +0800

    Fix typo in REST2 doc

diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
index 2ea9fb3201..2b4dc9a3b1 100644
--- a/lib/RT/REST2.pm
+++ b/lib/RT/REST2.pm
@@ -1229,7 +1229,7 @@ example):
                  "name" : "My Custom Field",
                  "values" : [
                      "CustomField value"
-                 },
+                 ]
              }
          ]
       }

commit 91e996c28e167829186aeece72412478c10653b7
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 06:41:01 2021 +0800

    Document role fields for ticket search result

diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
index 2b4dc9a3b1..80971fb284 100644
--- a/lib/RT/REST2.pm
+++ b/lib/RT/REST2.pm
@@ -1192,14 +1192,14 @@ You can use additional fields parameters to expand child blocks, for
 example (line wrapping inserted for readability):
 
     XX_RT_URL_XX/REST/2.0/tickets
-      ?fields=Owner,Status,Created,Subject,Queue,CustomFields
+      ?fields=Owner,Status,Created,Subject,Queue,CustomFields,Requestor,Cc,AdminCc,RT::CustomRole-1
       &fields[Queue]=Name,Description
 
 Says that in the result set for tickets, the extra fields for Owner, Status,
-Created, Subject, Queue and CustomFields should be included. But in
-addition, for the Queue block, also include Name and Description. The
-results would be similar to this (only one ticket is displayed in this
-example):
+Created, Subject, Queue, CustomFields, Requestor, Cc, AdminCc and
+CustomRoles should be included. But in addition, for the Queue block, also
+include Name and Description. The results would be similar to this (only one
+ticket is displayed in this example):
 
    "items" : [
       {
@@ -1231,6 +1231,28 @@ example):
                      "CustomField value"
                  ]
              }
+         ],
+         "Requestor" : [
+            {
+               "id" : "root",
+               "type" : "user",
+               "_url" : "XX_RT_URL_XX/REST/2.0/user/root"
+            }
+         ],
+         "Cc" : [
+            {
+               "id" : "root",
+               "type" : "user",
+               "_url" : "XX_RT_URL_XX/REST/2.0/user/root"
+            }
+         ],
+         "AdminCc" : [],
+         "RT::CustomRole-1" : [
+            {
+               "_url" : "XX_RT_URL_XX/REST/2.0/user/foo at example.com",
+               "type" : "user",
+               "id" : "foo at example.com"
+            }
          ]
       }
       { … },

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


More information about the rt-commit mailing list