Skip to content

Commit

Permalink
Set HTTP timeouts and error handling on latest release lookup #6
Browse files Browse the repository at this point in the history
  • Loading branch information
sedan07 committed Jan 26, 2021
1 parent f16aaf7 commit 1478c0d
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 9 deletions.
12 changes: 8 additions & 4 deletions app/Http/Controllers/Api/GeneralController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ public function version()
{
$latest = app()->make(Releases::class)->latest();

return $this->setMetaData([
'on_latest' => version_compare(CACHET_VERSION, $latest['tag_name']) === 1,
'latest' => $latest,
])->item(CACHET_VERSION);
if (array_key_exists('tag_name', $latest)) {
$this->setMetaData([
'on_latest' => version_compare(CACHET_VERSION, $latest['tag_name']) === 1,
'latest' => $latest,
]);
}

return $this->item(CACHET_VERSION);
}

/**
Expand Down
38 changes: 34 additions & 4 deletions app/Integrations/GitHub/Releases.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CachetHQ\Cachet\Integrations\Contracts\Releases as ReleasesContract;
use GuzzleHttp\Client;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Facades\Log;

class Releases implements ReleasesContract
{
Expand All @@ -32,6 +33,14 @@ class Releases implements ReleasesContract
*/
const FAILED = 1;

/**
* The GuzzleHTTP client instance for making the requests
*
* @var \GuzzleHttp\Client
*
*/
protected $client;

/**
* The cache repository instance.
*
Expand All @@ -56,14 +65,16 @@ class Releases implements ReleasesContract
/**
* Creates a new releases instance.
*
* @param \GuzzleHttp\Client $client
* @param \Illuminate\Contracts\Cache\Repository $cache
* @param string|null $token
* @param string|null $url
*
* @return void
*/
public function __construct(Repository $cache, $token = null, $url = null)
public function __construct(Client $client, Repository $cache, $token = null, $url = null)
{
$this->client = $client;
$this->cache = $cache;
$this->token = $token;
$this->url = $url ?: static::URL;
Expand All @@ -85,11 +96,30 @@ public function latest()

event(new SystemCheckedForUpdatesEvent());

return json_decode((new Client())->get($this->url, [
'headers' => $headers,
])->getBody(), true);
try {
$resp = $this->client->get($this->url, [
'headers' => $headers,
'timeout' => 5,
'connect_timeout' => 5,
]);

if ($resp->getStatusCode() == 200) {
return json_decode($resp->getBody(), true);
}

return [];

} catch (\Exception $e) {
Log::warning('Unable to lookup latest Cachet release. ' . $e->getMessage());
return [];
}

});

if (!is_array($release) or count($release) == 0) {
return [];
}

return [
'tag_name' => $release['tag_name'],
'prelease' => $release['prerelease'],
Expand Down
4 changes: 3 additions & 1 deletion app/Providers/IntegrationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use CachetHQ\Cachet\Integrations\GitHub\Releases;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\ServiceProvider;
use GuzzleHttp\Client;

/**
* This is the integration service provider.
Expand Down Expand Up @@ -111,10 +112,11 @@ protected function registerSystem()
protected function registerReleases()
{
$this->app->singleton(ReleasesContract::class, function ($app) {
$client = $app->make(Client::class);
$cache = $app['cache.store'];
$token = $app['config']->get('services.github.token');

return new Releases($cache, $token);
return new Releases($client, $cache, $token);
});
}
}
59 changes: 59 additions & 0 deletions tests/Integrations/GitHub/ReleasesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace CachetHQ\Tests\Cachet\Integrations\GitHub\Releases;

use CachetHQ\Tests\Cachet\AbstractTestCase;
use Illuminate\Support\Facades\Cache;
use CachetHQ\Cachet\Integrations\GitHub\Releases;

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Exception\ConnectException;

class ReleasesTest extends AbstractTestCase
{
/**
* Creates Mock Guzzle client
*
* @param array $mock_responses list of fake response to return to HTTP requests
*/
private function createMockClient(array $mock_responses)
{
$mock = new MockHandler($mock_responses);

$handlerStack = HandlerStack::create($mock);
$client = new Client(['handler' => $handlerStack]);
$this->app->instance(Client::class, $client);
}

public function test_latest_parses_response_from_github()
{
$file = dirname(__FILE__) . '/../payloads/github_api.json';
$payload = file_get_contents($file);

$mock_responses = [
new Response(200, [], $payload),
];
$this->createMockClient($mock_responses);

Cache::tags('release.latest')->flush();
$latest = $this->app->make(Releases::class)->latest();
$this->assertEquals('v2.3.18', $latest['tag_name']);
}

public function test_latest_returns_empty_on_timeout()
{
$mock_responses = [
new ConnectException('cURL error 28: Connection timed out after 5000 milliseconds', new Request('GET', 'repos/cachethq/cachet/releases/latest')),
];
$this->createMockClient($mock_responses);

Cache::tags('release.latest')->flush();
$latest = $this->app->make(Releases::class)->latest();
$this->assertEquals(0, count($latest));
}
}
41 changes: 41 additions & 0 deletions tests/Integrations/payloads/github_api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"url": "https://api.github.com/repos/CachetHQ/Cachet/releases/17514684",
"assets_url": "https://api.github.com/repos/CachetHQ/Cachet/releases/17514684/assets",
"upload_url": "https://uploads.github.com/repos/CachetHQ/Cachet/releases/17514684/assets{?name,label}",
"html_url": "https://github.com/CachetHQ/Cachet/releases/tag/v2.3.18",
"id": 17514684,
"author": {
"login": "GrahamCampbell",
"id": 2829600,
"node_id": "MDQ6VXNlcjI4Mjk2MDA=",
"avatar_url": "https://avatars.githubusercontent.com/u/2829600?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/GrahamCampbell",
"html_url": "https://github.com/GrahamCampbell",
"followers_url": "https://api.github.com/users/GrahamCampbell/followers",
"following_url": "https://api.github.com/users/GrahamCampbell/following{/other_user}",
"gists_url": "https://api.github.com/users/GrahamCampbell/gists{/gist_id}",
"starred_url": "https://api.github.com/users/GrahamCampbell/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/GrahamCampbell/subscriptions",
"organizations_url": "https://api.github.com/users/GrahamCampbell/orgs",
"repos_url": "https://api.github.com/users/GrahamCampbell/repos",
"events_url": "https://api.github.com/users/GrahamCampbell/events{/privacy}",
"received_events_url": "https://api.github.com/users/GrahamCampbell/received_events",
"type": "User",
"site_admin": false
},
"node_id": "MDc6UmVsZWFzZTE3NTE0Njg0",
"tag_name": "v2.3.18",
"target_commitish": "2.4",
"name": "v2.3.18",
"draft": false,
"prerelease": false,
"created_at": "2019-05-14T12:27:19Z",
"published_at": "2019-05-22T12:43:04Z",
"assets": [

],
"tarball_url": "https://api.github.com/repos/CachetHQ/Cachet/tarball/v2.3.18",
"zipball_url": "https://api.github.com/repos/CachetHQ/Cachet/zipball/v2.3.18",
"body": ""
}

0 comments on commit 1478c0d

Please sign in to comment.