[Bps-public-commit] rt-extension-rest2 branch, update-custom-roles-on-correspond-and-comment, repushed
Dianne Skoll
dianne at bestpractical.com
Tue Jan 12 10:56:28 EST 2021
The branch update-custom-roles-on-correspond-and-comment was deleted and repushed:
was 4d92af9c7c29e684cb8ce7ff2cb970cdd649011d
now dafe87a413680fc576a35cd3bf56b950c72c4484
1: 6dac39c ! 1: 188a4b4 Move _update_role_members and _fix_custom_role_ids into Util.pm
@@ -1,9 +1,32 @@
Author: Dianne Skoll <dianne at bestpractical.com>
- Move _update_role_members out of RT::Extension::REST2::Resource::Record::Writable and into RT::Extension::REST2::Util
+ Move _update_role_members and _fix_custom_role_ids into Util.pm
- This makes it reusable from other objects that might not be
- a ... REST2::Resource::Record::Writable.
+ This will allow them 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
+--- a/lib/RT/Extension/REST2/Resource/Message.pm
++++ b/lib/RT/Extension/REST2/Resource/Message.pm
+@@
+ use MIME::Base64;
+
+ extends 'RT::Extension::REST2::Resource';
++
+ use RT::Extension::REST2::Util qw( error_as_json update_custom_fields );
+
+ sub dispatch_rules {
+@@
+
+ push @results, $msg;
+ push @results, update_custom_fields($self->record, $args{CustomFields});
+- push @results, $self->_update_txn_custom_fields( $TransObj, $args{TxnCustomFields} || $args{TransactionCustomFields} );
++ # update_role_members wants custom role IDs (like RT::CustomRole-ID)
++ # rather than role names.
++ my $renamed_custom_roles = fix_custom_role_ids($self->record, $args{CustomRoles});
++ push @results, update_role_members($self->record, $renamed_custom_roles);
+
+ # Set ticket status if we were passed a "Status":"foo" argument
+ if ($args{Status}) {
diff --git a/lib/RT/Extension/REST2/Resource/Record/Writable.pm b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
--- a/lib/RT/Extension/REST2/Resource/Record/Writable.pm
@@ -160,6 +183,7 @@
format_datetime
update_custom_fields
+ update_role_members
++ fix_custom_role_ids
]]
};
@@ -282,6 +306,40 @@
+
+ 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;
2: 8c7fc47 < -: ------- Move _fix_custom_role_ids out of ... Resource::Message.pm into ... Util.pm
3: f6b8073 < -: ------- Allow updating of custom roles on ticket comment/correspond.
-: ------- > 2: ebfafbf Allow updating of custom roles on ticket comment/correspond.
4: 8a3cc6f ! 3: 59219eb Document CustomFields, TxnCustomFields and CustomRoles data members.
@@ -1,22 +1,23 @@
Author: Dianne Skoll <dianne at bestpractical.com>
- Update documentation: Document CustomFields and TxnCustomFields; note that CustomRoles can take user names as well as email addresses.
+ Document CustomFields, TxnCustomFields and CustomRoles data members.
diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@
- A hash whose keys are custom role names and values are as described below:
+ ticket to. The Status value must be a valid status based on the
+ lifecycle of the ticket's current queue.
- For a single-value custom role, the value must be a string representing an
--email address; the custom role is set to that address.
++=item C<CustomRoles>
++
++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 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.
++
++For a multi-value custom role, the value can be a string representing
+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.
@@ -32,7 +33,8 @@
+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
+ =head3 Add Attachments
5: 1bd31c1 ! 4: dafe87a Add unit test for updating custom roles on ticket comment/correspond.
@@ -1,6 +1,18 @@
Author: Dianne Skoll <dianne at bestpractical.com>
Add unit test for updating custom roles on ticket comment/correspond.
+
+diff --git a/lib/RT/Extension/REST2/Test.pm.in b/lib/RT/Extension/REST2/Test.pm.in
+--- a/lib/RT/Extension/REST2/Test.pm.in
++++ b/lib/RT/Extension/REST2/Test.pm.in
+@@
+ $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
new file mode 100644
@@ -227,7 +239,22 @@
+ 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;
-
6: 4d92af9 < -: ------- Update tests to verify that we can change a custom role by username as well as email address.
More information about the Bps-public-commit
mailing list