[Bps-public-commit] rt-extension-rest2 branch, collection-role-fields, created. 1.12-8-g878ba58

? sunnavy sunnavy at bestpractical.com
Thu May 27 11:15:39 EDT 2021


The branch, collection-role-fields has been created
        at  878ba58dcfc0570aade26fee011f978eb5cbcaaa (commit)

- Log -----------------------------------------------------------------
commit 57e03e566326c53c56da340f71cf563d6881df41
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/Extension/REST2/Resource/Tickets.pm b/lib/RT/Extension/REST2/Resource/Tickets.pm
index ee8eb58..8abbc7d 100644
--- a/lib/RT/Extension/REST2/Resource/Tickets.pm
+++ b/lib/RT/Extension/REST2/Resource/Tickets.pm
@@ -16,7 +16,7 @@ sub dispatch_rules {
 }
 
 use Encode qw( decode_utf8 );
-use RT::Extension::REST2::Util qw( error_as_json );
+use RT::Extension::REST2::Util qw( error_as_json expand_uid );
 use RT::Search::Simple;
 
 has 'query' => (
@@ -66,6 +66,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 eb942290e4ab3548f23e151adfd8fa3fe79340fd
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/xt/tickets.t b/xt/tickets.t
index 6008d00..a841de0 100644
--- a/xt/tickets.t
+++ b/xt/tickets.t
@@ -615,4 +615,34 @@ my ($ticket_url, $ticket_id);
     like($third_ticket->{_url}, qr{$rest_base_path/ticket/$ticket3_id$});
 }
 
+# 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 8866c01f1b6ce3a13fb7952c25eb0cd3805cc18b
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/Extension/REST2/Resource/Assets.pm b/lib/RT/Extension/REST2/Resource/Assets.pm
index 5649ddb..5144833 100644
--- a/lib/RT/Extension/REST2/Resource/Assets.pm
+++ b/lib/RT/Extension/REST2/Resource/Assets.pm
@@ -15,6 +15,28 @@ sub dispatch_rules {
     )
 }
 
+use RT::Extension::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 0f4488b3d8074c46459092363c44fde0044514d5
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/xt/assets.t b/xt/assets.t
index 088ea7a..2225119 100644
--- a/xt/assets.t
+++ b/xt/assets.t
@@ -252,4 +252,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 b4cc6c19394562823ba6efdf89bdad3bb364b7f8
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/Extension/REST2/Resource.pm b/lib/RT/Extension/REST2/Resource.pm
index e420de3..594f5c9 100644
--- a/lib/RT/Extension/REST2/Resource.pm
+++ b/lib/RT/Extension/REST2/Resource.pm
@@ -62,7 +62,48 @@ 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 ($item->can('_Accessible') && $item->_Accessible($field => 'read')) {
         # RT::Record derived object, so we can check access permissions.
 

commit 9c833eb7b8026e5ce2cc3d5e713a70bb725d73a7
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/xt/ticket-customroles.t b/xt/ticket-customroles.t
index ace93f4..4b8b2e4 100644
--- a/xt/ticket-customroles.t
+++ b/xt/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 dbc71d96ed74489eb6902dab0fd199136eeb821b
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/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index fdf5d66..a18a101 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -1142,7 +1142,7 @@ example):
                  "name" : "My Custom Field",
                  "values" : [
                      "CustomField value"
-                 },
+                 ]
              }
          ]
       }

commit 878ba58dcfc0570aade26fee011f978eb5cbcaaa
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/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index a18a101..3d07d64 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -1105,14 +1105,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" : [
       {
@@ -1144,6 +1144,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 Bps-public-commit mailing list