Customise displayed message in Slack from SVN integration

Viewed 1010

I'm pretty new to Slack. I've created a team, and I've added an integration with our SVN repository. I've set up a "post-commit" script and I get a notification in the intended Slack channel. All is good as far as that goes.

I would like to customise the notification in the slack channel, and I'm failing.

The post-commit script is Perl-based; a number of templates are available. I've used one and added a line to include the files changed thus:

my $files = `/usr/bin/svnlook changed -r $ARGV[1] $ARGV[0]`;
my $date = `/usr/bin/svnlook date -r $ARGV[1] $ARGV[0]`;
my $log = `/usr/bin/svnlook log -r $ARGV[1] $ARGV[0]`;
my $who = `/usr/bin/svnlook author -r $ARGV[1] $ARGV[0]`;
chomp $who;

my $payload = {
        'revision'      => $ARGV[1],
        'date'          => $date,
        'author'        => $who,
        'log'           => $log,
        'files'         => $files,
};

my $ua = LWP::UserAgent->new;
$ua->timeout(15);

my $req = POST( "https://${opt_domain}/services/hooks/subversion?token=${opt_token}", ['payload' => encode_json($payload)] );

The only message I get in the Slack channel is of the form:

<rev number>:<author>-<log message>

I don't know why I don't get the date and the list of files changed appear in the Slack message. The relevant svnlook commands, when run manually on the server produce the expected results. If I change the relevant bit of the script to:

my $payload = {
        'wibble'      => $ARGV[1],
        'date'          => $date,
        'author'        => $who,
        'log'           => $log,
        'files'         => $files,
};

and then manually invoke the hook with

post-commit /var/svn/repo 5966

(for example) I get an output that includes:

payload=%7B%22wibble%22%3A%225966%22

(as I expected/hoped), but then

Response:
HTTP/1.1 500 Server Error
Connection: close
...
X-Cache: Error from cloudfront
X-Frame-Options: SAMEORIGIN

invalid_payload

I was naively expecting the payload to specify the message, so you could craft a message something like:

revision: 5696
date: 2016/03/14 12:00:00
author: ...
log: <log text>
files: myfile.c, myfile.h

In the above example, I was wondering if I'd get

wibble: 5696

So, I have to come to the conclusion that something at the Slack end requires only certain fields in the received JSON and then formats them into the channel message according to some rule. I have found NO clue what fields are acceptable and how one might exercise some control over the formatting. For example, the fact I don't get an error about an invalid payload suggests that the date and files field names are valid, but how do I get them to appear in the Slack message?

2 Answers

Came across wanting to do the same thing. What I ended doing was changing the $payload's log value to include $files ... like so:

my $files = `/usr/bin/svnlook changed -r $ARGV[1] $ARGV[0]`;
my $payload = {
    'revision'      => $ARGV[1],
    'url'           => $url,
    'author'        => $who,
    'log'           => "$log\n$files"
};

(starting with the sample here: https://github.com/tinyspeck/services-examples/blob/master/subversion.pl)

Related