[Rt-commit] [rtir] 01/04: Refactor IP copying to be generic CF copying.

Kevin Falcone falcone at bestpractical.com
Fri Aug 15 14:19:52 EDT 2014


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

falcone pushed a commit to branch 3.2/merge-ips-on-link
in repository rtir.

commit e36cfe719b42a0f0c649531095d6efb62cfe96b8
Author: Kevin Falcone <falcone at bestpractical.com>
Date:   Wed Aug 13 22:27:27 2014 -0400

    Refactor IP copying to be generic CF copying.
    
    Adds a bunch of error checking, both on arguments as well as ensuring
    that the custom field you're copying between tickets is available on
    both tickets.  Retains the HasValue checking from the original code, so
    you only attempt to add values that are missing on that target.
---
 lib/RT/Action/RTIR.pm          | 50 ++++++++++++++++++++++++++++++++++++++++++
 lib/RT/Action/RTIR_MergeIPs.pm | 15 +------------
 2 files changed, 51 insertions(+), 14 deletions(-)

diff --git a/lib/RT/Action/RTIR.pm b/lib/RT/Action/RTIR.pm
index badb08b..61607ec 100644
--- a/lib/RT/Action/RTIR.pm
+++ b/lib/RT/Action/RTIR.pm
@@ -67,6 +67,56 @@ sub CreatorCurrentUser {
     return $user;
 }
 
+sub CopyCustomFields {
+    my ($self, %args) = @_;
+
+    for my $ticket (qw/From To/) {
+        unless ( ref $args{$ticket} eq 'RT::Ticket' && $args{$ticket}->Id ) {
+            RT->Logger->error("Argument $ticket isn't a ticket");
+            return;
+        }
+    }
+
+    unless ( defined $args{CF} ) {
+        RT->Logger->error("Must pass a CF");
+        return;
+    }
+
+    my $target = $args{To};
+    my $source = $args{From};
+
+    my $cf_name = $args{CF};
+
+    my $target_cf = $target->LoadCustomFieldByIdentifier( $cf_name );
+    unless ( $target_cf->Id ) {
+        RT->Logger->error("Couldn't load CF $cf_name on ticket ".$target->Id);
+        return;
+    }
+    my $has_values = $target->CustomFieldValues( $target_cf );
+
+    my $source_cf = $source->LoadCustomFieldByIdentifier( $cf_name );
+    unless ( $source_cf->Id ) {
+        RT->Logger->error("Couldn't load CF $cf_name on ticket ".$source->Id);
+        return;
+    }
+    my $add_values = $source->CustomFieldValues( 'IP' );
+
+    while ( my $value = $add_values->Next ) {
+        my $content = $value->Content;
+        next if $has_values->HasEntry( $content );
+
+        my ($status, $msg) = $target->AddCustomFieldValue(
+            Value => $content,
+            Field => $target_cf,
+        );
+        RT->Logger->error("Couldn't add $cf_name: $msg")
+            unless $status;
+    }
+
+    return 1;
+
+}
+
 RT::Base->_ImportOverlays;
 
 1;
diff --git a/lib/RT/Action/RTIR_MergeIPs.pm b/lib/RT/Action/RTIR_MergeIPs.pm
index 711ee43..031a280 100644
--- a/lib/RT/Action/RTIR_MergeIPs.pm
+++ b/lib/RT/Action/RTIR_MergeIPs.pm
@@ -74,24 +74,11 @@ sub Commit {
     my $target = $uri_obj->Object;
     return 1 if $target->id eq $txn->ObjectId;
 
-    my $has_values = $target->CustomFieldValues( 'IP' );
-
     my $source = RT::Ticket->new( $self->CurrentUser );
     $source->LoadById( $txn->ObjectId );
-    my $add_values = $source->CustomFieldValues( 'IP' );
-    while ( my $value = $add_values->Next ) {
-        my $ip = $value->Content;
-        next if $has_values->HasEntry( $ip );
 
-        my ($status, $msg) = $target->AddCustomFieldValue(
-            Value => $ip,
-            Field => 'IP',
-        );
-        RT->Logger->error("Couldn't add IP address: $msg")
-            unless $status;
-    }
+    return $self->CopyCustomFields( To => $target, From => $source, CF => 'IP' );
 
-    return 1;
 }
 
 RT::Base->_ImportOverlays;

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the rt-commit mailing list