From 1c5ab05521924a20a9dba7ef75aabdd74ebd897c Mon Sep 17 00:00:00 2001 From: Rob Vermaas Date: Thu, 15 Oct 2009 13:23:15 +0000 Subject: [PATCH] * added support for twitter notification 3 environment variables are important: TWITTER_USER TWITTER_PASS HYDRA_BUILD_BASEURL - twitter notification is off when TWITTER_USER and TWITTER_PASS are not defined - if HYDRA_BUILD_BASEURL is not defined, no URL is put in the twitter messages --- deps.nix | 1 + src/script/hydra_build.pl | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/deps.nix b/deps.nix index ad37ab72..f9caefb7 100644 --- a/deps.nix +++ b/deps.nix @@ -18,5 +18,6 @@ with pkgs; perlPackages.EmailSender perlPackages.EmailSimpleCreator perlPackages.TextTable + perlPackages.NetTwitterLite ] diff --git a/src/script/hydra_build.pl b/src/script/hydra_build.pl index af519a6c..4ed9ebe8 100755 --- a/src/script/hydra_build.pl +++ b/src/script/hydra_build.pl @@ -13,6 +13,7 @@ use Sys::Hostname::Long; use Config::General; use Text::Table; use POSIX qw(strftime); +use Net::Twitter::Lite; STDOUT->autoflush(); @@ -29,6 +30,35 @@ sub getBuildLog { return -e $logPath ? $logPath : undef; } +sub sendTwitterNotification { + my ($build) = @_; + + return unless (defined $ENV{'TWITTER_USER'} && defined $ENV{'TWITTER_PASS'}); + + my $addURL = defined $ENV{'HYDRA_BUILD_BASEURL'}; + + my $jobName = $build->project->name . ":" . $build->jobset->name . ":" . $build->job->name; + my $status = $build->resultInfo->buildstatus == 0 ? "SUCCEEDED" : "FAILED"; + my $system = $build->system; + my $duration = ($build->resultInfo->stoptime - $build->resultInfo->starttime) . " seconds"; + my $url = $ENV{'HYDRA_BUILD_BASEURL'}."/".$build->id ; + + my $nt = Net::Twitter::Lite->new( + username => $ENV{'TWITTER_USER'}, + password => $ENV{'TWITTER_PASS'} + ); + + my $tag = $build->project->name; + my $msg = "$jobName ($system): $status in $duration #$tag"; + if (length($msg) + 1 + length($url) <= 140) { + $msg = "$msg $url" ; + } + + eval { + my $result = eval { $nt->update($msg) }; + }; + warn "$@\n" if $@; +} sub sendEmailNotification { my ($build) = @_; @@ -374,6 +404,7 @@ sub doBuild { }); sendEmailNotification $build; + sendTwitterNotification $build; } @@ -384,6 +415,10 @@ if ($ENV{'HYDRA_MAIL_TEST'}) { sendEmailNotification $db->resultset('Builds')->find($buildId); exit 0; } +if ($ENV{'HYDRA_TWITTER_TEST'}) { + sendTwitterNotification $db->resultset('Builds')->find($buildId); + exit 0; +} # Lock the build. If necessary, steal the lock from the parent # process (runner.pl). This is so that if the runner dies, the