forked from lix-project/hydra
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
This commit is contained in:
parent
941665044e
commit
c6f0753435
62
src/lib/Hydra/Plugin/GithubPulls.pm
Normal file
62
src/lib/Hydra/Plugin/GithubPulls.pm
Normal file
|
@ -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;
|
Loading…
Reference in a new issue