Skip to content

Commit

Permalink
support for Envoyer
Browse files Browse the repository at this point in the history
  • Loading branch information
devajmeireles committed Sep 17, 2024
1 parent 0fed669 commit c5a46ac
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 5 deletions.
8 changes: 7 additions & 1 deletion config/envbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
|--------------------------------------------------------------------------
|
| Determines the provider to be used to fetch the latest version of the application.
| All keys inside "providers" is the supported values: github, bitbucket.
| All keys inside "providers" is the supported values: github, bitbucket, envoyer.
|
*/
'provider' => env('ENVBAR_PROVIDER'),
Expand All @@ -196,5 +196,11 @@
'repository' => env('ENVBAR_BITBUCKET_REPOSITORY'),
'cached_for' => env('ENVBAR_BITBUCKET_CACHED_FOR', 1),
],

'envoyer' => [
'token' => env('ENVBAR_ENVOYER_TOKEN', null),
'project_id' => env('ENVBAR_ENVOYER_PROJECT_ID', null),
'cached_for' => env('ENVBAR_BITBUCKET_CACHED_FOR', 1),
],
],
];
1 change: 1 addition & 0 deletions lang/en/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
return [
'github' => 'GitHub',
'bitbucket' => 'BitBucket',
'envoyer' => 'Envoyer',
];
2 changes: 2 additions & 0 deletions src/Compilers/EnvBarComponentCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use TallStackUi\EnvBar\Compilers\Colors\Colors;
use TallStackUi\EnvBar\Providers\BitBucketProvider;
use TallStackUi\EnvBar\Providers\EnvoyerProvider;
use TallStackUi\EnvBar\Providers\GitHubProvider;
use TallStackUi\EnvBar\Providers\GitProvider;

Expand Down Expand Up @@ -62,6 +63,7 @@ private function provider(): ?string
return match (config('envbar.provider')) {
'github' => app(GitHubProvider::class)->fetch(),
'bitbucket' => app(BitBucketProvider::class)->fetch(),
'envoyer' => app(EnvoyerProvider::class)->fetch(),
default => null,
};
}
Expand Down
39 changes: 39 additions & 0 deletions src/Providers/EnvoyerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace TallStackUi\EnvBar\Providers;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

class EnvoyerProvider extends AbstractProvider
{
protected array $keys = [
'token',
'project_id',
];

/**
* {@inheritDoc}
*/
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');
});
}

/**
* {@inheritDoc}
*/
public function provider(): string
{
return 'envoyer';
}
}
4 changes: 2 additions & 2 deletions tests/Browser/EnvBar/BitBucketProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function see_release(): void

$this->browse(function (Browser $browser): void {
$browser->visit('/')
->waitForText('Latest Release')
->assertSee('Latest Release')
->waitForText('Latest BitBucket Release')
->assertSee('Latest BitBucket Release')
->assertSee('v2.0.0');
});
}
Expand Down
58 changes: 58 additions & 0 deletions tests/Browser/EnvBar/EnvoyerProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Tests\Browser\EnvBar;

use Illuminate\Config\Repository;
use Illuminate\Support\Facades\Cache;
use Laravel\Dusk\Browser;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestWith;
use Tests\Browser\BrowserTestCase;

class EnvoyerProviderTest extends BrowserTestCase
{
#[Test]
public function see_release(): void
{
$this->beforeServingApplication(function ($app, Repository $config): void {
Cache::shouldReceive('remember')->andReturn('v2.0.0');

$config->set('envbar.provider', 'envoyer');

$config->set('envbar.providers.envoyer', [
'token' => 'tallstackui',
'project_id' => '12345',
]);
});

$this->browse(function (Browser $browser): void {
$browser->visit('/')
->waitForText('Latest Envoyer Release')
->assertSee('Latest Envoyer Release')
->assertSee('v2.0.0');
});
}

#[Test]
#[TestWith(['', '12345'])]
#[TestWith(['foo', ''])]
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');

$config->set('envbar.provider', 'envoyer');

$config->set('envbar.providers.envoyer', [
'token' => $token,
'project_id' => $project,
]);
});

$this->browse(function (Browser $browser) use ($token): void {
$expected = $token === '' ? 'token' : 'repository';

$browser->visit('/')->assertSee("The Envoyer provider requires the $expected key to be set.");
});
}
}
4 changes: 2 additions & 2 deletions tests/Browser/EnvBar/GitHubProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function see_release(): void

$this->browse(function (Browser $browser): void {
$browser->visit('/')
->waitForText('Latest Release')
->assertSee('Latest Release')
->waitForText('Latest GitHub Release')
->assertSee('Latest GitHub Release')
->assertSee('v1.0.0');
});
}
Expand Down

0 comments on commit c5a46ac

Please sign in to comment.