-
Notifications
You must be signed in to change notification settings - Fork 66
Create beta deploy command #282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sbourouis
wants to merge
21
commits into
1.x
Choose a base branch
from
nw-1258-implement-deployment-tagging-system-for-nightwatch-graphs
base: 1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
48f0bc7
Create deploy command
sbourouis b389d11
Remove baseUrl parameter from DeployCommand
sbourouis 4c45d13
Improve deploy command and add tests
sbourouis c1330b5
Merge branch '1.x' into nw-1258-implement-deployment-tagging-system-f…
jessarcher a90b76c
Update command
jessarcher acc1505
Fix tests
jessarcher 2995648
Add `v`
jessarcher 2814ea0
Update console output
jessarcher d2682c2
Formatting
jessarcher af53485
Merge branch '1.x' into nw-1258-implement-deployment-tagging-system-f…
jessarcher c76cab3
Avoid failing a deployment with a non-zero exit code
jessarcher daf4d6d
Improve reporting of deploy errors
jessarcher 5b8a09e
Send timestamp as an ISO8601 Zulu string with microseconds
jessarcher 5d20394
Remove deploy exception
jessarcher e2beb2d
Change timestamp format
jessarcher 07c33f0
Merge branch '1.x' into nw-1258-implement-deployment-tagging-system-f…
jessarcher 54e9732
Linting
jessarcher 5825fd8
Merge branch '1.x' into nw-1258-implement-deployment-tagging-system-f…
jradtilbrook 9986e58
Revert "Linting"
jradtilbrook e9d53e0
Update deploy fields
jessarcher 81fff87
Merge branch '1.x' into nw-1258-implement-deployment-tagging-system-f…
jessarcher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| <?php | ||
|
|
||
| namespace Laravel\Nightwatch\Console; | ||
|
|
||
| use Carbon\CarbonImmutable; | ||
| use Illuminate\Console\Command; | ||
| use Illuminate\Http\Client\RequestException; | ||
| use Illuminate\Support\Facades\Http; | ||
| use Illuminate\Support\Str; | ||
| use Laravel\Nightwatch\NightwatchDeployException; | ||
| use SensitiveParameter; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Throwable; | ||
|
|
||
| use function config; | ||
| use function report; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[AsCommand(name: 'nightwatch:deploy', description: 'Notify Nightwatch of a deployment.')] | ||
| final class DeployCommand extends Command | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| protected $signature = 'nightwatch:deploy'; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected $description = 'Notify Nightwatch of a deployment.'; | ||
|
|
||
| /** | ||
| * @var bool | ||
| */ | ||
| protected $hidden = true; | ||
|
|
||
| public function __construct( | ||
| #[SensitiveParameter] private ?string $token, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| public function handle(): int | ||
| { | ||
| if (! $this->token) { | ||
| $this->components->error('Please configure the [NIGHTWATCH_TOKEN] environment variable.'); | ||
|
|
||
| report(new NightwatchDeployException('NIGHTWATCH_TOKEN environment variable is not configured.')); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| $version = config('nightwatch.deployment') ?? ''; | ||
|
|
||
| $baseUrl = ! empty($_SERVER['NIGHTWATCH_BASE_URL']) ? $_SERVER['NIGHTWATCH_BASE_URL'] : 'https://nightwatch.laravel.com'; | ||
|
|
||
| try { | ||
| Http::connectTimeout(5) | ||
| ->timeout(10) | ||
| ->acceptJson() | ||
| ->withToken($this->token) | ||
| ->post("{$baseUrl}/api/deployments", [ | ||
| 'v' => 1, | ||
| 'timestamp' => CarbonImmutable::now()->timestamp, | ||
| 'version' => $version, | ||
| ]) | ||
| ->throw(); | ||
|
|
||
| $this->components->info('Deployment sent to Nightwatch successfully.'); | ||
| } catch (RequestException $e) { | ||
| $message = Str::limit($e->response->json('message') ?? "[{$e->getCode()}] {$e->response->body()}", 1000, '[...]'); // @phpstan-ignore argument.type | ||
|
|
||
| report(new NightwatchDeployException($message, previous: $e)); | ||
|
|
||
| $this->components->error("Deployment could not be sent to Nightwatch: {$message}"); | ||
| } catch (Throwable $e) { | ||
| report(new NightwatchDeployException($e->getMessage(), previous: $e)); | ||
|
|
||
| $this->components->error("Deployment could not be sent to Nightwatch: {$e->getMessage()}"); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace Laravel\Nightwatch; | ||
|
|
||
| use RuntimeException; | ||
|
|
||
| class NightwatchDeployException extends RuntimeException | ||
| { | ||
| // | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Feature\Console; | ||
|
|
||
| use Illuminate\Contracts\Debug\ExceptionHandler; | ||
| use Illuminate\Http\Client\ConnectionException; | ||
| use Illuminate\Http\Client\Request; | ||
| use Illuminate\Http\Client\RequestException; | ||
| use Illuminate\Support\Facades\Http; | ||
| use Laravel\Nightwatch\Console\DeployCommand; | ||
| use Laravel\Nightwatch\NightwatchDeployException; | ||
| use Orchestra\Testbench\Attributes\WithEnv; | ||
| use Tests\TestCase; | ||
| use Throwable; | ||
|
|
||
| use function env; | ||
| use function json_encode; | ||
| use function now; | ||
|
|
||
| class DeployCommandTest extends TestCase | ||
| { | ||
| #[WithEnv('NIGHTWATCH_TOKEN', 'test-token')] | ||
| #[WithEnv('NIGHTWATCH_DEPLOY', 'v1.2.3')] | ||
| public function test_it_can_run_the_deploy_command(): void | ||
| { | ||
| $this->freezeTime(); | ||
| Http::fake([ | ||
| '*/api/deployments' => function (Request $request) { | ||
| $this->assertEquals(['Bearer '.env('NIGHTWATCH_TOKEN')], $request->header('Authorization')); | ||
| $this->assertEquals([ | ||
| 'v' => 1, | ||
| 'timestamp' => now()->getTimestamp(), | ||
| 'version' => 'v1.2.3', | ||
| ], $request->data()); | ||
|
|
||
| return Http::response('OK'); | ||
| }, | ||
| ]); | ||
|
|
||
| $this->artisan('nightwatch:deploy') | ||
| ->expectsOutputToContain('Deployment sent to Nightwatch successfully.') | ||
| ->assertExitCode(0); | ||
| } | ||
|
|
||
| #[WithEnv('NIGHTWATCH_TOKEN', 'test-token')] | ||
| public function test_it_can_run_the_deploy_command_without_a_version(): void | ||
| { | ||
| $this->freezeTime(); | ||
| Http::fake([ | ||
| '*/api/deployments' => function (Request $request) { | ||
| $this->assertEquals(['Bearer '.env('NIGHTWATCH_TOKEN')], $request->header('Authorization')); | ||
| $this->assertEquals([ | ||
| 'v' => 1, | ||
| 'timestamp' => now()->getTimestamp(), | ||
| 'version' => '', | ||
| ], $request->data()); | ||
|
|
||
| return Http::response('OK'); | ||
| }, | ||
| ]); | ||
|
|
||
| $this->artisan('nightwatch:deploy') | ||
| ->expectsOutputToContain('Deployment sent to Nightwatch successfully.') | ||
| ->assertExitCode(0); | ||
| } | ||
|
|
||
| public function test_it_fails_when_the_deploy_command_is_run_without_a_token(): void | ||
| { | ||
| $reported = null; | ||
| $this->app->make(ExceptionHandler::class)->reportable(function (Throwable $e) use (&$reported) { | ||
| $reported = $e; | ||
| }); | ||
| $this->app->singleton(DeployCommand::class, fn () => new DeployCommand(token: null)); | ||
|
|
||
| $this->artisan('nightwatch:deploy') | ||
| ->expectsOutputToContain('Please configure the [NIGHTWATCH_TOKEN] environment variable.') | ||
| ->assertExitCode(0); | ||
|
|
||
| $this->assertInstanceOf(NightwatchDeployException::class, $reported); | ||
| $this->assertSame('NIGHTWATCH_TOKEN environment variable is not configured.', $reported?->getMessage()); | ||
| } | ||
|
|
||
| #[WithEnv('NIGHTWATCH_TOKEN', 'test-token')] | ||
| public function test_it_handles_error_responses(): void | ||
| { | ||
| $reported = null; | ||
| $this->app->make(ExceptionHandler::class)->reportable(function (Throwable $e) use (&$reported) { | ||
| $reported = $e; | ||
| }); | ||
| Http::fake([ | ||
| '*/api/deployments' => Http::response(json_encode(['message' => 'Invalid environment token.']), 403), | ||
| ]); | ||
|
|
||
| $this->artisan('nightwatch:deploy') | ||
| ->expectsOutputToContain('Deployment could not be sent to Nightwatch: Invalid environment token.') | ||
| ->assertExitCode(0); | ||
|
|
||
| $this->assertInstanceOf(NightwatchDeployException::class, $reported); | ||
| $this->assertSame('Invalid environment token.', $reported?->getMessage()); | ||
| $this->assertInstanceOf(RequestException::class, $reported?->getPrevious()); | ||
| } | ||
|
|
||
| #[WithEnv('NIGHTWATCH_TOKEN', 'test-token')] | ||
| public function test_it_handles_http_errors(): void | ||
| { | ||
| $reported = null; | ||
| $this->app->make(ExceptionHandler::class)->reportable(function (Throwable $e) use (&$reported) { | ||
| $reported = $e; | ||
| }); | ||
| Http::fake([ | ||
| '*/api/deployments' => Http::response('Whoops!', 500), | ||
| ]); | ||
|
|
||
| $this->artisan('nightwatch:deploy') | ||
| ->expectsOutputToContain('Deployment could not be sent to Nightwatch: [500] Whoops!') | ||
| ->assertExitCode(0); | ||
|
|
||
| $this->assertInstanceOf(NightwatchDeployException::class, $reported); | ||
| $this->assertSame('[500] Whoops!', $reported?->getMessage()); | ||
| $this->assertInstanceOf(RequestException::class, $reported?->getPrevious()); | ||
| } | ||
|
|
||
| #[WithEnv('NIGHTWATCH_TOKEN', 'test-token')] | ||
| public function test_it_handles_connection_errors(): void | ||
| { | ||
| $reported = null; | ||
| $this->app->make(ExceptionHandler::class)->reportable(function (Throwable $e) use (&$reported) { | ||
| $reported = $e; | ||
| }); | ||
| Http::fake([ | ||
| '*/api/deployments' => fn () => throw new ConnectionException('Connection timeout.'), | ||
| ]); | ||
|
|
||
| $this->artisan('nightwatch:deploy') | ||
| ->expectsOutputToContain('Deployment could not be sent to Nightwatch: Connection timeout.') | ||
| ->assertExitCode(0); | ||
|
|
||
| $this->assertInstanceOf(NightwatchDeployException::class, $reported); | ||
| $this->assertSame('Connection timeout.', $reported?->getMessage()); | ||
| $this->assertInstanceOf(ConnectionException::class, $reported?->getPrevious()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.