[Rt-commit] rt branch, 5.0/sla-by-priority, created. rt-5.0.0beta1-23-g545fe12e63

? sunnavy sunnavy at bestpractical.com
Thu Jun 11 18:24:52 EDT 2020


The branch, 5.0/sla-by-priority has been created
        at  545fe12e631942069f21c4060d2b09a99fd42109 (commit)

- Log -----------------------------------------------------------------
commit 834f181ec9f123a1a1dd8275cad011f40df05320
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Jun 12 03:27:47 2020 +0800

    Set SLA on priority change based on %PriorityToSLA config

diff --git a/etc/RT_Config.pm.in b/etc/RT_Config.pm.in
index 51fe3c13e3..9eece693c1 100644
--- a/etc/RT_Config.pm.in
+++ b/etc/RT_Config.pm.in
@@ -4657,6 +4657,23 @@ Default Business Hours setting is in $ServiceBusinessHours{'Default'}.
 
 Set( %ServiceBusinessHours, );
 
+=item C<%PriorityToSLA>
+
+Set mapping between priority values and SLA's so that when priority is
+updated the corresponding SLA will be set.
+
+Both stringified and numeric priority values are supported, for example:
+
+    Set(%PriorityToSLA,
+        Low    => '8h',
+        Medium => '4h',
+        High   => '2h',
+    );
+
+=cut
+
+Set( %PriorityToSLA, );
+
 =back
 
 =head2 Custom Date Ranges
diff --git a/etc/initialdata b/etc/initialdata
index 1949560948..2480cdf641 100644
--- a/etc/initialdata
+++ b/etc/initialdata
@@ -125,6 +125,10 @@
        Description => 'Set the due date according to an agreement' , # loc
        ExecModule  => 'SLA_SetDue',
     },
+    {  Name        => 'Set SLA according to Priority', # loc
+       Description => 'Set SLA according to Priority', # loc
+       ExecModule  => 'SetSLAFromPriority',
+    },
 );
 
 @ScripConditions = (
@@ -830,6 +834,10 @@ Hour:         { $SubscriptionObj->SubValue('Hour') }
        ScripCondition    => 'Require due set according to SLA',
        ScripAction       => 'Set due date according to SLA',
        Template          => 'Blank' },
