[Rt-commit] rt branch 5.0/dont-insert-duplicate-cfs-on-queue created. rt-5.0.3-237-g0063dcedda

BPS Git Server git at git.bestpractical.com
Mon Feb 6 19:11:38 UTC 2023


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".

The branch, 5.0/dont-insert-duplicate-cfs-on-queue has been created
        at  0063dcedda0709962aaa8b26a4cf57c0500bcad9 (commit)

- Log -----------------------------------------------------------------
commit 0063dcedda0709962aaa8b26a4cf57c0500bcad9
Author: Jason Crome <jcrome at bestpractical.com>
Date:   Mon Jan 30 16:00:20 2023 -0500

    Don't add duplicate Cfs to object when inserting data
    
    It is possible to inadvertently add a custom field to an object multiple
    times when inserting from an initialdata file. This change prevents this
    from happening.

diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index 5b63911441..2c4a4cf845 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -1343,7 +1343,48 @@ sub InsertData {
                     delete $item->{'BasedOn'};
                 }
 
-            } 
+            }
+
+            my $cf_check = RT::CustomField->new( $RT::SystemUser );
+            my( $found ) = $cf_check->LoadByName(
+                Name          => $item->{'Name'},
+                LookupType    => $item->{ LookupType },
+                IncludeGlobal => 1,
+            );
+            if( $found ) {
+                my( $is_global, $is_applied, $what );
+
+                if( $cf_check->IsGlobal ) {
+                    $is_global = 1;
+                } else {
+                    $is_applied = $cf_check->IsAddedToAny;
+                    my $thing;
+                    my $class = $cf_check->RecordClassFromLookupType( $item->{ LookupType } );
+                    if ($class) {
+                        $thing = $class->new( RT->SystemUser );
+                        $apply_to = [ $apply_to ] unless ref $apply_to;
+                        foreach my $key ( @{ $apply_to } ) {
+                            my ( $ok, $msg ) = $thing->Load( $key );
+                            if( $ok ) {
+                                my $ocf = RT::ObjectCustomField->new( RT->SystemUser );
+                                ( $is_applied ) = $ocf->LoadByCols(
+                                    CustomField => $cf_check->id,
+                                    ObjectId    => $thing->id,
+                                );
+                                if( $is_applied ) {
+                                    $what = "$class $key";
+                                    last;
+                                }
+                            }
+                        }
+                    }
+                }
+                if( $is_global or $is_applied ) {
+                    $RT::Logger->warn( "Custom field " . $item->{ Name }. " already exists" .
+                        ( $is_global ? " globally" : " for $what") . ". Skipping." );
+                    next;
+                }
+            }
 
             my ( $return, $msg ) = $new_entry->Create(%$item);
             unless( $return ) {
diff --git a/t/api/initialdata-roundtrip.t b/t/api/initialdata-roundtrip.t
index e55223acc7..b2e3c24781 100644
--- a/t/api/initialdata-roundtrip.t
+++ b/t/api/initialdata-roundtrip.t
@@ -1069,6 +1069,7 @@ for my $test (@tests) {
             qr/^Invalid value for Name$/,
             qr/^Queue already exists$/,
             qr/^Invalid Name \(names must be unique and may not be all digits\)$/,
+            qr/^Custom field .* already exists/,
         );
 
         # Avoid reporting this anonymous call frame as the source of the warning

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


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list