[Bps-public-commit] rt-extension-rest2 branch, update-custom-roles-on-correspond-and-comment, updated. 1.09-11-gc9dff1c

Dianne Skoll dianne at bestpractical.com
Thu Dec 31 09:17:39 EST 2020


The branch, update-custom-roles-on-correspond-and-comment has been updated
       via  c9dff1c36d78913b19209439934fdc7829cd11fb (commit)
       via  9afd06e94dbbff6f4b21602aa660ea8129ba95e6 (commit)
       via  d6b8b823982b7e3d8f5ed32fc906ef42e244cab3 (commit)
      from  5241bba915dd66946408c14cc3768ec2c8d2c733 (commit)

Summary of changes:
 lib/RT/Extension/REST2.pm                  | 21 ++++++++++++++----
 lib/RT/Extension/REST2/Resource/Message.pm | 18 ++-------------
 lib/RT/Extension/REST2/Test.pm.in          |  1 +
 lib/RT/Extension/REST2/Util.pm             | 35 ++++++++++++++++++++++++++++++
 xt/ticket-correspond-update-customroles.t  | 16 ++++++++++++++
 5 files changed, 71 insertions(+), 20 deletions(-)

- Log -----------------------------------------------------------------
commit d6b8b823982b7e3d8f5ed32fc906ef42e244cab3
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Thu Dec 31 08:58:13 2020 -0500

    Move _fix_custom_role_ids out of ... Resource::Message.pm into ... Util.pm
    
    This will allow it to be re-used in other places such as a ticket update.

diff --git a/lib/RT/Extension/REST2/Resource/Message.pm b/lib/RT/Extension/REST2/Resource/Message.pm
index 8422274..4ab4a48 100644
--- a/lib/RT/Extension/REST2/Resource/Message.pm
+++ b/lib/RT/Extension/REST2/Resource/Message.pm
@@ -7,7 +7,7 @@ use namespace::autoclean;
 use MIME::Base64;
 
 extends 'RT::Extension::REST2::Resource';
