[Bps-public-commit] RT-Extension-NHD branch, master, updated. 156989923b95cf63d256755bb0cc1b1f207439e8

Ruslan Zakirov ruz at bestpractical.com
Thu Nov 10 16:29:36 EST 2011


The branch, master has been updated
       via  156989923b95cf63d256755bb0cc1b1f207439e8 (commit)
      from  7f3ae82fbcffe7a4fa0751888737563b7966c95f (commit)

Summary of changes:
 etc/NHD_Config.pm    |   20 ++++++++++++++++
 lib/RT/NHD/Ticket.pm |   63 +++++++++++++++++++++++++++++++++++++++++++++----
 t/rest/ticket.t      |    7 ++---
 3 files changed, 80 insertions(+), 10 deletions(-)

- Log -----------------------------------------------------------------
commit 156989923b95cf63d256755bb0cc1b1f207439e8
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Fri Nov 11 01:27:29 2011 +0400

    configurable status conversions NHD <-> RT

diff --git a/etc/NHD_Config.pm b/etc/NHD_Config.pm
index 0eb385b..ad1bb23 100644
--- a/etc/NHD_Config.pm
+++ b/etc/NHD_Config.pm
@@ -20,6 +20,26 @@ Set( $NHD_WebURL, RT->Config->Get('WebURL') . 'NoAuth/NHD/1.0' );
 
 Set( $NHD_Name, RT->Config->Get('Organization') );
 
+=item C<$NHD_StatusMap>
+
+=cut
+
+Set( %NHD_StatusMap,
+    'NHD -> default' => {
+        open     => 'open',
+        pending  => 'stalled',
+        closed   => 'resolved',
+    },
+    'default -> NHD' => {
+        new      => 'open',
+        open     => 'open',
+        stalled  => 'pending',
+        resolved => 'closed',
+        rejected => 'closed',
+        deleted  => 'closed',
+    },
+);
+
 =back
 
 =cut
diff --git a/lib/RT/NHD/Ticket.pm b/lib/RT/NHD/Ticket.pm
index cc23f22..bec4ea5 100644
--- a/lib/RT/NHD/Ticket.pm
+++ b/lib/RT/NHD/Ticket.pm
@@ -75,6 +75,12 @@ sub Create {
         return (0, "Agreement has no queue defined");
     }
 
+    ($args{'Status'}, my $msg) = $self->ConvertRemoteStatus(
+        Status => $args{'Status'}, Queue => $queue,
+    );
+    return (0, "Couldn't convert status: $msg")
+        unless $args{'Status'};
+
     if ( ref $args{'Requestor'} eq 'HASH') {
         my ($user, $msg) = $self->LoadOrCreateUser(
             %{ $args{'Requestor'} },
@@ -89,7 +95,7 @@ sub Create {
     my $corresponds = delete $args{'Corresponds'};
 
     my $ticket = RT::Ticket->new( $self->CurrentUser );
-    my ($id, $txn, $msg) = $ticket->Create(
+    (my $id, undef, $msg) = $ticket->Create(
         %args,
         Queue => $queue,
     );
@@ -116,10 +122,11 @@ sub Update {
     my $ticket = $self->Ticket;
 
     if ( exists $args{'Status'} ) {
-        return $self->RollbackTransaction("Invalid status value")
-            unless grep $_ eq lc($args{'Status'}), @STATUSES;
-        $args{'Status'} = $STATUS_NHD2RT{ lc $args{'Status'} };
-        delete $args{'Status'} if $ticket->Status eq $args{'Status'};
+        ($args{'Status'}, my $msg) = $self->ConvertRemoteStatus(
+            Status => $args{'Status'}
+        );
+        return $self->RollbackTransaction("Couldn't convert status: $msg")
+            unless $args{'Status'};
     }
     foreach my $field ( qw(Status Subject) ) {
         next unless exists $args{ $field };
@@ -147,6 +154,7 @@ sub Update {
     return (1, 'Updated');
 }
 
+
 sub id { (shift)->Ticket->id }
 
 sub UUID {
@@ -181,7 +189,7 @@ sub ForJSON {
     $res{'uuid'} = $self->UUID( $args{'Agreement'} );
     $res{'subject'} = $ticket->Subject;
     $res{'requested_at'} = $ticket->CreatedObj->NHD;
-    $res{'status'} = $ticket->Status; # XXX: convert it
+    $res{'status'} = $self->ConvertLocalStatus;
 
     if ( my $requestor = $ticket->Requestors->UserMembersObj->First ) {
         $res{'requester'} = $self->PresentUser(
@@ -228,6 +236,49 @@ sub FromJSON {
     return $res;
 }
 
+sub ConvertRemoteStatus {
+    my $self = shift;
+    my %args = (
+        Status => undef,
+        Queue => undef,
+        @_
+    );
+    return (undef, "no value") unless $args{'Status'};
+    return (undef, "invalid value") unless grep $args{'Status'} eq $_, @STATUSES;
+
+    my $queue = $self->id ? $self->Ticket->QueueObj : $args{'Queue'};
+    my $map_name = 'NHD -> '. $queue->Lifecycle->Name;
+    return $self->ConvertStatus( $map_name => $args{'Status'} );
+}
+
+sub ConvertLocalStatus {
+    my $self = shift;
+    my %args = (
+        Status => undef,
+        @_
+    );
+    my $ticket = $self->Ticket;
+    $args{'Status'} ||= $ticket->Status;
+
+    my $map_name = $ticket->QueueObj->Lifecycle->Name .' -> NHD';
+    return $self->ConvertStatus( $map_name => $args{'Status'} );
+}
+
+sub ConvertStatus {
+    my $self     = shift;
+    my $map_name = shift;
+    my $status   = shift;
+
+    my $map = RT->Config->Get('NHD_StatusMap')->{ $map_name };
+    return (undef, "no map in \%NHD_StatusMap for '$map_name'")
+        unless $map;
+
+    my $res = $map->{ $status };
+    return (undef, "no mapping for '$status' in '$map_name' map")
+        unless $res;
+    return ($res);
+}
+
 sub LoadOrCreateUser {
     my $self = shift;
     my %args = @_;
diff --git a/t/rest/ticket.t b/t/rest/ticket.t
index 9489ca2..672a3d2 100644
--- a/t/rest/ticket.t
+++ b/t/rest/ticket.t
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use RT::Extension::NetworkedHelpDesk::Test tests => 13;
+use RT::Extension::NetworkedHelpDesk::Test tests => 18;
 my $test = 'RT::Extension::NetworkedHelpDesk::Test';
 use Digest::SHA1 qw(sha1_hex);
 
@@ -98,15 +98,14 @@ my $access_key = sha1_hex( ''. ++$i );
         },
     );
 
-    diag $response->content;
     my $json = RT::Extension::NetworkedHelpDesk->FromJSON( $response->content );
     is_deeply(
         $json,
         {
             uuid => $uuid,
-            subject => 'test ticket',
+            subject => 'another test ticket',
             requested_at => "2010-11-24 22:13:54 +0000",
-            status => 'open',
+            status => 'pending',
             requester => {
                 uuid => sha1_hex( ''. $i ),
                 name => 'John Doe',

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



More information about the Bps-public-commit mailing list