Skip to content

Commit

Permalink
throw exception when provider request fails
Browse files Browse the repository at this point in the history
  • Loading branch information
devajmeireles committed Sep 17, 2024
1 parent 746ffd9 commit cc62e71
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 39 deletions.
30 changes: 18 additions & 12 deletions src/Providers/BitBucketProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
24 changes: 16 additions & 8 deletions src/Providers/EnvoyerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
22 changes: 14 additions & 8 deletions src/Providers/GitHubProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions tests/Browser/EnvBar/BitBucketProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');
});
}
Expand All @@ -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');

Expand Down
8 changes: 5 additions & 3 deletions tests/Browser/EnvBar/EnvoyerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');
});
}

Expand All @@ -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');

Expand Down
10 changes: 6 additions & 4 deletions tests/Browser/EnvBar/GitHubProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');
});
}
Expand All @@ -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');

Expand Down

0 comments on commit cc62e71

Please sign in to comment.