+    {  Description       => "On Priority Change Set SLA according to Priority",
+       ScripCondition    => 'On Priority Change',
+       ScripAction       => 'Set SLA according to Priority',
+       Template          => 'Blank' },
 );
 
 @ACL = (
diff --git a/etc/upgrade/4.5.9/content b/etc/upgrade/4.5.9/content
new file mode 100644
index 0000000000..c7a4349742
--- /dev/null
+++ b/etc/upgrade/4.5.9/content
@@ -0,0 +1,17 @@
+use strict;
+use warnings;
+
+our @ScripActions = (
+    {   Name        => 'Set SLA according to Priority',    # loc
+        Description => 'Set SLA according to Priority',    # loc
+        ExecModule  => 'SetSLAFromPriority',
+    },
+);
+
+our @Scrips = (
+    {   Description    => "On Priority Change Set SLA according to Priority",
+        ScripCondition => 'On Priority Change',
+        ScripAction    => 'Set SLA according to Priority',
+        Template       => 'Blank'
+    },
+);
diff --git a/lib/RT/Action/SetSLAFromPriority.pm b/lib/RT/Action/SetSLAFromPriority.pm
new file mode 100644
index 0000000000..190ce16b6d
--- /dev/null
+++ b/lib/RT/Action/SetSLAFromPriority.pm
@@ -0,0 +1,90 @@
+# 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::Action::SetSLAFromPriority;
+use base 'RT::Action';
+
+use strict;
+use warnings;
+
+=head2 Prepare
+
+=cut
+
+sub Prepare {
+    my $self = shift;
+    return 0 if $self->TicketObj->QueueObj->SLADisabled;
+
+    my $priority_to_sla = RT::Config->Get( 'PriorityToSLA' ) or return 0;
+    my $sla
+        = $priority_to_sla->{ $self->TicketObj->Priority } || $priority_to_sla->{ $self->TicketObj->PriorityAsString }
+        or return 0;
+
+    return 0 if ( $self->TicketObj->SLA // '' ) eq $sla;
+    $self->{SLA} = $sla;
+    return 1;
+}
+
+=head2 Commit
+
+Set SLA based on Priority.
+
+=cut
+
+sub Commit {
+    my $self = shift;
+
+    if ( $self->{SLA} ) {
+        my ($ret, $msg) = $self->TicketObj->SetSLA( $self->{SLA} );
+        RT::Logger->error( "Could not set ticket SLA from priority: $msg" ) unless $ret;
+        return $ret;
+    }
+    return 1;
+}
+
+1;

commit 3a5fc5c58eb6e3b74109189daa52ed3b9fd00fc6
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Jun 12 04:31:18 2020 +0800

    Set SLA on create based on %PriorityToSLA config

diff --git a/lib/RT/Ticket.pm b/lib/RT/Ticket.pm
index 7ef41e494d..92e734f5a6 100644
--- a/lib/RT/Ticket.pm
+++ b/lib/RT/Ticket.pm
@@ -397,7 +397,7 @@ sub Create {
         Resolved        => $Resolved->ISO,
         Due             => $Due->ISO,
         $args{ 'Type' } eq 'ticket'
-          ? ( SLA => $args{ SLA } || RT::SLA->GetDefaultServiceLevel( Queue => $QueueObj ), )
+          ? ( SLA => $args{ SLA } )
           : (),
     );
 
@@ -406,6 +406,15 @@ sub Create {
         $params{$attr} = $args{$attr} if $args{$attr};
     }
 
+    if ( exists $params{SLA} && !$params{SLA} && !$QueueObj->SLADisabled ) {
+        if ( defined $params{Priority} and length $params{Priority} and ( my $map = RT->Config->Get('PriorityToSLA') ) )
+        {
+            $params{SLA} = $map->{ $params{Priority} }
+                || $map->{ $self->_PriorityAsString( $params{Priority}, $QueueObj->Name ) };
+        }
+        $params{SLA} ||= RT::SLA->GetDefaultServiceLevel( Queue => $QueueObj );
+    }
+
     # Delete null integer parameters
     foreach my $attr
         (qw(TimeWorked TimeLeft TimeEstimated InitialPriority FinalPriority))

commit b594fb0101639bcb038fcc5f17197833133f928f
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Jun 12 05:36:07 2020 +0800

    Add hint to SLA inputs for the priority mapping thing

diff --git a/share/html/Ticket/Create.html b/share/html/Ticket/Create.html
index 001ed549ca..debee27b3b 100644
--- a/share/html/Ticket/Create.html
+++ b/share/html/Ticket/Create.html
@@ -115,6 +115,7 @@
                     DefaultValue    => RT::SLA->GetDefaultServiceLevel(Queue => $QueueObj) ? 0 : 1,
                     QueueObj        => $QueueObj,
                 },
+                keys %{ RT->Config->Get('PriorityToSLA') || {} } ? ( hint => loc("Automatically set by Priority") ) : (),
             }),
         ]
         &>
diff --git a/share/html/Ticket/Elements/EditBasics b/share/html/Ticket/Elements/EditBasics
index ec276020e0..2d0351f8ad 100644
--- a/share/html/Ticket/Elements/EditBasics
+++ b/share/html/Ticket/Elements/EditBasics
@@ -104,6 +104,7 @@ unless ( @fields ) {
                 DefaultFromArgs => 0,
                 TicketObj => $TicketObj,
             },
