From 9939ddf22c06f55693fe1a3a7c5dadfa4eabc6be Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 27 Jun 2013 18:56:31 +0200 Subject: [PATCH] Add a plugin for HipChat notification This plugin sends notification of build failure or success to a HipChat room, if the status differs from the last build. The plugin can be configured by adding one or more of these stanzas to hydra.conf: jobs = (patchelf|nixops):.*:.* room = 1234 token = 39ab2198fe... Here "jobs" is a regular expression against which the fully qualified job name of the build is matched (so for instance "nixops:master:tarball" will match the stanza above). --- release.nix | 2 + src/lib/Hydra/Plugin/HipChatNotification.pm | 71 +++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/lib/Hydra/Plugin/HipChatNotification.pm diff --git a/release.nix b/release.nix index 7842e8e6..ba47265a 100644 --- a/release.nix +++ b/release.nix @@ -76,6 +76,8 @@ rec { DigestSHA1 EmailSender FileSlurp + LWP + LWPProtocolHttps IOCompress IPCRun JSONXS diff --git a/src/lib/Hydra/Plugin/HipChatNotification.pm b/src/lib/Hydra/Plugin/HipChatNotification.pm new file mode 100644 index 00000000..13ef187f --- /dev/null +++ b/src/lib/Hydra/Plugin/HipChatNotification.pm @@ -0,0 +1,71 @@ +package Hydra::Plugin::HipChatNotification; + +use strict; +use parent 'Hydra::Plugin'; +use LWP::UserAgent; +use Hydra::Helper::CatalystUtils; + +sub buildFinished { + my ($self, $build, $dependents) = @_; + + my $cfg = $self->{config}->{hipchat}; + my @config = ref $cfg eq "ARRAY" ? @$cfg : ($cfg); + + my $baseurl = $self->{config}->{'base_uri'} || "http://localhost:3000"; + + # Figure out to which rooms to send notification. For each email + # room, we send one aggregate message. + my %rooms; + foreach my $b ($build, @{$dependents}) { + my $prevBuild = getPreviousBuild($b); + my $jobName = showJobName $b; + + foreach my $room (@config) { + next unless $jobName =~ /^$room->{jobs}$/; + + # If build is cancelled or aborted, do not send email. + next if $b->buildstatus == 4 || $b->buildstatus == 3; + + # If there is a previous (that is not cancelled or aborted) build + # with same buildstatus, do not send email. + next if defined $prevBuild && ($b->buildstatus == $prevBuild->buildstatus); + + $rooms{$room->{room}} //= { room => $room, builds => [] }; + push @{$rooms{$room->{room}}->{builds}}, $b; + } + } + + # Send a message to each room. + foreach my $roomId (keys %rooms) { + my $room = $rooms{$roomId}; + my @deps = grep { $_->id != $build->id } @{$room->{builds}}; + + my $imgBase = "http://hydra.nixos.org"; + my $img = + $build->buildstatus == 0 ? "$imgBase/static/images/checkmark_16.png" : + $build->buildstatus == 2 ? "$imgBase/static/images/dependency_16.png" : + $build->buildstatus == 4 ? "$imgBase/static/images/cancelled_16.png" : + "$imgBase/static/images/error_16.png"; + + my $msg = ""; + $msg .= " "; + $msg .= "Job ${\showJobName($build)}"; + $msg .= " (and ${\scalar @deps} others)" if scalar @deps > 0; + $msg .= ": " . showStatus($build) . ""; + + print STDERR "sending hipchat notification to room $roomId: $msg\n"; + + my $ua = LWP::UserAgent->new(); + my $resp = $ua->post('https://api.hipchat.com/v1/rooms/message?format=json&auth_token=' . $room->{room}->{token}, { + room_id => $roomId, + from => 'Hydra', + message => $msg, + message_format => 'html', + notify => 0, + color => $build->buildstatus == 0 ? 'green' : 'red' }); + + print STDERR $resp->status_line, ": ", $resp->decoded_content,"\n" if !$resp->is_success; + } +} + +1;