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: adf59a395993
Choose a base ref
...
head repository: NixOS/hydra
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1c44de1779d0
Choose a head ref
  • 2 commits
  • 1 file changed
  • 2 contributors

Commits on Jun 22, 2018

  1. Copy the full SHA
    f738f6f View commit details

Commits on Nov 6, 2018

  1. Merge pull request #567 from phile314/bitbucket_pulls

    Add BitBucket pull request support
    edolstra authored Nov 6, 2018

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    1c44de1 View commit details
Showing with 57 additions and 0 deletions.
  1. +57 −0 src/lib/Hydra/Plugin/BitBucketPulls.pm
57 changes: 57 additions & 0 deletions src/lib/Hydra/Plugin/BitBucketPulls.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package Hydra::Plugin::BitBucketPulls;

use strict;
use parent 'Hydra::Plugin';
use HTTP::Request;
use LWP::UserAgent;
use JSON;
use Hydra::Helper::CatalystUtils;
use File::Temp;
use POSIX qw(strftime);

sub supportedInputTypes {
my ($self, $inputTypes) = @_;
$inputTypes->{'bitbucketpulls'} = 'Open BitBucket Pull Requests';
}

sub _iterate {
my ($url, $auth, $pulls, $ua) = @_;
my $req = HTTP::Request->new('GET', $url);
$req->header('Authorization' => $auth) if defined $auth;
my $res = $ua->request($req);
my $content = $res->decoded_content;
die "Error pulling from the bitbucket pulls API: $content\n"
unless $res->is_success;
my $response = decode_json $content;
my $pulls_list = $response->{values};
# TODO Stream out the json instead
foreach my $pull (@$pulls_list) {
$pulls->{$pull->{id}} = $pull;
}
my $next = $response->{next};
_iterate($next, $auth, $pulls, $ua) unless $next eq "";
}

sub fetchInput {
my ($self, $type, $name, $value, $project, $jobset) = @_;
return undef if $type ne "bitbucketpulls";
# TODO Allow filtering of some kind here?
(my $owner, my $repo) = split ' ', $value;
my $auth = $self->{config}->{bitbucket_authorization}->{$owner};
my %pulls;
my $ua = LWP::UserAgent->new();
_iterate("https://api.bitbucket.com/2.0/repositories/$owner/$repo/pullrequests?state=OPEN", $auth, \%pulls, $ua);
my $tempdir = File::Temp->newdir("bitbucket-pulls" . "XXXXX", TMPDIR => 1);
my $filename = "$tempdir/bitbucket-pulls.json";
open(my $fh, ">", $filename) or die "Cannot open $filename for writing: $!";
print $fh encode_json \%pulls;
close $fh;
system("jq -S . < $filename > $tempdir/bitbucket-pulls-sorted.json");
my $storePath = trim(`nix-store --add "$tempdir/bitbucket-pulls-sorted.json"`
or die "cannot copy path $filename to the Nix store.\n");
chomp $storePath;
my $timestamp = time;
return { storePath => $storePath, revision => strftime "%Y%m%d%H%M%S", gmtime($timestamp) };
}

1;