mirror of
https://github.com/linuxserver/Heimdall.git
synced 2025-11-14 11:52:58 +09:00
Update to laravel 7
This commit is contained in:
@@ -5,10 +5,12 @@ namespace Github\HttpClient\Plugin;
|
||||
use Github\Exception\ApiLimitExceedException;
|
||||
use Github\Exception\ErrorException;
|
||||
use Github\Exception\RuntimeException;
|
||||
use Github\Exception\SsoRequiredException;
|
||||
use Github\Exception\TwoFactorAuthenticationRequiredException;
|
||||
use Github\Exception\ValidationFailedException;
|
||||
use Github\HttpClient\Message\ResponseMediator;
|
||||
use Http\Client\Common\Plugin;
|
||||
use Http\Promise\Promise;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
@@ -16,23 +18,25 @@ use Psr\Http\Message\ResponseInterface;
|
||||
* @author Joseph Bielawski <stloyd@gmail.com>
|
||||
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
||||
*/
|
||||
class GithubExceptionThrower implements Plugin
|
||||
final class GithubExceptionThrower implements Plugin
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @return Promise
|
||||
*/
|
||||
public function handleRequest(RequestInterface $request, callable $next, callable $first)
|
||||
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
|
||||
{
|
||||
return $next($request)->then(function (ResponseInterface $response) use ($request) {
|
||||
if ($response->getStatusCode() < 400 || $response->getStatusCode() > 600) {
|
||||
$this->checkGraphqlErrors($response);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
// If error:
|
||||
$remaining = ResponseMediator::getHeader($response, 'X-RateLimit-Remaining');
|
||||
if (null !== $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getRequestTarget(), 1, 10)) {
|
||||
$limit = ResponseMediator::getHeader($response, 'X-RateLimit-Limit');
|
||||
$reset = ResponseMediator::getHeader($response, 'X-RateLimit-Reset');
|
||||
if ((429 === $response->getStatusCode()) && null !== $remaining && 1 > $remaining && 'rate_limit' !== substr($request->getRequestTarget(), 1, 10)) {
|
||||
$limit = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Limit');
|
||||
$reset = (int) ResponseMediator::getHeader($response, 'X-RateLimit-Reset');
|
||||
|
||||
throw new ApiLimitExceedException($limit, $reset);
|
||||
}
|
||||
@@ -46,13 +50,13 @@ class GithubExceptionThrower implements Plugin
|
||||
$content = ResponseMediator::getContent($response);
|
||||
if (is_array($content) && isset($content['message'])) {
|
||||
if (400 === $response->getStatusCode()) {
|
||||
throw new ErrorException($content['message'], 400);
|
||||
throw new ErrorException(sprintf('%s (%s)', $content['message'], $response->getReasonPhrase()), 400);
|
||||
}
|
||||
|
||||
if (422 === $response->getStatusCode() && isset($content['errors'])) {
|
||||
$errors = [];
|
||||
foreach ($content['errors'] as $error) {
|
||||
switch ($error['code']) {
|
||||
switch ($error['code'] ?? null) {
|
||||
case 'missing':
|
||||
$errors[] = sprintf('The %s %s does not exist, for resource "%s"', $error['field'], $error['value'], $error['resource']);
|
||||
break;
|
||||
@@ -74,13 +78,24 @@ class GithubExceptionThrower implements Plugin
|
||||
break;
|
||||
|
||||
default:
|
||||
$errors[] = $error['message'];
|
||||
if (is_string($error)) {
|
||||
$errors[] = $error;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (isset($error['message'])) {
|
||||
$errors[] = $error['message'];
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
throw new ValidationFailedException('Validation Failed: '.implode(', ', $errors), 422);
|
||||
throw new ValidationFailedException(
|
||||
$errors ? 'Validation Failed: '.implode(', ', $errors) : 'Validation Failed',
|
||||
422
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +110,51 @@ class GithubExceptionThrower implements Plugin
|
||||
throw new RuntimeException(implode(', ', $errors), 502);
|
||||
}
|
||||
|
||||
if ((403 === $response->getStatusCode()) && $response->hasHeader('X-GitHub-SSO') && 0 === strpos((string) ResponseMediator::getHeader($response, 'X-GitHub-SSO'), 'required;')) {
|
||||
// The header will look something like this:
|
||||
// required; url=https://github.com/orgs/octodocs-test/sso?authorization_request=AZSCKtL4U8yX1H3sCQIVnVgmjmon5fWxks5YrqhJgah0b2tlbl9pZM4EuMz4
|
||||
// So we strip out the first 14 characters, leaving only the URL.
|
||||
// @see https://developer.github.com/v3/auth/#authenticating-for-saml-sso
|
||||
$url = substr((string) ResponseMediator::getHeader($response, 'X-GitHub-SSO'), 14);
|
||||
|
||||
throw new SsoRequiredException($url);
|
||||
}
|
||||
|
||||
throw new RuntimeException(isset($content['message']) ? $content['message'] : $content, $response->getStatusCode());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The graphql api doesn't return a 5xx http status for errors. Instead it returns a 200 with an error body.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
private function checkGraphqlErrors(ResponseInterface $response): void
|
||||
{
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = ResponseMediator::getContent($response);
|
||||
if (!is_array($content)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($content['errors']) || !is_array($content['errors'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
$errors = [];
|
||||
foreach ($content['errors'] as $error) {
|
||||
if (isset($error['message'])) {
|
||||
$errors[] = $error['message'];
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new RuntimeException(implode(', ', $errors));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user