From fd8cb39a118165b1d8b0cc9e835d9c1a10c5c323 Mon Sep 17 00:00:00 2001 From: Chaker Benhamed Date: Tue, 4 Jul 2017 10:07:25 +0100 Subject: [PATCH] Add BitBucket plugin This plugin will post to the build status system in BitBucket. In order to use it you need to add to ExtraConfig username = bitbucket_username password = bitbucket_password You can use an application password https://blog.bitbucket.org/2016/06/06/app-passwords-bitbucket-cloud/ --- src/lib/Hydra/Plugin/BitBucketStatus.pm | 69 +++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/lib/Hydra/Plugin/BitBucketStatus.pm diff --git a/src/lib/Hydra/Plugin/BitBucketStatus.pm b/src/lib/Hydra/Plugin/BitBucketStatus.pm new file mode 100644 index 00000000..f4419b2e --- /dev/null +++ b/src/lib/Hydra/Plugin/BitBucketStatus.pm @@ -0,0 +1,69 @@ +package Hydra::Plugin::BitBucketStatus; + +use strict; +use parent 'Hydra::Plugin'; +use HTTP::Request; +use JSON; +use LWP::UserAgent; +use Hydra::Helper::CatalystUtils; + +sub toBitBucketState { + my ($buildStatus) = @_; + if ($buildStatus == 0) { + return "SUCCESSFUL"; + } else { + return "FAILED"; + } +} + +sub common { + my ($self, $build, $dependents, $finished) = @_; + my $bitbucket = $self->{config}->{bitbucket}; + my $baseurl = $self->{config}->{'base_uri'} || "http://localhost:3000"; + + foreach my $b ($build, @{$dependents}) { + my $jobName = showJobName $b; + my $evals = $build->jobsetevals; + my $ua = LWP::UserAgent->new(); + my $body = encode_json( + { + state => $finished ? toBitBucketState($b->buildstatus) : "INPROGRESS", + url => "$baseurl/build/" . $b->id, + name => $jobName, + key => $jobName, + description => "Hydra build #" . $b->id . " of $jobName", + }); + while (my $eval = $evals->next) { + foreach my $i ($eval->jobsetevalinputs){ + next unless defined $i; + my $uri = $i->uri; + my $rev = $i->revision; + # Skip if the uri is not a bitbucket repo + next unless index($uri, 'bitbucket') != -1; + $uri =~ m![:/]([^/]+)/([^/]+?)?$!; + my $owner = $1; + my $repo = $2; + my $req = HTTP::Request->new('POST', "https://api.bitbucket.org/2.0/repositories/$owner/$repo/commit/$rev/statuses/build"); + $req->header('Content-Type' => 'application/json'); + $req->authorization_basic($bitbucket->{username}, $bitbucket->{password}); + $req->content($body); + my $res = $ua->request($req); + print STDERR $res->status_line, ": ", $res->decoded_content, "\n" unless $res->is_success; + } + } + } +} + +sub buildQueued { + common(@_, [], 0); +} + +sub buildStarted { + common(@_, [], 0); +} + +sub buildFinished { + common(@_, 1); +} + +1;