Skip to content

Commit

Permalink
Fixed some issues when doing production testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlive committed Jun 27, 2019
1 parent a3b4c21 commit 2a108d4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
26 changes: 12 additions & 14 deletions src/Commands/HoneybadgerDeployCommand.php
Expand Up @@ -5,6 +5,7 @@
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Symfony\Component\HttpFoundation\Response;

class HoneybadgerDeployCommand extends Command
{
Expand Down Expand Up @@ -44,46 +45,43 @@ public function __construct(Client $client)
*/
public function handle()
{
$params = $this->resolveParams();

$response = $this->client->post(
'https://api.honeybadger.io/v1/deploys',
[
'form_params' => $this->resolveParams(),
'form_params' => $params,
]
);

$body = json_decode((string) $response->getBody(), true);

if ($response->getStatusCode() !== 200 || $body['status'] !== 'OK') {
if ($response->getStatusCode() !== Response::HTTP_CREATED || $body['status'] !== 'OK') {
throw new \Exception(vsprintf('Sending the deployment to Honeybadger failed. Status code %s. Response %s.', [
$response->getStatusCode(),
(string) $response->getBody(),
]));
}
}

private function resolveParams() : array
{
return array_merge(
$this->resolveConfigValues(),
$this->resolveOptions()
);
$this->info(sprintf('Deployment %s successfully sent', $params['deploy']['revision']));
}

private function resolveConfigValues() : array
private function resolveParams() : array
{
$config = Config::get('honeybadger');

return [
'api_key' => $config['api_key'],
'revision' => $config['version'] ?? $this->gitHash(),
'environment' => $config['environment_name'],
'api_key' => $this->option('apiKey') ?? $config['api_key'],
'deploy' => array_merge([
'revision' => $config['version'] ?? $this->gitHash(),
'environment' => $config['environment_name'],
], $this->resolveOptions()),
];
}

private function resolveOptions() : array
{
return array_filter([
'api_key' => $this->option('apiKey'),
'environment' => $this->option('environment'),
'revision' => $this->option('revision'),
'repository' => $this->option('repository') ?? $this->gitRemote(),
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/UserContext.php
Expand Up @@ -32,7 +32,7 @@ public function handle($request, Closure $next)
if (app()->bound('honeybadger') && $request->user()) {
$this->honeybadger->context(
'user_id',
$request->user()->getAuthIdentifier()
$request->user()->getAuthIdentifier()
);
}

Expand Down
24 changes: 14 additions & 10 deletions tests/Commands/HoneybadgetDeployCommandTest.php
Expand Up @@ -26,7 +26,7 @@ public function post($url, $options = [])
$this->url = $url;
$this->options = $options;

return $this->response ?? new Response(200, [], json_encode(['status' => 'OK']));
return $this->response ?? new Response(201, [], json_encode(['status' => 'OK']));
}
};

Expand All @@ -52,10 +52,12 @@ public function default_params_resolve()
$this->assertEquals([
'form_params' => [
'api_key' => 'secret1234',
'environment' => 'production',
'revision' => '1.1.0',
'repository' => trim(exec('git config --get remote.origin.url')),
'local_username' => get_current_user(),
'deploy' => [
'environment' => 'production',
'revision' => '1.1.0',
'repository' => trim(exec('git config --get remote.origin.url')),
'local_username' => get_current_user(),
],
],
], $this->client->options);
}
Expand All @@ -71,7 +73,7 @@ public function revision_falls_back_to_git_hash()
$this->artisan('honeybadger:deploy');

$this->assertEquals(
$this->client->options['form_params']['revision'],
$this->client->options['form_params']['deploy']['revision'],
trim(exec('git log --pretty="%h" -n1 HEAD'))
);
}
Expand All @@ -96,10 +98,12 @@ public function params_from_options_override_defaults()
$this->assertEquals([
'form_params' => [
'api_key' => 'supersecret',
'environment' => 'staging',
'revision' => '2.0',
'repository' => 'https://github.com/honeybadger-io/honeybadger-laravel',
'local_username' => 'systemuser',
'deploy' => [
'environment' => 'staging',
'revision' => '2.0',
'repository' => 'https://github.com/honeybadger-io/honeybadger-laravel',
'local_username' => 'systemuser',
],
],
], $this->client->options);
}
Expand Down

0 comments on commit 2a108d4

Please sign in to comment.