[Rt-commit] rt branch 5.0/rest2-current-interface created. rt-5.0.2-185-g150990481f

BPS Git Server git at git.bestpractical.com
Mon Apr 25 19:16:56 UTC 2022


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/rest2-current-interface has been created
        at  150990481f33fa5833b4b8bbd4d61c893c0a0976 (commit)

- Log -----------------------------------------------------------------
commit 150990481f33fa5833b4b8bbd4d61c893c0a0976
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 26 03:02:51 2022 +0800

    Add current interface tests for REST2

diff --git a/t/ticket/interface.t b/t/ticket/interface.t
index fd3ee581bb..c43e13419e 100644
--- a/t/ticket/interface.t
+++ b/t/ticket/interface.t
@@ -100,4 +100,25 @@ ok( $id, "Created ticket #$id" );
 $ticket->Load($id);
 is( $ticket->FirstCustomFieldValue('Interface'), 'REST', 'Interface is set to REST' );
 
+diag 'Test REST2 interface';
+require RT::Test::REST2;
+my $user = RT::Test::REST2->user;
+$user->PrincipalObj->GrantRight( Right => $_ )
+    for qw/CreateTicket SeeQueue ShowTicket ModifyCustomField SeeCustomField/;
+my $rest2_m = RT::Test::REST2->mech;
+
+my $res = $rest2_m->post_json(
+    "/REST/2.0/ticket",
+    {
+        Subject => 'Test REST2 interface',
+        Queue   => 'General',
+        Content => 'Test',
+    },
+    'Authorization' => RT::Test::REST2->authorization_header,
+);
+is( $res->code, 201 );
+ok( ($id) = $res->header('location') =~ qr[/ticket/(\d+)] );
+$ticket->Load($id);
+is( $ticket->FirstCustomFieldValue('Interface'), 'REST2', 'Interface is set to REST2' );
+
 done_testing;

commit 4b8f1bdafa841a657ad16c5c2625ea49eba5923c
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Apr 26 02:59:44 2022 +0800

    Set current interface for REST2
    
    The code is imported from rt-extension-rest2(58b3a255f7).

diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
index 1f4c8e4bba..01630500ca 100644
--- a/lib/RT/REST2.pm
+++ b/lib/RT/REST2.pm
@@ -1449,17 +1449,13 @@ handle them appropriately.
 sub to_psgi_app {
     my $self = shift;
     my $res = $self->to_app(@_);
-
-    return Plack::Util::response_cb($res, sub {
-        my $res = shift;
-        $self->CleanupRequest;
-    });
 }
 
 sub to_app {
     my $class = shift;
 
     return builder {
+        enable '+RT::REST2::Middleware::CleanupRequest';
         enable '+RT::REST2::Middleware::ErrorAsJSON';
         enable '+RT::REST2::Middleware::Log';
         enable '+RT::REST2::Middleware::Auth';
@@ -1484,22 +1480,4 @@ sub PSGIWrap {
     };
 }
 
-sub CleanupRequest {
-
-    if ( $RT::Handle && $RT::Handle->TransactionDepth ) {
-        $RT::Handle->ForceRollback;
-        $RT::Logger->crit(
-            "Transaction not committed. Usually indicates a software fault."
-            . "Data loss may have occurred" );
-    }
-
-    # Clean out the ACL cache. the performance impact should be marginal.
-    # Consistency is imprived, too.
-    RT::Principal->InvalidateACLCache();
-    DBIx::SearchBuilder::Record::Cachable->FlushCache
-      if ( RT->Config->Get('WebFlushDbCacheEveryRequest')
-        and UNIVERSAL::can(
-            'DBIx::SearchBuilder::Record::Cachable' => 'FlushCache' ) );
-}
-
 1;
diff --git a/lib/RT/REST2/Dispatcher.pm b/lib/RT/REST2/Dispatcher.pm
index fe7f4ef234..32353447b4 100644
--- a/lib/RT/REST2/Dispatcher.pm
+++ b/lib/RT/REST2/Dispatcher.pm
@@ -92,6 +92,7 @@ sub to_psgi_app {
     return sub {
         my $env = shift;
 
+        RT->SetCurrentInterface('REST2');
         RT::ConnectToDatabase();
         my $dispatch = $self->_dispatcher->dispatch($env->{PATH_INFO});
 
diff --git a/lib/RT/REST2/Dispatcher.pm b/lib/RT/REST2/Middleware/CleanupRequest.pm
similarity index 50%
copy from lib/RT/REST2/Dispatcher.pm
copy to lib/RT/REST2/Middleware/CleanupRequest.pm
index fe7f4ef234..52192deb97 100644
--- a/lib/RT/REST2/Dispatcher.pm
+++ b/lib/RT/REST2/Middleware/CleanupRequest.pm
@@ -46,76 +46,19 @@
 #
 # END BPS TAGGED BLOCK }}}
 
-package RT::REST2::Dispatcher;
+package RT::REST2::Middleware::CleanupRequest;
+
 use strict;
 use warnings;
-use Moose;
-use Web::Machine;
-use Path::Dispatcher;
-use Plack::Request;
-use List::MoreUtils 'uniq';
-
-use Module::Pluggable (
-    search_path => ['RT::REST2::Resource'],
-    sub_name    => '_resource_classes',
-    require     => 1,
-    max_depth   => 5,
-);
-
-has _dispatcher => (
-    is         => 'ro',
-    isa        => 'Path::Dispatcher',
-    builder    => '_build_dispatcher',
-);
-
-sub _build_dispatcher {
-    my $self = shift;
-    my $dispatcher = Path::Dispatcher->new;
-
-    for my $resource_class ($self->_resource_classes) {
-        if ($resource_class->can('dispatch_rules')) {
-            my @rules = $resource_class->dispatch_rules;
-            for my $rule (@rules) {
-                $rule->{_rest2_resource} = $resource_class;
-                $dispatcher->add_rule($rule);
-            }
-        }
-    }
-
-    return $dispatcher;
-}
-
-sub to_psgi_app {
-    my $class = shift;
-    my $self = $class->new;
-
-    return sub {
-        my $env = shift;
-
-        RT::ConnectToDatabase();
-        my $dispatch = $self->_dispatcher->dispatch($env->{PATH_INFO});
-
-        return [404, ['Content-Type' => 'text/plain'], 'Not Found']
-            if !$dispatch->has_matches;
-
-        my @matches = $dispatch->matches;
-        my @matched_resources = uniq map { $_->rule->{_rest2_resource} } @matches;
-        if (@matched_resources > 1) {
-            RT->Logger->error("Path $env->{PATH_INFO} erroneously matched " . scalar(@matched_resources) . " resources: " . (join ', ', @matched_resources) . ". Refusing to dispatch.");
-            return [500, ['Content-Type' => 'text/plain'], 'Internal Server Error']
-        }
 
-        my $match = shift @matches;
+use base 'Plack::Middleware';
 
-        my $rule = $match->rule;
-        my $resource = $rule->{_rest2_resource};
-        my $args = $rule->block ? $match->run(Plack::Request->new($env)) : {};
-        my $machine = Web::Machine->new(
-            resource      => $resource,
-            resource_args => [%$args],
-        );
-        return $machine->call($env);
-    };
+sub call {
+    my ( $self, $env ) = @_;
+    my $res = $self->app->($env);
+    require RT::Interface::Web::Handler;
+    RT::Interface::Web::Handler->CleanupRequest;
+    return $res;
 }
 
 1;

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


hooks/post-receive
-- 
rt


More information about the rt-commit mailing list