[Bps-public-commit] rt-extension-notifyslack branch, master, updated. 144a9bfe7dc9da796632f77ccc58093197482e45

Maureen Mirville maureen at bestpractical.com
Wed Jul 18 19:41:03 EDT 2018


The branch, master has been updated
       via  144a9bfe7dc9da796632f77ccc58093197482e45 (commit)
       via  0ead016a751c404175247bbaee804d629f82fcae (commit)
      from  a34fe7814bbb6f7decdfee21c2a314d1b48ab185 (commit)

Summary of changes:
 etc/initialdata              |  1 +
 lib/RT/Action/NotifySlack.pm | 25 ++++++++++++++++++++-----
 2 files changed, 21 insertions(+), 5 deletions(-)

- Log -----------------------------------------------------------------
commit 0ead016a751c404175247bbaee804d629f82fcae
Author: Maureen E. Mirville <maureen at bestpractical.com>
Date:   Wed Jul 18 19:04:27 2018 -0400

    Add Slack 'channel' as ScripAction argument
    
    Users can select which channel to post RT notifications in slack
    based on a lookup of the values stored in the RT %SlackWebHookUrls
    config.

diff --git a/etc/initialdata b/etc/initialdata
index 5477ac3..fe152b8 100644
--- a/etc/initialdata
+++ b/etc/initialdata
@@ -3,5 +3,6 @@ push @ScripActions, (
         Name        => 'Notify Slack',
         Description => 'Post ticket updates to slack channel',
         ExecModule  => 'NotifySlack',
+        Argument    => '#general',
     },
 );
diff --git a/lib/RT/Action/NotifySlack.pm b/lib/RT/Action/NotifySlack.pm
index 6130880..33966bf 100644
--- a/lib/RT/Action/NotifySlack.pm
+++ b/lib/RT/Action/NotifySlack.pm
@@ -19,18 +19,24 @@ sub Prepare  {
 sub Commit {
     my $self = shift;
 
-    my $webhook_url = RT->Config->Get( 'SlackWebHookUrl' );
-    my $default_channel = RT->Config->Get( 'SlackDefaultChannel' );
-
+    my $webhook_urls = RT->Config->Get( 'SlackWebHookUrls' ) || {};
+    my $channel = $self->Argument;
+
+    my $webhook_url = undef;
+    for my $key ( keys %{ $webhook_urls } ) {
+        if ( $key eq $channel ) {
+            $webhook_url = $webhook_urls->{ $key };
+        }
+    }
     my $ua = LWP::UserAgent->new;
     $ua->timeout(15);
 
     my $slack_message = 'Ticket #' . $self->TicketObj->id . ' updated';
 
     my $payload = {
-        channel => $default_channel // $default_channel,
         text => $slack_message,
     };
+    return RT::Logger->error( 'Slack channel: ' . $channel .  ' not found. Check %SlackWebHookUrls config values.' ) unless $webhook_url;
 
     my $req = POST("$webhook_url", ['payload' => encode_json($payload)]);
 

commit 144a9bfe7dc9da796632f77ccc58093197482e45
Author: Maureen E. Mirville <maureen at bestpractical.com>
Date:   Wed Jul 18 19:15:39 2018 -0400

    Update message posted to slack
    
    To be more consistent, messages posted to slack are mirrored with
    the notification message posted in RT. Additionally, Slack messages
    contain links directing back to the ticket.

diff --git a/lib/RT/Action/NotifySlack.pm b/lib/RT/Action/NotifySlack.pm
index 33966bf..908753e 100644
--- a/lib/RT/Action/NotifySlack.pm
+++ b/lib/RT/Action/NotifySlack.pm
@@ -31,7 +31,16 @@ sub Commit {
     my $ua = LWP::UserAgent->new;
     $ua->timeout(15);
 
-    my $slack_message = 'Ticket #' . $self->TicketObj->id . ' updated';
+    my $ticket = $self->TicketObj;
+    my $rt_url = RT->Config->Get( 'WebURL' )."Ticket/Display.html?id=".$ticket->id;
+    my $txn = $self->TransactionObj;
+
+    # Slack uses the format <www.example.com|Example Text> to insert a link into the payload's text
+    my $slack_message = '<'.$rt_url.'|Ticket #'.$ticket->id.'>: '.$txn->BriefDescription;
+
+    if ( $txn->Type eq 'Comment' || $txn->Type eq 'Correspond' ) {
+        $slack_message = $slack_message . ' on <'.$rt_url.'#txn-'.$txn->id.'| #txn-'.$txn->id.'>';
+    }
 
     my $payload = {
         text => $slack_message,

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


More information about the Bps-public-commit mailing list