[Rt-commit] rt branch, 5.0/rest2-merge-ticket, created. rt-5.0.0-23-gf040575a1c

Aaron Trevena ast at bestpractical.com
Mon Sep 21 16:03:17 EDT 2020


The branch, 5.0/rest2-merge-ticket has been created
        at  f040575a1cc562ac3b45ab663a3b957fc20002ea (commit)

- Log -----------------------------------------------------------------
commit 051de22eb6fda0b0d4458a551e47dbe28db854d2
Author: Aaron Trevena <ast at bestpractical.com>
Date:   Mon Sep 21 21:01:21 2020 +0100

    Add merge ticket endpoint to REST2 API
    
    Add Resource class providing endpoint to merge ticket

diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
index 1079f95cd6..94a52ec87c 100644
--- a/lib/RT/REST2.pm
+++ b/lib/RT/REST2.pm
@@ -482,6 +482,10 @@ curl for SSL like --cacert.
     PUT /tickets/bulk
         update multiple tickets' metadata; provide JSON content(array of hashes)
 
+    PUT ticket/:id/merge
+        merge ticket into another ticket matching the id provided in
+         MergeIntoTicket form or json field.
+
 =head3 Ticket Examples
 
 Below are some examples using the endpoints above.
@@ -530,6 +534,12 @@ Below are some examples using the endpoints above.
     -d '[{ "field" : "id", "operator" : ">=", "value" : 0 }]'
     'https://myrt.com/REST/2.0/asset'
 
+    # Merge ticket into another
+    curl -X PUT -H "Content-Type: application/json" -u 'root:password'
+        -d '{"MergeIntoTicket": 7 }'
+        'https://myrt.com/REST/2.0/ticket/6/merge'
+
+
 =head3 Transactions
 
     GET /transactions?query=<JSON>
diff --git a/lib/RT/REST2/Resource/Merge.pm b/lib/RT/REST2/Resource/Merge.pm
new file mode 100644
index 0000000000..94db6c4128
--- /dev/null
+++ b/lib/RT/REST2/Resource/Merge.pm
@@ -0,0 +1,151 @@
+# BEGIN BPS TAGGED BLOCK {{{
+#
+# COPYRIGHT:
+#
+# This software is Copyright (c) 1996-2020 Best Practical Solutions, LLC
+#                                          <sales at bestpractical.com>
+#
+# (Except where explicitly superseded by other copyright notices)
+#
+#
+# LICENSE:
+#
+# This work is made available to you under the terms of Version 2 of
+# the GNU General Public License. A copy of that license should have
+# been provided with this software, but in any event can be snarfed
+# from www.gnu.org.
+#
+# This work is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+#
+#
+# CONTRIBUTION SUBMISSION POLICY:
+#
+# (The following paragraph is not intended to limit the rights granted
+# to you to modify and distribute this software under the terms of
+# the GNU General Public License and is only of importance to you if
+# you choose to contribute your changes and enhancements to the
+# community by submitting them to Best Practical Solutions, LLC.)
+#
+# By intentionally submitting any modifications, corrections or
+# derivatives to this work, or any other work intended for use with
+# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# royalty-free, perpetual, license to use, copy, create derivative
+# works based on those contributions, and sublicense and distribute
+# those contributions and any derivatives thereof.
+#
+# END BPS TAGGED BLOCK }}}
+
+package RT::REST2::Resource::Merge;
+use strict;
+use warnings;
+
+use Moose;
+use namespace::autoclean;
+use MIME::Base64;
+
+extends 'RT::REST2::Resource';
+use RT::REST2::Util qw( error_as_json );
+
+sub dispatch_rules {
+    Path::Dispatcher::Rule::Regex->new(
+        regex => qr{^/ticket/(\d+)/merge$},
+        block => sub {
+            my ($match, $req) = @_;
+            my $ticket = RT::Ticket->new($req->env->{"rt.current_user"});
+            $ticket->Load($match->pos(1));
+            return { record => $ticket };
+        },
+    );
+}
+
+has record => (
+    is       => 'ro',
+    isa      => 'RT::Record',
+    required => 1,
+);
+
+has merged_ticket_id => (
+    is       => 'rw',
+    isa      => 'Maybe[Int]'
+);
+
+sub post_is_create            { 1 }
+sub create_path_after_handler { 1 }
+sub allowed_methods           { ['PUT'] }
+sub charsets_provided         { [ 'utf-8' ] }
+sub default_charset           { 'utf-8' }
+sub content_types_provided    { [ { 'application/json' => sub {} } ] }
+
+# Web::Machine uses 'application/octet-stream' as default content-type if
+# none provided. We accept empty POST so may receive empty content-type.
+sub content_types_accepted    { [
+    { 'application/x-www-form-urlencoded' => 'from_form' },
+    { 'multipart/form-data' => 'from_form' },
+    { 'application/json' => 'from_json' }
+] }
+
+
+sub from_json {
+    my $self = shift;
+    my $payload = shift || JSON::decode_json( $self->request->content );
+    my $destination_ticket_id = $payload->{MergeIntoTicket};
+
+    # throw error unless we have a ticket id.
+    return error_as_json(
+        $self->response,
+        \400, "MergeIntoTicket is a required field")
+            unless $destination_ticket_id;
+
+    return $self->merge_ticket($destination_ticket_id);
+}
+
+sub from_form {
+    my $self = shift;
+    my $destination_ticket_id = $self->request->parameters->{MergeIntoTicket};
+
+    # throw error unless we have a ticket id.
+    return error_as_json(
+        $self->response,
+        \400, "MergeIntoTicket is a required field")
+        unless $destination_ticket_id;
+
+    return $self->merge_ticket($destination_ticket_id);
+}
+
+sub merge_ticket {
+    my ($self, $destination_ticket_id) = @_;
+
+    my $source_ticket = $self->record;
+
+    my ($status, $msg) = $source_ticket->MergeInto($destination_ticket_id);
+    unless ($status) {
+        return error_as_json(
+            $self->response,
+            \400,  $msg);
+    }
+
+    $self->merged_ticket_id($destination_ticket_id);
+    $self->response->body(JSON::to_json([$msg], { pretty => 1 }));
+
+    return 1;
+}
+
+sub create_path {
+    return '/ticket/' . shift->merged_ticket_id;
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;
+

commit f040575a1cc562ac3b45ab663a3b957fc20002ea
Author: Aaron Trevena <ast at bestpractical.com>
Date:   Mon Sep 21 21:02:43 2020 +0100

    Add tests for new REST2 ticket merge endpoint

diff --git a/t/rest2/tickets.t b/t/rest2/tickets.t
index 837efd2992..15948993ff 100644
--- a/t/rest2/tickets.t
+++ b/t/rest2/tickets.t
@@ -469,6 +469,43 @@ my ($ticket_url, $ticket_id);
     is($content->{ContentType}, 'text/html');
 }
 
