2

I downloaded the sample SVN post-commit hook provided by Slack integration.

#!/usr/bin/perl

use warnings;
use strict;

use HTTP::Request::Common qw(POST);
use HTTP::Status qw(is_client_error);
use LWP::UserAgent;
use JSON;

my $repository = "myrepo";
my $websvn = "websvn.mydomain.com";
my $opt_domain = "myteam.slack.com";
my $opt_token = "mytoken";

my $log = qx|export LC_ALL="cs_CZ.UTF-8"; /usr/bin/svnlook log -r $ARGV[1] $ARGV[0]|;
my $log = $log." ".unpack('H*',$log);

my $who = `/usr/bin/svnlook author -r $ARGV[1] $ARGV[0]`;
my $url = "http://${websvn}/revision.php?repname=${repository}&rev=$ARGV[1]";
chomp $who;

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

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

my $req = POST( "https://${opt_domain}/services/hooks/subversion?token=${opt_token}", ['payload' => encode_json($payload)] );
my $s = $req->as_string;
print STDERR "Request:\n$s\n";

my $resp = $ua->request($req);
$s = $resp->as_string;
print STDERR "Response:\n$s\n";

(full file here: https://github.com/tinyspeck/services-examples/blob/master/subversion.pl)

Now the problem is, that if I want to commit message containing special characters (Czech), the string is unable to translate properly and the resulting message in slack channel looks like this:

25: falnyr - ÅeÅicha
c59865c5996963686120746573746f766163c3ad20636f6d6d69740a

I have read about the isolated (vacuum) SVN hook environment, so I assume I need to declare the locale inside the script, but since I am untouched by Perl, I really don`t know how.

My commit attempt:

falnyr@cap:test $ export LC_ALL="cs_CZ.UTF-8"
falnyr@cap:test $ touch file.txt
falnyr@cap:test $ svn add file.txt
A         file.txt
falnyr@cap:test $ svn commit -m "Řeřicha"
Store password unencrypted (yes/no)? no
Adding         file.txt
Transmitting file data .
Committed revision x.
falnyr@cap:test $
Jan Richter
  • 1,976
  • 4
  • 29
  • 49
  • Are the characters added as ISO-8859-2 or UTF-8 or something else? – choroba Oct 15 '15 at 19:44
  • I think the environment has no information of the character encoding since its empty, that is what I am trying to figure out. – Jan Richter Oct 15 '15 at 19:47
  • [Encode::Guess](http://p3rl.org/Encode::Guess) might help with unknown encodings. – choroba Oct 15 '15 at 19:50
  • @choroba: Edited the comment the way you provided, check edit – Jan Richter Oct 15 '15 at 20:52
  • But it's already the svn commit which failed. How should the post commit hook fix that? – yacc Oct 16 '15 at 11:24
  • I have the confirmation from my colleague that even if the commit was successful, the string is faulty. (Commiting via TortoiseSVN @ Windows) – Jan Richter Oct 16 '15 at 19:51
  • @falnyr And what happens if you're going to use TortoiseSVN entirely to commit a file and view its log history? Are you able to use TortoiseSVN with Czech characters? – yacc Oct 17 '15 at 09:54
  • @yacc czech characters are fine, the whole work with TortoiseSVN and the repository works for years now, I dont know if the problem could be that I am commiting via console, but the same result to Slack was with TortoiseSVN commit, so I suppose it has something to do with the script. – Jan Richter Oct 17 '15 at 12:17
  • @falnyr Maybe I'm wrong, but to me it seems you're dealing with two problems here. If svn defies Czech chars but Tortoise not, it seems that Tortoise uses a precommit alteration of the string. It would be interesting to have a look at the final value in the repos. And then, to stop Slack from hiccuping you'd have to mimic T's transformation. – yacc Oct 17 '15 at 12:37
  • @yacc I don`t think there is a TortoiseSVN pre-commit alteration since I am using a SVN plugin in SublimeText to commit on Ubuntu with czech characters without a problem. What does it mean to mimic T's transformation? – Jan Richter Oct 17 '15 at 13:22
  • @falnyr What happens if you try `$log = qx|/usr/bin/svnlook log -r $ARGV[1] $ARGV[0]|;` in above hook? And could you add this directly after previous line: `$log=join(",",$log,unpack('H*',$log));` – yacc Oct 17 '15 at 14:18
  • @yacc added the lines you provided, updated question ^ (no result so far) – Jan Richter Oct 17 '15 at 20:43
  • @falnyr Ok, sure that's no progress so far but we're on track. What do you get if you add another line: `$log=pack('H*','c59865c59969636861')." ".unpack('H*',"Řeřicha");` – yacc Oct 18 '15 at 12:27
  • @yacc the output is: 18 - falnyr - ÅeÅicha c59865c59969636861 – Jan Richter Oct 18 '15 at 21:48
  • And if you do this entirely in the console: `svn commit -m "Řeřicha"; svnlook log -r x file.txt` (take care of the right revision and path). – yacc Oct 19 '15 at 09:24
  • @yacc Committed revision 19. svnlook: E000020: Can't open file 'qwerty.txt/format': Not a directory – Jan Richter Oct 19 '15 at 15:00
  • @falnyr That's not the right path. Could you add this to the hook script (similar to the previous additions): `$log = "@ARGV";` so we get the exact arguments used for svnlook. – yacc Oct 19 '15 at 15:07
  • @yacc Slack output: 24: falnyr - /var/svn/test 24 – Jan Richter Oct 19 '15 at 16:17
  • Then you should do this in the console: `export LC_ALL="cs_CZ.UTF-8"; svnlook log -r 24 /var/svn/test` – yacc Oct 19 '15 at 16:26
  • @yacc export LC_ALL="cs_CZ.UTF-8"; svnlook log -r 24 /var/svn/test Řeřicha – Jan Richter Oct 19 '15 at 17:01
  • Then put this into the hook: `$log = qx|export LC_ALL="cs_CZ.UTF-8"; /usr/bin/svnlook log -r $ARGV[1] $ARGV[0]|;` followed by `$log=$log." ".unpack('H*',$log);` and observe the slack message. I hope it's UTF8 now. From there you need a way to pass the UTF8 string safely down the slack line. I'm off for today, bye. – yacc Oct 19 '15 at 17:36
  • @yacc added the code you provided, still bad encoding in slack (check updated question) – Jan Richter Oct 19 '15 at 21:16

1 Answers1

1

Add the following lines to your hook. Slack should now be able to talk Czech. :)

use Encode qw(decode_utf8);
...
my $log = qx|export LC_ALL="cs_CZ.UTF-8"; /usr/bin/svnlook log -r $ARGV[1] $ARGV[0]|;
$log = decode_utf8($log);
yacc
  • 2,915
  • 4
  • 19
  • 33