hydra/src/lib/Hydra/Plugin/CircleCINotification.pm

51 lines
1.5 KiB
Perl
Raw Normal View History

2017-04-19 15:20:09 -04:00
package Hydra::Plugin::CircleCINotification;
use strict;
2021-08-19 16:36:43 -04:00
use warnings;
2017-04-19 15:20:09 -04:00
use parent 'Hydra::Plugin';
use HTTP::Request;
use LWP::UserAgent;
use Hydra::Helper::CatalystUtils;
2021-10-19 22:53:39 -04:00
use JSON::MaybeXS;
2017-04-19 15:20:09 -04:00
sub isEnabled {
my ($self) = @_;
return defined $self->{config}->{circleci};
}
2017-04-19 15:20:09 -04:00
sub buildFinished {
my ($self, $topbuild, $dependents) = @_;
2017-04-19 15:20:09 -04:00
my $cfg = $self->{config}->{circleci};
my @config = defined $cfg ? ref $cfg eq "ARRAY" ? @$cfg : ($cfg) : ();
# Figure out to which branches to send notification.
my %branches;
foreach my $build ($topbuild, @{$dependents}) {
my $prevBuild = getPreviousBuild($build);
my $jobName = showJobName $build;
2017-04-19 15:20:09 -04:00
foreach my $branch (@config) {
my $force = $branch->{force};
next unless $jobName =~ /^$branch->{jobs}$/;
# If build is failed, don't trigger circleci
next if ! $force && $build->buildstatus != 0;
2017-04-19 15:20:09 -04:00
my $fullUrl = "https://circleci.com/api/v1.1/project/" . $branch->{vcstype} . "/" . $branch->{username} . "/" . $branch->{project} . "/tree/" . $branch->{branch} . "?circle-token=" . $branch->{token};
$branches{$fullUrl} = 1;
}
}
return if scalar keys %branches == 0;
# Trigger earch branch
my $ua = LWP::UserAgent->new();
foreach my $url (keys %branches) {
my $req = HTTP::Request->new('POST', $url);
$req->header('Content-Type' => 'application/json');
$ua->request($req);
}
}
1;