[Rt-commit] rt branch, 5.0/rest2-ticket-more-updates, created. rt-5.0.0-190-g464ee2491c
? sunnavy
sunnavy at bestpractical.com
Thu Dec 31 14:06:01 EST 2020
The branch, 5.0/rest2-ticket-more-updates has been created
at 464ee2491cfa024a9697dd991674747f6c5a3700 (commit)
- Log -----------------------------------------------------------------
commit 51e81b7c2d6f7689978e8727433a4fc306f2a4e6
Author: sunnavy <sunnavy at bestpractical.com>
Date: Sat Oct 17 02:08:38 2020 +0800
Support to merge tickets in REST2
diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
index 5f7fc1b7f4..bbdfb6216c 100644
--- a/lib/RT/REST2.pm
+++ b/lib/RT/REST2.pm
@@ -499,6 +499,11 @@ Below are some examples using the endpoints above.
-d '{ "Subject": "Update test", "CustomFields": {"Severity": "High"}}'
'https://myrt.com/REST/2.0/ticket/6'
+ # Merge a ticket into another
+ curl -X PUT -H "Content-Type: application/json" -u 'root:password'
+ -d '{ "MergeInto": 3 }'
+ 'https://myrt.com/REST/2.0/ticket/6'
+
# Correspond a ticket
curl -X POST -H "Content-Type: application/json" -u 'root:password'
-d '{ "Content": "Testing a correspondence", "ContentType": "text/plain" }'
diff --git a/lib/RT/REST2/Resource/Ticket.pm b/lib/RT/REST2/Resource/Ticket.pm
index 02a020d02c..d671e0c59c 100644
--- a/lib/RT/REST2/Resource/Ticket.pm
+++ b/lib/RT/REST2/Resource/Ticket.pm
@@ -60,7 +60,7 @@ with (
=> { -alias => { hypermedia_links => '_default_hypermedia_links' } },
'RT::REST2::Resource::Record::Deletable',
'RT::REST2::Resource::Record::Writable'
- => { -alias => { create_record => '_create_record' } },
+ => { -alias => { create_record => '_create_record', update_record => '_update_record' } },
);
sub dispatch_rules {
@@ -104,6 +104,19 @@ sub create_record {
return ($ok, $msg);
}
+sub update_record {
+ my $self = shift;
+ my $data = shift;
+
+ my @results;
+ push @results, $self->_update_record($data);
+ if ( my $ticket_id = delete $data->{MergeInto} ) {
+ my ( $ok, $msg ) = $self->record->MergeInto($ticket_id);
+ push @results, $msg;
+ }
+ return @results;
+}
+
sub forbidden {
my $self = shift;
return 0 unless $self->record->id;
commit 71333f66616920919bd370505f482dbdab67e1b6
Author: sunnavy <sunnavy at bestpractical.com>
Date: Sat Oct 17 02:37:25 2020 +0800
Support to take/untake/steal tickets in REST2
diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
index bbdfb6216c..e552b4cac7 100644
--- a/lib/RT/REST2.pm
+++ b/lib/RT/REST2.pm
@@ -504,6 +504,18 @@ Below are some examples using the endpoints above.
-d '{ "MergeInto": 3 }'
'https://myrt.com/REST/2.0/ticket/6'
+ # Take a ticket
+ curl -X PUT -H "Content-Type: application/json" -u 'root:password'
+ 'https://myrt.com/REST/2.0/ticket/6/take'
+
+ # Untake a ticket
+ curl -X PUT -H "Content-Type: application/json" -u 'root:password'
+ 'https://myrt.com/REST/2.0/ticket/6/untake'
+
+ # Steal a ticket
+ curl -X PUT -H "Content-Type: application/json" -u 'root:password'
+ 'https://myrt.com/REST/2.0/ticket/6/steal'
+
# Correspond a ticket
curl -X POST -H "Content-Type: application/json" -u 'root:password'
-d '{ "Content": "Testing a correspondence", "ContentType": "text/plain" }'
diff --git a/lib/RT/REST2/Resource/Record/Writable.pm b/lib/RT/REST2/Resource/Record/Writable.pm
index 8fdcb4bd57..634bf7076f 100644
--- a/lib/RT/REST2/Resource/Record/Writable.pm
+++ b/lib/RT/REST2/Resource/Record/Writable.pm
@@ -145,7 +145,16 @@ sub from_multipart {
sub from_json {
my $self = shift;
- my $params = shift || JSON::decode_json( $self->request->content );
+ my $params = shift;
+
+ if ( !$params ) {
+ if ( my $content = $self->request->content ) {
+ $params = JSON::decode_json($content);
+ }
+ else {
+ $params = {};
+ }
+ }
%$params = (
%$params,
diff --git a/lib/RT/REST2/Resource/Role/RequestBodyIsJSON.pm b/lib/RT/REST2/Resource/Role/RequestBodyIsJSON.pm
index f896faf806..ceb0951b5b 100644
--- a/lib/RT/REST2/Resource/Role/RequestBodyIsJSON.pm
+++ b/lib/RT/REST2/Resource/Role/RequestBodyIsJSON.pm
@@ -75,6 +75,7 @@ role {
return 0 unless $request->method =~ /^(PUT|POST)$/;
return 0 unless $request->header('Content-Type') =~ /^application\/json/;
+ return 0 unless $request->content; # allow empty content
my $json = eval {
JSON::from_json($request->content)
};
diff --git a/lib/RT/REST2/Resource/Ticket.pm b/lib/RT/REST2/Resource/Ticket.pm
index d671e0c59c..c1fe91a4ed 100644
--- a/lib/RT/REST2/Resource/Ticket.pm
+++ b/lib/RT/REST2/Resource/Ticket.pm
@@ -63,6 +63,11 @@ with (
=> { -alias => { create_record => '_create_record', update_record => '_update_record' } },
);
+has 'action' => (
+ is => 'ro',
+ isa => 'Str',
+);
+
sub dispatch_rules {
Path::Dispatcher::Rule::Regex->new(
regex => qr{^/ticket/?$},
@@ -71,7 +76,11 @@ sub dispatch_rules {
Path::Dispatcher::Rule::Regex->new(
regex => qr{^/ticket/(\d+)/?$},
block => sub { { record_class => 'RT::Ticket', record_id => shift->pos(1) } },
- )
+ ),
+ Path::Dispatcher::Rule::Regex->new(
+ regex => qr{^/ticket/(\d+)/(take|untake|steal)$},
+ block => sub { { record_class => 'RT::Ticket', record_id => $_[0]->pos(1), action => $_[0]->pos(2) } },
+ ),
}
sub create_record {
@@ -109,6 +118,13 @@ sub update_record {
my $data = shift;
my @results;
+
+ if ( my $action = $self->action ) {
+ my $method = ucfirst $action;
+ my ( $ok, $msg ) = $self->record->$method();
+ push @results, $msg;
+ }
+
push @results, $self->_update_record($data);
if ( my $ticket_id = delete $data->{MergeInto} ) {
my ( $ok, $msg ) = $self->record->MergeInto($ticket_id);
commit 4a7f98165f0b945b7fa0f3e406da1aff2eb80059
Author: sunnavy <sunnavy at bestpractical.com>
Date: Sat Oct 17 03:00:34 2020 +0800
Test ticket merge in REST2
diff --git a/t/rest2/tickets.t b/t/rest2/tickets.t
index 1642e80f3f..037826a22b 100644
--- a/t/rest2/tickets.t
+++ b/t/rest2/tickets.t
@@ -338,6 +338,18 @@ my ($ticket_url, $ticket_id);
$content = $mech->json_response;
is($content->{Subject}, 'Ticket update using REST');
is($content->{Priority}, 42);
+
+ $payload = {
+ Subject => 'Ticket creation using REST',
+ Queue => 'General',
+ };
+ $res = $mech->post_json( "$rest_base_path/ticket", $payload, 'Authorization' => $auth, );
+ is( $res->code, 201 );
+
+ $payload = { MergeInto => $ticket_id };
+ $res = $mech->put_json( $res->header('location'), $payload, 'Authorization' => $auth, );
+ is( $res->code, 200 );
+ is_deeply( $mech->json_response, ['Merge Successful'] );
}
# Transactions
@@ -353,11 +365,11 @@ my ($ticket_url, $ticket_id);
is($res->code, 200);
my $content = $mech->json_response;
- is($content->{count}, 3);
+ is($content->{count}, 6);
is($content->{page}, 1);
is($content->{per_page}, 20);
- is($content->{total}, 3);
- is(scalar @{$content->{items}}, 3);
+ is($content->{total}, 6);
+ is(scalar @{$content->{items}}, 6);
for my $txn (@{ $content->{items} }) {
is($txn->{type}, 'transaction');
commit 464ee2491cfa024a9697dd991674747f6c5a3700
Author: sunnavy <sunnavy at bestpractical.com>
Date: Sat Oct 17 03:11:37 2020 +0800
Test ticket steal/take/untake in REST2
diff --git a/lib/RT/Test/REST2.pm b/lib/RT/Test/REST2.pm
index 098841868b..9b80acadfd 100644
--- a/lib/RT/Test/REST2.pm
+++ b/lib/RT/Test/REST2.pm
@@ -154,7 +154,7 @@ sub mech { RT::Test::REST2::Mechanize->new }
my ($self, $url, $payload, %headers) = @_;
$self->put(
$url,
- Content => $json->encode($payload),
+ $payload ? ( Content => $json->encode($payload) ) : (),
'Content-Type' => 'application/json; charset=utf-8',
%headers,
);
diff --git a/t/rest2/tickets.t b/t/rest2/tickets.t
index 037826a22b..e6daeae222 100644
--- a/t/rest2/tickets.t
+++ b/t/rest2/tickets.t
@@ -339,6 +339,25 @@ my ($ticket_url, $ticket_id);
is($content->{Subject}, 'Ticket update using REST');
is($content->{Priority}, 42);
+ $payload = { 'Owner' => 'root' };
+ $res = $mech->put_json( $ticket_url, $payload, 'Authorization' => $auth, );
+ is( $res->code, 200 );
+ is_deeply( $mech->json_response, ["Ticket $ticket_id: Owner changed from Nobody to root"] );
+
+ $user->PrincipalObj->GrantRight( Right => 'OwnTicket' );
+
+ my %result = (
+ steal => 'Owner changed from root to test',
+ untake => 'Owner changed from test to Nobody',
+ take => 'Owner changed from Nobody to test',
+ );
+
+ for my $action (qw/steal untake take/) {
+ $res = $mech->put_json( "$ticket_url/$action", undef, 'Authorization' => $auth, );
+ is( $res->code, 200 );
+ is_deeply( $mech->json_response, [ $result{$action} ] );
+ }
+
$payload = {
Subject => 'Ticket creation using REST',
Queue => 'General',
@@ -365,11 +384,14 @@ my ($ticket_url, $ticket_id);
is($res->code, 200);
my $content = $mech->json_response;
- is($content->{count}, 6);
+ is($content->{count}, 14);
is($content->{page}, 1);
is($content->{per_page}, 20);
- is($content->{total}, 6);
- is(scalar @{$content->{items}}, 6);
+
+ # TODO This 14 VS 15 inconsitency is because user lacks ShowOutgoingEmail.
+ # It'll be perfect if we can keep them in sync
+ is($content->{total}, 15);
+ is(scalar @{$content->{items}}, 14);
for my $txn (@{ $content->{items} }) {
is($txn->{type}, 'transaction');
-----------------------------------------------------------------------
More information about the rt-commit
mailing list