+            keys %{ RT->Config->Get('PriorityToSLA') || {} } ? ( hint => loc("Automatically set by Priority") ) : (),
         }),
         # Time Estimated, Worked, and Left
         (

commit 4bfd5e3b505ea5300644f8edbfe7c3ef88d64291
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Jun 12 05:38:24 2020 +0800

    Automatically update SLA on priority change in web forms

diff --git a/share/html/Elements/JavascriptConfig b/share/html/Elements/JavascriptConfig
index a4eca5e6ac..fa451eb641 100644
--- a/share/html/Elements/JavascriptConfig
+++ b/share/html/Elements/JavascriptConfig
@@ -49,7 +49,7 @@
 my $Config = {};
 $Config->{$_} = RT->Config->Get( $_, $session{CurrentUser} )
   for qw(rtname WebPath MessageBoxRichText MessageBoxRichTextHeight
-         MaxAttachmentSize);
+         MaxAttachmentSize PriorityToSLA);
 
 my $CurrentUser = {};
 if ($session{CurrentUser} and $session{CurrentUser}->id) {
diff --git a/share/static/js/util.js b/share/static/js/util.js
index 382e336903..bab0e700fe 100644
--- a/share/static/js/util.js
+++ b/share/static/js/util.js
@@ -723,6 +723,42 @@ jQuery(function() {
             }
         });
     }
+
+    if ( !jQuery.isEmptyObject(RT.Config.PriorityToSLA) ) {
+        var priority_input = jQuery(':input[name=Priority], :input[name=InitialPriority]');
+        if ( priority_input.length ) {
+
+            var form = priority_input.closest('form');
+            var sla_input = form.find('select[name=SLA]');
+
+            if ( sla_input.length ) {
+                // readonly still doesn't work with select :/
+                sla_input.attr('disabled', true);
+                priority_input.change( function() {
+                    var value = jQuery(this).val();
+                    var sla = RT.Config.PriorityToSLA[value];
+                    if ( !sla && priority_input.is('select') ) {
+                        // Find priority string
+                        var value_string = priority_input.find('option:selected').text();
+                        sla = RT.Config.PriorityToSLA[value_string];
+                    }
+
+                    if ( sla ) {
+                        sla_input.val(sla).change();
+                        if ( !sla_input.attr('disabled') ) {
+                            sla_input.attr('disabled', true).selectpicker('refresh');
+                        }
+                    }
+                    else {
+                        // No corresponding SLA, allow user to edit
+                        if ( sla_input.attr('disabled') ) {
+                            sla_input.attr('disabled', false).selectpicker('refresh');
+                        }
+                    }
+                }).change();
+            }
+        }
+    }
 });
 
 /* inline edit */

commit 545fe12e631942069f21c4060d2b09a99fd42109
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Fri Jun 12 06:03:21 2020 +0800

    Add basic tests for %PriorityToSLA config

diff --git a/t/sla/priority_to_sla.t b/t/sla/priority_to_sla.t
new file mode 100644
index 0000000000..51014493e7
--- /dev/null
+++ b/t/sla/priority_to_sla.t
@@ -0,0 +1,26 @@
+use strict;
+use warnings;
+
+use RT::Test tests => undef;
+
+my $queue = RT::Test->load_or_create_queue( Name => 'General' );
+my ( $ret, $msg ) = $queue->SetSLADisabled(0);
+ok( $ret, 'Enable queue SLA' );
+
+RT->Config->Set( 'PriorityToSLA', Low => '3 days', Medium => '1 day', High => '4 hours', 80 => '8 hours' );
+my $ticket = RT::Test->create_ticket( Queue => $queue->Id );
+is( $ticket->SLA, '3 days', 'SLA is set to 3 days' );
+
+$ticket = RT::Test->create_ticket( Queue => $queue->Id, Priority => 50 );
+is( $ticket->SLA, '1 day', 'SLA is set to 1 day' );
+
+( $ret, $msg ) = $ticket->SetPriority(100);
+ok( $ret, 'Updated priority to 100' );
+is( $ticket->PriorityAsString, 'High',    'Priority is High' );
+is( $ticket->SLA,              '4 hours', 'SLA is set to 4 hours' );
+
+( $ret, $msg ) = $ticket->SetPriority(80);
+ok( $ret, 'Updated priority to 80' );
+is( $ticket->SLA, '8 hours', 'SLA is set to 8 hours' );
+
+done_testing;

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


More information about the rt-commit mailing list