From f4b4ee575168df585cfb96a8d3f98a30f889afb8 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Wed, 19 Apr 2017 15:20:09 -0400 Subject: [PATCH] Add CircleCI plugin. --- src/lib/Hydra/Plugin/CircleCINotification.pm | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/lib/Hydra/Plugin/CircleCINotification.pm diff --git a/src/lib/Hydra/Plugin/CircleCINotification.pm b/src/lib/Hydra/Plugin/CircleCINotification.pm new file mode 100644 index 00000000..e42fb565 --- /dev/null +++ b/src/lib/Hydra/Plugin/CircleCINotification.pm @@ -0,0 +1,44 @@ +package Hydra::Plugin::CircleCINotification; + +use strict; +use parent 'Hydra::Plugin'; +use HTTP::Request; +use LWP::UserAgent; +use Hydra::Helper::CatalystUtils; +use JSON; + +sub buildFinished { + my ($self, $build, $dependents) = @_; + 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 $b ($build, @{$dependents}) { + my $prevBuild = getPreviousBuild($b); + my $jobName = showJobName $b; + + foreach my $branch (@config) { + my $force = $branch->{force}; + next unless $jobName =~ /^$branch->{jobs}$/; + + # If build is failed, don't trigger circleci + next if ! $force && $b->buildstatus != 0; + + 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;