From c6f07534359dd567b6dfe9397dc22e1f90925397 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Wed, 24 May 2017 05:31:27 -0400 Subject: [PATCH] Add GithubPulls input plugin. This can be used with declarative projects to build PRs. The github_authorization section should contain verbatim Authorization header contents keyed by repo owner for private repos --- src/lib/Hydra/Plugin/GithubPulls.pm | 62 +++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/lib/Hydra/Plugin/GithubPulls.pm diff --git a/src/lib/Hydra/Plugin/GithubPulls.pm b/src/lib/Hydra/Plugin/GithubPulls.pm new file mode 100644 index 00000000..39700e88 --- /dev/null +++ b/src/lib/Hydra/Plugin/GithubPulls.pm @@ -0,0 +1,62 @@ +package Hydra::Plugin::GithubPulls; + +use strict; +use parent 'Hydra::Plugin'; +use HTTP::Request; +use LWP::UserAgent; +use JSON; +use Hydra::Helper::CatalystUtils; +use File::Temp; + +sub supportedInputTypes { + my ($self, $inputTypes) = @_; + $inputTypes->{'githubpulls'} = 'Open GitHub Pull Requests'; +} + +sub _iterate { + my ($url, $auth, $pulls, $ua) = @_; + my $req = HTTP::Request->new('GET', $url); + $req->header('Accept' => 'application/vnd.github.v3+json'); + $req->header('Authorization' => $auth) if defined $auth; + my $res = $ua->request($req); + my $content = $res->decoded_content; + die "Error pulling from the github pulls API: $content\n" + unless $res->is_success; + my $pulls_list = decode_json $content; + # TODO Stream out the json instead + foreach my $pull (@$pulls_list) { + $pulls->{$pull->{number}} = $pull; + } + # TODO Make Link header parsing more robust!!! + my @links = split ',', $res->header("Link"); + my $next = ""; + foreach my $link (@links) { + my ($url, $rel) = split ";", $link; + if (trim($rel) eq 'rel="next"') { + $next = substr trim($url), 1, -1; + last; + } + } + _iterate($next, $auth, $pulls, $ua) unless $next eq ""; +} + +sub fetchInput { + my ($self, $type, $name, $value, $project, $jobset) = @_; + return undef if $type ne "githubpulls"; + # TODO Allow filtering of some kind here? + (my $owner, my $repo) = split ' ', $value; + my $auth = $self->{config}->{github_authorization}->{$owner}; + my %pulls; + my $ua = LWP::UserAgent->new(); + _iterate("https://api.github.com/repos/$owner/$repo/pulls?per_page=100", $auth, \%pulls, $ua); + my $tempdir = File::Temp->newdir("github-pulls" . "XXXXX", TMPDIR => 1); + my $filename = "$tempdir/github-pulls.json"; + open(my $fh, ">", $filename) or die "Cannot open $filename for writing: $!"; + print $fh encode_json \%pulls; + close $fh; + my $storePath = `nix-store --add "$filename"` + or die "cannot copy path $filename to the Nix store.\n"; + return { storePath => $storePath }; +} + +1;