From ae2dd755319901861b468f36ce55279f58bf9be4 Mon Sep 17 00:00:00 2001 From: Zoffix Znet Date: Sat, 7 Nov 2015 20:22:41 -0500 Subject: [PATCH] Handle GitHub API Redirects The API docs say any request can result in a redirect. Not handling this made us fail handling a couple of modules in the eco --- web/lib/P6Project/Hosts/Github.pm | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/web/lib/P6Project/Hosts/Github.pm b/web/lib/P6Project/Hosts/Github.pm index 500e2e6..bcab311 100644 --- a/web/lib/P6Project/Hosts/Github.pm +++ b/web/lib/P6Project/Hosts/Github.pm @@ -57,13 +57,24 @@ sub get_api { if ($call) { $url .= $call; } - my $tx = $self->p6p->ua->get($url, {Authorization => "token $github_token"}); - if (! $tx->success ) { - my $error = $self->_format_error($tx->error); - $self->p6p->stats->error("Error for project $project->{name} : could not get $url: $error"); - return; + + FETCH_URL: { + my $tx = $self->p6p->ua->get($url, {Authorization => "token $github_token"}); + if (! $tx->success ) { + my $error = $self->_format_error($tx->error); + $self->p6p->stats->error("Error for project $project->{name} : could not get $url: $error"); + return; + } + + my $res = $tx->res->json; + do { $url = $res->{url}; redo FETCH_URL; } + if ref $res eq 'HASH' + and $res->{message} + and $res->{message} eq 'Moved Permanently' + and $url ne $res->{url}; + + return $res; } - return $tx->res->json; } sub file_url {