[Rt-commit] rt branch, 4.4-trunk, updated. rt-4.4.1-324-gedb3936

Shawn Moore shawn at bestpractical.com
Mon Mar 20 13:05:05 EDT 2017


The branch, 4.4-trunk has been updated
       via  edb3936456fb546cb6883c4340272f3fb2d658a7 (commit)
       via  96382aeb8c5af06ead18236bb0ead3db31798c94 (commit)
      from  1183dc6d174f5e0671c9488aa2223504196c94ea (commit)

Summary of changes:
 etc/RT_Config.pm.in | 34 ++++++++++++++++++++++++++++++
 lib/RT/Handle.pm    | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 91 insertions(+), 2 deletions(-)

- Log -----------------------------------------------------------------
commit 96382aeb8c5af06ead18236bb0ead3db31798c94
Author: Aaron Kondziela <aaron at bestpractical.com>
Date:   Fri Mar 10 17:48:22 2017 -0500

    Custom initialdata handler support
    
    Adds functionality to support plugins for initialdata files that are
    written in something other than the standard perl. Built to support
    the RT::Extension::Initialdata::JSON module.
    
    Fixes T#180408

diff --git a/etc/RT_Config.pm.in b/etc/RT_Config.pm.in
index 7722558..225cec1 100644
--- a/etc/RT_Config.pm.in
+++ b/etc/RT_Config.pm.in
@@ -2918,6 +2918,40 @@ Set($ExternalStorageDirectLink, 0);
 
 =back
 
+
+=head1 Initialdata Formats
+
+RT supports pluggable data format parsers for F<initialdata> files.
+
+If you add format handlers, note that you can remove the perl entry if you
+don't want it available. B<Removing the default perl entry may cause problems
+installing plugins and RT updates>. If so, re-enable it temporarily.
+
+=over 4
+
+=item C<$InitialdataFormatHandlers>
+
+Set the C<$InitialdataFormatHandlers> to an arrayref containing a list of
+format handler modules. The 'perl' entry is the system default, and handles
+perl-style intialdata files.
+
+    Set( $InitialdataFormatHandlers,
+         [
+            'perl',
+            'RT::Extension::Initialdata::Foo',
+            ...
+         [
+       );
+
+=back
+
+=cut
+
+Set( $InitialdataFormatHandlers, [
+    'perl',
+]);
+
+
 =head1 Lifecycles
 
 =head2 Lifecycle definitions
diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index 81747ad..8ac813b 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -859,9 +859,64 @@ sub InsertData {
            @Catalogs, @Assets);
 
     local $@;
+
     $RT::Logger->debug("Going to load '$datafile' data file");
-    eval { require $datafile }
-      or return (0, "Couldn't load data from '$datafile' for import:\n\nERROR:". $@);
+
+    my $datafile_content = do {
+        local $/;
+        open (my $f, '<:encoding(UTF-8)', $datafile)
+            or die "Cannot open initialdata file '$datafile' for read: $@";
+        <$f>;
+    };
+
+    my $format_handler;
+    my $handlers = RT->Config->Get('InitialdataFormatHandlers');
+
+    foreach my $handler_candidate (@$handlers) {
+        next if $handler_candidate eq 'perl';
+        $handler_candidate->require
+            or die "Config option InitialdataFormatHandlers lists '$handler_candidate', but it failed to load:\n$@\n";
+
+        if ($handler_candidate->CanLoad($datafile_content)) {
+            $RT::Logger->debug("Initialdata file '$datafile' can be loaded by $handler_candidate");
+            $format_handler = $handler_candidate;
+            last;
+        } else {
+            $RT::Logger->debug("Initialdata file '$datafile' can not be loaded by $handler_candidate");
+        }
+    }
+
+    if ( $format_handler ) {
+        $format_handler->Load(
+            $datafile_content,
+            {
+                Groups          => \@Groups,
+                Users           => \@Users,
+                Members         => \@Members,
+                ACL             => \@ACL,
+                Queues          => \@Queues,
+                Classes         => \@Classes,
+                ScripActions    => \@ScripActions,
+                ScripConditions => \@ScripConditions,
+                Templates       => \@Templates,
+                CustomFields    => \@CustomFields,
+                CustomRoles     => \@CustomRoles,
+                Scrips          => \@Scrips,
+                Attributes      => \@Attributes,
+                Initial         => \@Initial,
+                Final           => \@Final,
+                Catalogs        => \@Catalogs,
+                Assets          => \@Assets,
+            },
+        ) or return (0, "Couldn't load data from '$datafile' for import:\n\nERROR:" . $@);
+    }
+
+    if ( !$format_handler and grep(/^perl$/, @$handlers) ) {
+        # Use perl-style initialdata
+        # Note: eval of perl initialdata should only be done once
+        eval { require $datafile }
+          or return (0, "Couldn't load data from '$datafile':\nERROR:" . $@ . "\n\nDo you have the correct initialdata handler in RT_Config for this type of file?");
+    }
 
     if ( @Initial ) {
         $RT::Logger->debug("Running initial actions...");

commit edb3936456fb546cb6883c4340272f3fb2d658a7
Merge: 1183dc6 96382ae
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Mon Mar 20 17:04:36 2017 +0000

    Merge branch '4.4.1/json-initialdata' into 4.4-trunk


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


More information about the rt-commit mailing list