[Bps-public-commit] rt-extension-rest2 branch, allow-updating-of-ticket-links, created. 1.07-1-g9e158c1

Craig Kaiser craig at bestpractical.com
Thu Jun 13 14:45:05 EDT 2019


The branch, allow-updating-of-ticket-links has been created
        at  9e158c19c7aa63f040a9e6b8e974b122f48866db (commit)

- Log -----------------------------------------------------------------
commit 9e158c19c7aa63f040a9e6b8e974b122f48866db
Author: Craig Kaiser <craig at bestpractical.com>
Date:   Thu Jun 13 14:39:05 2019 -0400

    Allow updating of ticket links
    
    Use the 'AddLink' and 'DeleteLink' keys with a link
    'type' (MemberOf DependsOn RefersTo ect) key and a
    'base' key, this can be a ticket id or a link URL.

diff --git a/lib/RT/Extension/REST2/Resource/Record/Writable.pm b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
index 6d04d3b..b53f86e 100644
--- a/lib/RT/Extension/REST2/Resource/Record/Writable.pm
+++ b/lib/RT/Extension/REST2/Resource/Record/Writable.pm
@@ -54,6 +54,7 @@ sub update_record {
 
     push @results, $self->_update_custom_fields($data->{CustomFields});
     push @results, $self->_update_role_members($data);
+    push @results, $self->_update_links($data);
     push @results, $self->_update_disabled($data->{Disabled})
       unless grep { $_ eq 'Disabled' } $self->record->WritableAttributes;
 
@@ -140,6 +141,37 @@ sub _update_custom_fields {
     return @results;
 }
 
+sub _update_links {
+    my $self = shift;
+    my $data = shift;
+
+    my $record = $self->record;
+
+    return unless $record->DOES('RT::Record::Role::Links');
+
+    my @results;
+
+    foreach my $keyword ( 'Add', 'Delete' ) {
+        my $action = $keyword . 'Link';
+        if ( $data->{$action} && ref($data->{$action}) eq 'ARRAY' ) {
+            foreach my $link ( @{ $data->{$action} } ) {
+                next
+                    unless ( $link->{type}
+                    && ( $link->{target} || $link->{base} ) );
+                my ($val, $msg) =
+                    $record->$action(
+                    Type   => $link->{type},
+                    Target => $link->{target} || undef,
+                    Base   => $link->{base} || undef
+                    );
+                push @results, $msg;
+            }
+        }
+    }
+
+    return @results;
+}
+
 sub _update_role_members {
     my $self = shift;
     my $data = shift;
diff --git a/lib/RT/Extension/REST2/Resource/Record/Writable.pm b/lib/RT/Extension/REST2/Resource/Record/Writable.pm.orig
similarity index 100%
copy from lib/RT/Extension/REST2/Resource/Record/Writable.pm
copy to lib/RT/Extension/REST2/Resource/Record/Writable.pm.orig
diff --git a/lib/RT/Extension/REST2/Resource/Record/Writable.pm.rej b/lib/RT/Extension/REST2/Resource/Record/Writable.pm.rej
new file mode 100644
index 0000000..ac8a76e
--- /dev/null
+++ b/lib/RT/Extension/REST2/Resource/Record/Writable.pm.rej
@@ -0,0 +1,16 @@
+***************
+*** 154,159 ****
+  
+      push @results, $self->_update_custom_fields($data->{CustomFields});
+      push @results, $self->_update_role_members($data);
+      push @results, $self->_update_disabled($data->{Disabled});
+  
+      # XXX TODO: Figure out how to return success/failure?  Core RT::Record's
+--- 154,160 ----
+  
+      push @results, $self->_update_custom_fields($data->{CustomFields});
+      push @results, $self->_update_role_members($data);
++     push @results, $self->_update_links($data);
+      push @results, $self->_update_disabled($data->{Disabled});
+  
+      # XXX TODO: Figure out how to return success/failure?  Core RT::Record's
diff --git a/lib/RT/Extension/REST2/Util.pm b/lib/RT/Extension/REST2/Util.pm
index e814c1c..1727724 100644
--- a/lib/RT/Extension/REST2/Util.pm
+++ b/lib/RT/Extension/REST2/Util.pm
@@ -145,6 +145,7 @@ sub deserialize_record {
     my $data   = shift;
 
     my $does_roles = $record->DOES("RT::Record::Role::Roles");
+    my $does_links = $record->DOES("RT::Record::Role::Links");
 
     # Sanitize input for the Perl API
     for my $field (sort keys %$data) {
@@ -152,6 +153,7 @@ sub deserialize_record {
 
         my $value = $data->{$field};
         next unless ref $value;
+
         if (looks_like_uid($value)) {
             # Deconstruct UIDs back into simple foreign key IDs, assuming it
             # points to the same record type (class).
@@ -167,6 +169,11 @@ sub deserialize_record {
             }
             $data->{$field} = \@members;
         }
+        elsif ( $does_links and ($field =~ /^(Add|Delete)Link$/ ) ) {
+            my @links = ref $value eq 'ARRAY'
+                ? @$value : $value;
+            $data->{$field} = \@links;
+        }
         else {
             RT->Logger->debug("Received unknown value via JSON for field $field: ".ref($value));
             delete $data->{$field};

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


More information about the Bps-public-commit mailing list