使用Perl构建SVN提交后挂钩:IRC Bot打印提交消息

时间:2010-02-08 19:38:40

标签: perl svn hook commit irc

我正在尝试构建一个IRC Bot,它在私有通道中告诉我每个我想知道的提交消息。但我很难得到

#!/bin/bash 
REPOS="$1" 
REV="$2" 
# call bot with arguments reposname, revison and commit message in one string 
/usr/bin/perl /home/user/repo/svn_irc_bot.pl "$REPOS" "$REV" 
# all checks passed, so allow the commit 
exit 0

然后,调用Perl-Skript:

#!/usr/bin/perl -w
# see http://www.javalinux.it/wordpress/2009/10/15/writing-an-irc-bot-for-svn-commit-notification/
# see http://oreilly.com/pub/h/1964
use strict;
# We will use a raw socket to connect to the IRC server.
use IO::Socket;
my $repos = $ARGV[0];
my $rev = $ARGV[1];
my $commit = `/usr/bin/svnlook log $repos`;
my $user = `whoami`;
# The server to connect to and our details.
my $server = "irc.server.com";
my $nick = "bot2";
my $login = "bot2";
# The channel which the bot will join.
# my $channel = "#channel";
# Connect to the IRC server.
my $sock = new IO::Socket::INET(PeerAddr => $server,
                                PeerPort => 6667,
                                Proto => 'tcp') or
                                die "Can't connect\n";
# Log on to the server.
print $sock "NICK $nick\r\n";
print $sock "USER $login 8 * :Perl IRC Hacks Robot\r\n";
# Read lines from the server until it tells us we have connected.
while (my $input = <$sock>) {
    # Check the numerical responses from the server.
    if ($input =~ /004/) {
        # We are now logged in.
        print $sock "PRIVMSG mynick : $user: $repos r$rev -- $commit\n";
        last;
    }
    elsif ($input =~ /433/) {
        die "Nickname is already in use.";
    }
}
sleep(5);
print $sock "QUIT bye... \n";
sleep(5);
close($sock);

所以,我的Bot确实连接了,可以跟我说话......

如果我手动启动shell脚本,则只发送一个单词($ user内的字符串,甚至不是后面的冒号)。

如果SVN通过提交调用脚本,似乎$ user和$ commit字符串为空,$ user和$ repos被传输...

我想我对whoami和svnlook的使用有问题......但我无法弄明白。也许有人可以给我一个提示?

3 个答案:

答案 0 :(得分:1)

您只使用whoami而不是命令的完整路径,但无法保证SVN调用脚本时,$PATH env变量将包含与你的一个人有一个。

您应该检查的另一件事是SVN运行脚本的uid有权使用svnlook并访问存储库。

不确定您的问题是否来自此问题,但它确实是开始查看的好地方。

答案 1 :(得分:1)

你只获得没有前面冒号的“$ user”的原因是因为你正在从whoami捕获输出,并且该输出包含换行符。该换行符被解释为要发送的字符串的结尾。在使用chomp $user之前,请尝试$user删除换行符。

  

如果SVN槽调用脚本   提交,似乎是$ user和   $ commit字符串为空,$ user和   $ repos传输......

我将假设你的意思是通过SVN $user$commit是空的,但$rev$repos被传输,因为这是有意义的..

来自$commit的{​​{1}}会遇到同样的问题,但由于提交是在邮件末尾提出的,因此如果您的邮件中包含换行符,则只会出现问题。例如,如果邮件的第一行是换行符,则您将看不到任何内容。为此,我建议删除邮件中的所有换行符,可能是svnlook

至于y/\n//在钩子内是空白的,这取决于你如何使用svn。 $user完全有可能无法找到用户ID,例如,如果运行挂钩的进程与任何登录无关。在这种情况下,您可能需要另一种确定用户的方法,例如whoami的第一行输出。

答案 2 :(得分:0)

我不确定,但尝试两件事。

首先检查您正在运行的文件的权限。如果他们没有权限运行whoami和svnlookup那么你就差点搞砸了。 其次只是给qx(cmd)一个镜头而不是cmd