-use RT::Extension::REST2::Util qw( error_as_json update_custom_fields update_role_members);
+use RT::Extension::REST2::Util qw( error_as_json update_custom_fields update_role_members fix_custom_role_ids);
 
 sub dispatch_rules {
     Path::Dispatcher::Rule::Regex->new(
@@ -153,7 +153,7 @@ sub add_message {
 
     # update_role_members wants custom role IDs (like RT::CustomRole-ID)
     # rather than role names.
-    my $renamed_custom_roles = $self->_fix_custom_role_ids($args{CustomRoles});
+    my $renamed_custom_roles = fix_custom_role_ids($self->record, $args{CustomRoles});
     push @results, update_role_members($self->record, $renamed_custom_roles);
     push @results, $self->_update_txn_custom_fields( $TransObj, $args{TxnCustomFields} || $args{TransactionCustomFields} );
 
@@ -163,20 +163,6 @@ sub add_message {
     return 1;
 }
 
-sub _fix_custom_role_ids
-{
-    my ($self, $custom_roles) = @_;
-    my $ret = {};
-    return $ret unless $custom_roles;
-
-    foreach my $key (keys(%$custom_roles)) {
-        my $cr = RT::CustomRole->new($self->record->CurrentUser);
-        next unless $cr->Load($key);
-        $ret->{'RT::CustomRole-' . $cr->Id} = $custom_roles->{$key};
-    }
-    return $ret;
-}
-
 sub _update_txn_custom_fields {
     my $self = shift;
     my $TransObj = shift;
diff --git a/lib/RT/Extension/REST2/Util.pm b/lib/RT/Extension/REST2/Util.pm
index e20f9b7..55e05f8 100644
--- a/lib/RT/Extension/REST2/Util.pm
+++ b/lib/RT/Extension/REST2/Util.pm
@@ -22,6 +22,7 @@ use Sub::Exporter -setup => {
         format_datetime
         update_custom_fields
         update_role_members
+        fix_custom_role_ids
     ]]
 };
 
@@ -513,4 +514,38 @@ sub update_role_members {
     return @results;
 }
 
+=head2 fix_custom_role_ids ( $record, $custom_roles )
+
+$record is the RT object (eg, an RT::Ticket) associated
+with custom roles.
+
+$custom_roles is a hashref where the keys are custom role
+IDs, names or email addresses and the values can be
+anything.  Returns a new hashref where all the keys
+are replaced with "RT::CustomRole-ID" if they were
+not originally in that form, and the values are kept
+the same.
+
+=cut
+
+sub fix_custom_role_ids
+{
+    my ($record, $custom_roles) = @_;
+    my $ret = {};
+    return $ret unless $custom_roles;
+
+    foreach my $key (keys(%$custom_roles)) {
+        if ($key =~ /^RT::CustomRole-\d+$/) {
+            # Already in the correct form
+            $ret->{$key} = $custom_roles->{$key};
+            next;
+        }
+
+        my $cr = RT::CustomRole->new($record->CurrentUser);
+        next unless $cr->Load($key);
+        $ret->{'RT::CustomRole-' . $cr->Id} = $custom_roles->{$key};
+    }
+    return $ret;
+}
+
 1;

commit 9afd06e94dbbff6f4b21602aa660ea8129ba95e6
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Thu Dec 31 09:08:18 2020 -0500

    Update tests to verify that we can change a custom role by username as well as email address.

diff --git a/lib/RT/Extension/REST2/Test.pm.in b/lib/RT/Extension/REST2/Test.pm.in
index 3ff37c7..fb1cdaa 100644
--- a/lib/RT/Extension/REST2/Test.pm.in
+++ b/lib/RT/Extension/REST2/Test.pm.in
@@ -46,6 +46,7 @@ sub mech { RT::Extension::REST2::Test::Mechanize->new }
         $u->Create(
             Name => 'test',
             Password => 'password',
+            EmailAddress => 'test at rt.example',
             Privileged => 1,
         );
         return $u;
diff --git a/xt/ticket-correspond-update-customroles.t b/xt/ticket-correspond-update-customroles.t
index a1e7c9c..f3462b7 100644
--- a/xt/ticket-correspond-update-customroles.t
+++ b/xt/ticket-correspond-update-customroles.t
@@ -218,6 +218,22 @@ my ($ticket_url, $ticket_id);
     is($ticket->RoleAddresses("RT::CustomRole-$single_id"), 'abacus at example.com',
        "Single Member role changed to first member of array");
 
+    # Try using a username instead of password
+    $res = $mech->post_json($correspond_url,
+        {
+            Content => 'Hello from hypermedia!',
+            ContentType => 'text/plain',
+            CustomRoles => {
+                'Single Member' => 'test',
+            },
+        },
+        'Authorization' => $auth,
+    );
+    is($res->code, 201);
+    $content = $mech->json_response;
+    cmp_deeply($content, ['Correspondence added', 'Single Member changed from abacus at example.com to test'], "Got expected respose");
+    is($ticket->RoleAddresses("RT::CustomRole-$single_id"), 'test at rt.example',
+       "Single Member role changed");
 }
 
 done_testing;

commit c9dff1c36d78913b19209439934fdc7829cd11fb
Author: Dianne Skoll <dianne at bestpractical.com>
Date:   Thu Dec 31 09:13:37 2020 -0500

    Update documentation: Document CustomFields and TxnCustomFields; note that CustomRoles can take user names as well as email addresses.

diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index 1cd1039..69245f9 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -274,12 +274,25 @@ The time, in minutes, you've taken to work on your response/comment, optional.
 A hash whose keys are custom role names and values are as described below:
 
 For a single-value custom role, the value must be a string representing an
-email address; the custom role is set to that address.
+email address or user name; the custom role is set to the user with
+that email address or user name.
 
 For a multi-value custom role, the value can be a string representing
-an email address or can be an array of email addresses; in either
-case, the members of the custom role are set to the email address or
-addresses supplied.
+an email address or user name, or can be an array of email addresses
+or user names; in either case, the members of the custom role are set
+to the corresponding users.
+
+=item C<CustomFields>
+
+A hash similar to the C<CustomRoles> hash, but whose keys are custom
+field names that apply to the Ticket; those fields are set to the
+supplied values.
+
+=item C<TxnCustomFields>
+
+A hash similar to the C<CustomRoles> hash, but whose keys are custom
+field names that apply to the Transaction; those fields are set
+to the supplied values.
 
 =back
 

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


More information about the Bps-public-commit mailing list