From cc62e719a604a07e76244bf87a7c6bbe7970e1d1 Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Mon, 16 Sep 2024 22:06:06 -0300 Subject: [PATCH] throw exception when provider request fails --- src/Providers/BitBucketProvider.php | 30 +++++++++++-------- src/Providers/EnvoyerProvider.php | 24 ++++++++++----- src/Providers/GitHubProvider.php | 22 +++++++++----- .../Browser/EnvBar/BitBucketProviderTest.php | 10 ++++--- tests/Browser/EnvBar/EnvoyerProviderTest.php | 8 +++-- tests/Browser/EnvBar/GitHubProviderTest.php | 10 ++++--- 6 files changed, 65 insertions(+), 39 deletions(-) diff --git a/src/Providers/BitBucketProvider.php b/src/Providers/BitBucketProvider.php index 82d5359..6be51d6 100644 --- a/src/Providers/BitBucketProvider.php +++ b/src/Providers/BitBucketProvider.php @@ -19,18 +19,24 @@ public function fetch(): ?string { $this->validate(); - return Cache::remember($this->cacheKey(), now()->addDays($this->configuration->get('cached_for', 1)), function (): ?string { - $response = Http::withToken($this->configuration->get('token')) - ->get('https://api.bitbucket.org/2.0/repositories/'.$this->configuration->get('repository').'/refs/tags', [ - 'sort' => 'target.date', - ]); - - if ($response->ok()) { - return rescue(fn () => $response->collect()->get('values')[0]['name'], report: false); - } - - return null; - }); + if (Cache::has($this->cacheKey())) { + return Cache::get($this->cacheKey()); + } + + $response = Http::withToken($this->configuration->get('token')) + ->get('https://api.bitbucket.org/2.0/repositories/'.$this->configuration->get('repository').'/refs/tags', [ + 'sort' => 'target.date', + ]); + + if ($response->ok()) { + Cache::put($this->cacheKey(), $tag = $response->json('values.0.name'), now()->addDays($this->configuration->get('cached_for', 1))); + + return $tag; + } + + $response->throw(); + + return null; } /** diff --git a/src/Providers/EnvoyerProvider.php b/src/Providers/EnvoyerProvider.php index c95c135..b96b745 100644 --- a/src/Providers/EnvoyerProvider.php +++ b/src/Providers/EnvoyerProvider.php @@ -19,14 +19,22 @@ public function fetch(): ?string { $this->validate(); - return Cache::remember($this->cacheKey(), now()->addDays($this->configuration->get('cached_for', 1)), function (): ?string { - $response = Http::withToken($this->configuration->get('token')) - ->get('https://envoyer.io/api/projects/'.$this->configuration->get('project_id')); - - return $response->failed() - ? null - : $response->json('project.last_deployed_branch'); - }); + if (Cache::has($this->cacheKey())) { + return Cache::get($this->cacheKey()); + } + + $response = Http::withToken($this->configuration->get('token')) + ->get('https://envoyer.io/api/projects/'.$this->configuration->get('project_id')); + + if ($response->ok()) { + Cache::put($this->cacheKey(), $tag = $response->json('project.last_deployed_branch'), now()->addDays($this->configuration->get('cached_for', 1))); + + return $tag; + } + + $response->throw(); + + return null; } /** diff --git a/src/Providers/GitHubProvider.php b/src/Providers/GitHubProvider.php index 3571f02..fbff79f 100644 --- a/src/Providers/GitHubProvider.php +++ b/src/Providers/GitHubProvider.php @@ -19,16 +19,22 @@ public function fetch(): ?string { $this->validate(); - return Cache::remember($this->cacheKey(), now()->addDays($this->configuration->get('cached_for', 1)), function (): ?string { - $response = Http::withToken($this->configuration->get('token')) - ->get('https://api.github.com/repos/'.$this->configuration->get('repository').'/tags'); + if (Cache::has($this->cacheKey())) { + return Cache::get($this->cacheKey()); + } - if ($response->ok()) { - return $response->collect()->first()['name']; - } + $response = Http::withToken($this->configuration->get('token')) + ->get('https://api.github.com/repos/'.$this->configuration->get('repository').'/tags'); - return null; - }); + if ($response->ok()) { + Cache::put($this->cacheKey(), $tag = $response->json('0.name'), now()->addDays($this->configuration->get('cached_for', 1))); + + return $tag; + } + + $response->throw(); + + return null; } /** diff --git a/tests/Browser/EnvBar/BitBucketProviderTest.php b/tests/Browser/EnvBar/BitBucketProviderTest.php index bb508a0..38993b2 100644 --- a/tests/Browser/EnvBar/BitBucketProviderTest.php +++ b/tests/Browser/EnvBar/BitBucketProviderTest.php @@ -15,7 +15,8 @@ class BitBucketProviderTest extends BrowserTestCase public function see_release(): void { $this->beforeServingApplication(function ($app, Repository $config): void { - Cache::shouldReceive('remember')->andReturn('v2.0.0'); + Cache::shouldReceive('has')->andReturnTrue(); + Cache::shouldReceive('get')->andReturn('v2.0.0'); $config->set('envbar.provider', 'bitbucket'); @@ -27,8 +28,8 @@ public function see_release(): void $this->browse(function (Browser $browser): void { $browser->visit('/') - ->waitForText('Latest BitBucket Release') - ->assertSee('Latest BitBucket Release') + ->waitForText('Latest Bitbucket Release') + ->assertSee('Latest Bitbucket Release') ->assertSee('v2.0.0'); }); } @@ -39,7 +40,8 @@ public function see_release(): void public function throw_exception_when_parameters_is_empty(string $token, string $repository): void { $this->beforeServingApplication(function ($app, Repository $config) use ($token, $repository): void { - Cache::shouldReceive('remember')->andReturn('v2.0.0'); + Cache::shouldReceive('has')->andReturnTrue(); + Cache::shouldReceive('get')->andReturn('v2.0.0'); $config->set('envbar.provider', 'bitbucket'); diff --git a/tests/Browser/EnvBar/EnvoyerProviderTest.php b/tests/Browser/EnvBar/EnvoyerProviderTest.php index 8c53dc3..df02990 100644 --- a/tests/Browser/EnvBar/EnvoyerProviderTest.php +++ b/tests/Browser/EnvBar/EnvoyerProviderTest.php @@ -15,7 +15,8 @@ class EnvoyerProviderTest extends BrowserTestCase public function see_release(): void { $this->beforeServingApplication(function ($app, Repository $config): void { - Cache::shouldReceive('remember')->andReturn('v2.0.0'); + Cache::shouldReceive('has')->andReturnTrue(); + Cache::shouldReceive('get')->andReturn('v3.0.0'); $config->set('envbar.provider', 'envoyer'); @@ -29,7 +30,7 @@ public function see_release(): void $browser->visit('/') ->waitForText('Latest Envoyer Release') ->assertSee('Latest Envoyer Release') - ->assertSee('v2.0.0'); + ->assertSee('v3.0.0'); }); } @@ -39,7 +40,8 @@ public function see_release(): void public function throw_exception_when_parameters_is_empty(string $token, string $project): void { $this->beforeServingApplication(function ($app, Repository $config) use ($token, $project): void { - Cache::shouldReceive('remember')->andReturn('v2.0.0'); + Cache::shouldReceive('has')->andReturnTrue(); + Cache::shouldReceive('get')->andReturn('v3.0.0'); $config->set('envbar.provider', 'envoyer'); diff --git a/tests/Browser/EnvBar/GitHubProviderTest.php b/tests/Browser/EnvBar/GitHubProviderTest.php index 7b2c83c..5fced27 100644 --- a/tests/Browser/EnvBar/GitHubProviderTest.php +++ b/tests/Browser/EnvBar/GitHubProviderTest.php @@ -15,7 +15,8 @@ class GitHubProviderTest extends BrowserTestCase public function see_release(): void { $this->beforeServingApplication(function ($app, Repository $config): void { - Cache::shouldReceive('remember')->andReturn('v1.0.0'); + Cache::shouldReceive('has')->andReturnTrue(); + Cache::shouldReceive('get')->andReturn('v1.0.0'); $config->set('envbar.provider', 'github'); @@ -27,8 +28,8 @@ public function see_release(): void $this->browse(function (Browser $browser): void { $browser->visit('/') - ->waitForText('Latest GitHub Release') - ->assertSee('Latest GitHub Release') + ->waitForText('Latest Gitbub Release') + ->assertSee('Latest Gitbub Release') ->assertSee('v1.0.0'); }); } @@ -39,7 +40,8 @@ public function see_release(): void public function throw_exception_when_parameters_is_empty(string $token, string $repository): void { $this->beforeServingApplication(function ($app, Repository $config) use ($token, $repository): void { - Cache::shouldReceive('remember')->andReturn('v1.0.0'); + Cache::shouldReceive('has')->andReturnTrue(); + Cache::shouldReceive('get')->andReturn('v1.0.0'); $config->set('envbar.provider', 'github');