+# Merge into another ticket
+{
+
+    my $queue = RT::Test->load_or_create_queue(Name => 'TestMergeQueue');
+    ok( RT::Test->add_rights(
+        { Principal => $user,  Right => [qw(OwnTicket SeeQueue ModifyTicket ShowTicket)] }
+    ), 'set rights');
+
+    my $src_ticket = RT::Test->create_ticket(Queue => $queue->Id,
+                                             Subject => 'Test mergee',
+                                             Content => 'Test test test',
+                                             Requestor => $user->EmailAddress
+                                         );
+    my $src_ticket_id = $src_ticket->Id;
+
+    my $dest_ticket = RT::Test->create_ticket(Queue => $queue->Id,
+                                             Subject => 'Test merged into',
+                                             Content => 'Test test test merged into',
+                                             Requestor => $user->EmailAddress
+                                          );
+
+    # try to merge without destination ticket
+
+    my $res = $mech->put_json("$rest_base_path/ticket/$src_ticket_id/merge",
+                              { } , 'Authorization' => $auth );
+    like($res->code, qr/400/, 'attempt to take non-existant ticket gives 4xx error');
+    is($mech->json_response->{message}, 'MergeIntoTicket is a required field', 'correct error on missing ticket id');
+
+    # merge Ticket
+    $res = $mech->put_json("$rest_base_path/ticket/$src_ticket_id/merge",
+                              { MergeIntoTicket => $dest_ticket->Id }, 'Authorization' => $auth );
+    is($res->code, 200, 'request to take ticket gives 200 success');
+    is($mech->json_response->[0], 'Merge Successful', 'merged ok') or diag $mech->json_response->[0];
+
+    $dest_ticket->Delete;
+}
+
 # Ticket Sorted Search
 {
     my $ticket2 = RT::Ticket->new($RT::SystemUser);

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


More information about the rt-commit mailing list