[Bps-public-commit] rt-extension-rest2 01/02: Support custom roles by name on ticket create

Jim Brandt jbrandt at bestpractical.com
Mon Aug 9 22:44:22 UTC 2021


This is an automated email from the git hooks/post-receive script.

sunnavy pushed a commit to branch custom-roles-on-create
in repository rt-extension-rest2.

commit 704bf19819fb2f25dfdeef72e6b791c0511eb077
Author: Jim Brandt <jbrandt at bestpractical.com>
AuthorDate: Fri Aug 6 11:04:04 2021 -0400

    Support custom roles by name on ticket create
    
    We previously supported custom roles on ticket create using
    syntax like RT::CustomRole-2. Add support for setting custom roles
    by name, using the same syntax as update. Using the name visible
    in the web UI is much easier for users.
---
 README                                             | 3 ++-
 lib/RT/Extension/REST2.pm                          | 3 ++-
 lib/RT/Extension/REST2/Resource/Record/Writable.pm | 9 ++++++++-
 lib/RT/Extension/REST2/Util.pm                     | 1 +
 4 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/README b/README
index 60fd423..0e7dbc7 100644
--- a/README
+++ b/README
@@ -457,10 +457,11 @@ USAGE
    Ticket Examples
     Below are some examples using the endpoints above.
 
-        # Create a ticket, setting some custom fields
+        # Create a ticket, setting some custom fields and a custom role
         curl -X POST -H "Content-Type: application/json" -u 'root:password'
             -d '{ "Queue": "General", "Subject": "Create ticket test",
                 "Requestor": "user1 at example.com", "Cc": "user2 at example.com",
+                "CustomRoles": {"My Role": "staff1 at example.com"},
                 "Content": "Testing a create",
                 "CustomFields": {"Severity": "Low"}}'
             'https://myrt.com/REST/2.0/ticket'
diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index b60494c..025a46e 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -511,10 +511,11 @@ curl for SSL like --cacert.
 
 Below are some examples using the endpoints above.
 
-    # Create a ticket, setting some custom fields
+    # Create a ticket, setting some custom fields and a custom role
     curl -X POST -H "Content-Type: application/json" -u 'root:password'
         -d '{ "Queue": "General", "Subject": "Create ticket test",
             "Requestor": "user1 at example.com", "Cc": "user2 at example.com",
+            "CustomRoles": {"My Role": "staff1 at example.com"},
             "Content": "Testing a create",
             "CustomFields": {"Severity": "Low"}}'
         'https://myrt.com/REST/2.0/ticket'
diff --git a/lib/RT/Extension/REST2/Resource/Record/Writable.pm b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
index d8e7068..aa8d8a4 100644
--- a/lib/RT/Extension/REST2/Resource/Record/Writable.pm
+++ b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
@@ -5,7 +5,7 @@ use warnings;
 use Moose::Role;
 use namespace::autoclean;
 use JSON ();
-use RT::Extension::REST2::Util qw( deserialize_record error_as_json expand_uid update_custom_fields update_role_members);
+use RT::Extension::REST2::Util qw( deserialize_record error_as_json expand_uid update_custom_fields update_role_members fix_custom_role_ids );
 use List::MoreUtils 'uniq';
 
 with 'RT::Extension::REST2::Resource::Role::RequestBodyIsJSON'
@@ -253,6 +253,13 @@ sub create_record {
         }
     }
 
+    if ( $args{CustomRoles} ) {
+        # RT::Ticket::Create wants custom role IDs (like RT::CustomRole-ID)
+        # rather than role names.
+        %args = ( %args, %{ fix_custom_role_ids( $record, delete $args{CustomRoles} ) } );
+    }
+
+
     my $method = $record->isa('RT::Group') ? 'CreateUserDefinedGroup' : 'Create';
     my ($ok, @rest) = $record->$method(%args);
 
diff --git a/lib/RT/Extension/REST2/Util.pm b/lib/RT/Extension/REST2/Util.pm
index 429901a..4b6d1d3 100644
--- a/lib/RT/Extension/REST2/Util.pm
+++ b/lib/RT/Extension/REST2/Util.pm
@@ -176,6 +176,7 @@ sub deserialize_record {
     # Sanitize input for the Perl API
     for my $field (sort keys %$data) {
         next if $field eq 'CustomFields';
+        next if $field eq 'CustomRoles';
 
         my $value = $data->{$field};
         next unless ref $value;

-- 
To stop receiving notification emails like this one, please contact
sysadmin at bestpractical.com.


More information about the Bps-public-commit mailing list