From c5a46acf72ffd333a5a6b4080822f104b5c9d7f9 Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Mon, 16 Sep 2024 21:38:28 -0300 Subject: [PATCH] support for Envoyer --- config/envbar.php | 8 ++- lang/en/providers.php | 1 + src/Compilers/EnvBarComponentCompiler.php | 2 + src/Providers/EnvoyerProvider.php | 39 +++++++++++++ .../Browser/EnvBar/BitBucketProviderTest.php | 4 +- tests/Browser/EnvBar/EnvoyerProviderTest.php | 58 +++++++++++++++++++ tests/Browser/EnvBar/GitHubProviderTest.php | 4 +- 7 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 src/Providers/EnvoyerProvider.php create mode 100644 tests/Browser/EnvBar/EnvoyerProviderTest.php diff --git a/config/envbar.php b/config/envbar.php index 1061c28..49685a9 100644 --- a/config/envbar.php +++ b/config/envbar.php @@ -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'), @@ -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), + ], ], ]; diff --git a/lang/en/providers.php b/lang/en/providers.php index 661fcb7..d875a96 100644 --- a/lang/en/providers.php +++ b/lang/en/providers.php @@ -3,4 +3,5 @@ return [ 'github' => 'GitHub', 'bitbucket' => 'BitBucket', + 'envoyer' => 'Envoyer', ]; diff --git a/src/Compilers/EnvBarComponentCompiler.php b/src/Compilers/EnvBarComponentCompiler.php index e5d26f1..2d6c830 100644 --- a/src/Compilers/EnvBarComponentCompiler.php +++ b/src/Compilers/EnvBarComponentCompiler.php @@ -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; @@ -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, }; } diff --git a/src/Providers/EnvoyerProvider.php b/src/Providers/EnvoyerProvider.php new file mode 100644 index 0000000..c95c135 --- /dev/null +++ b/src/Providers/EnvoyerProvider.php @@ -0,0 +1,39 @@ +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'; + } +} diff --git a/tests/Browser/EnvBar/BitBucketProviderTest.php b/tests/Browser/EnvBar/BitBucketProviderTest.php index 8fd3c53..bb508a0 100644 --- a/tests/Browser/EnvBar/BitBucketProviderTest.php +++ b/tests/Browser/EnvBar/BitBucketProviderTest.php @@ -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'); }); } diff --git a/tests/Browser/EnvBar/EnvoyerProviderTest.php b/tests/Browser/EnvBar/EnvoyerProviderTest.php new file mode 100644 index 0000000..8c53dc3 --- /dev/null +++ b/tests/Browser/EnvBar/EnvoyerProviderTest.php @@ -0,0 +1,58 @@ +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."); + }); + } +} diff --git a/tests/Browser/EnvBar/GitHubProviderTest.php b/tests/Browser/EnvBar/GitHubProviderTest.php index 393875c..7b2c83c 100644 --- a/tests/Browser/EnvBar/GitHubProviderTest.php +++ b/tests/Browser/EnvBar/GitHubProviderTest.php @@ -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'); }); }