Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: NixOS/hydra
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 53e1294825c3
Choose a base ref
...
head repository: NixOS/hydra
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9668bc01da33
Choose a head ref
  • 4 commits
  • 1 file changed
  • 2 contributors

Commits on Mar 26, 2020

  1. Verified

    This commit was signed with the committer’s verified signature.
    edolstra Eelco Dolstra
    Copy the full SHA
    956f009 View commit details
  2. Refactor code

    Extract the conditions before the loop, as they do not change due to channel
    definition.
    knl committed Mar 26, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    986fde8 View commit details
  3. Add debug logging

    This will help us track potential problems with the plugin.
    knl committed Mar 26, 2020
    Copy the full SHA
    1bee6e3 View commit details

Commits on Apr 1, 2020

  1. Merge pull request #726 from knl/document-slack-notifications-plugin

    Document SlackNotifications plugin
    edolstra authored Apr 1, 2020
    1
    Copy the full SHA
    9668bc0 View commit details
Showing with 67 additions and 6 deletions.
  1. +67 −6 src/lib/Hydra/Plugin/SlackNotification.pm
73 changes: 67 additions & 6 deletions src/lib/Hydra/Plugin/SlackNotification.pm
Original file line number Diff line number Diff line change
@@ -7,6 +7,53 @@ use LWP::UserAgent;
use Hydra::Helper::CatalystUtils;
use JSON;

=head1 NAME
SlackNotification - hydra-notify plugin for sending Slack notifications about
build results
=head1 DESCRIPTION
This plugin reports build statuses to various Slack channels. One can configure
which builds are reported to which channels, and whether reports should be on
state change (regressions and improvements), or for each build.
=head1 CONFIGURATION
The module is configured using the C<slack> block in Hydra's config file. There
can be multiple such blocks in the config file, each configuring different (or
even the same) set of builds and how they report to Slack channels.
The following entries are recognized in the C<slack> block:
=over 4
=item jobs
A pattern for job names. All builds whose job name matches this pattern will
emit a message to the designated Slack channel (see C<url>). The pattern will
match the whole name, thus leaving this field empty will result in no
notifications being sent. To match on all builds, use C<.*>.
=item url
The URL to a L<Slack incoming webhook|https://api.slack.com/messaging/webhooks>.
Slack administrators have to prepare one incoming webhook for each channel. This
URL should be treated as secret, as anyone knowing the URL could post a message
to the Slack workspace (or more precisely, the channel behind it).
=item force
(Optional) An I<integer> indicating whether to report on every build or only on
changes in the status. If not provided, defaults to 0, that is, sending reports
only when build status changes from success to failure, and vice-versa. Any
other value results in reporting on every build.
=back
=cut

sub isEnabled {
my ($self) = @_;
return defined $self->{config}->{slack};
@@ -40,20 +87,32 @@ sub buildFinished {
# we send one aggregate message.
my %channels;
foreach my $b ($build, @{$dependents}) {
my $prevBuild = getPreviousBuild($b);
my $jobName = showJobName $b;
my $buildStatus = $b->buildstatus;
my $cancelledOrAborted = $buildStatus == 4 || $buildStatus == 3;

my $prevBuild = getPreviousBuild($b);
my $sameAsPrevious = defined $prevBuild && ($buildStatus == $prevBuild->buildstatus);
my $prevBuildStatus = (defined $prevBuild) ? $prevBuild->buildstatus : -1;
my $prevBuildId = (defined $prevBuild) ? $prevBuild->id : -1;

print STDERR "SlackNotification_Debug job name $jobName status $buildStatus (previous: $prevBuildStatus from $prevBuildId)\n";

foreach my $channel (@config) {
my $force = $channel->{force};
next unless $jobName =~ /^$channel->{jobs}$/;

# If build is cancelled or aborted, do not send email.
next if ! $force && ($b->buildstatus == 4 || $b->buildstatus == 3);
my $force = $channel->{force};

print STDERR "SlackNotification_Debug found match with '$channel->{jobs}' with force=$force\n";

# If build is cancelled or aborted, do not send Slack notification.
next if ! $force && $cancelledOrAborted;

# If there is a previous (that is not cancelled or aborted) build
# with same buildstatus, do not send email.
next if ! $force && defined $prevBuild && ($b->buildstatus == $prevBuild->buildstatus);
# with same buildstatus, do not send Slack notification.
next if ! $force && $sameAsPrevious;

print STDERR "SlackNotification_Debug adding $jobName to the report list\n";
$channels{$channel->{url}} //= { channel => $channel, builds => [] };
push @{$channels{$channel->{url}}->{builds}}, $b;
}
@@ -93,6 +152,8 @@ sub buildFinished {
$text .= join(" or ", scalar @x > 1 ? join(", ", @x[0..scalar @x - 2]) : (), $x[-1]);
}

print STDERR "SlackNotification_Debug POSTing to url ending with: ${\substr $url, -8}\n";

my $msg =
{ attachments =>
[{ fallback => "Job " . showJobName($build) . " build number " . $build->id . ": " . showStatus($build),