|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Honeybadger\HoneybadgerLaravel\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Support\Facades\Config; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
|
|
10
|
|
|
class HoneybadgerDeployCommand extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The name and signature of the console command. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $signature = 'honeybadger:deploy {--apiKey=} {--environment=} {--revision=} {--repository=} {--username=}'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \GuzzleHttp\Client |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $client; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The console command description. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $description = 'Send deployment to Honeybadger'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \GuzzleHttp\Client |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(Client $client) |
|
35
|
|
|
{ |
|
36
|
|
|
parent::__construct(); |
|
37
|
|
|
|
|
38
|
|
|
$this->client = $client; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Execute the console command. |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
public function handle() |
|
47
|
|
|
{ |
|
48
|
|
|
$params = $this->resolveParams(); |
|
49
|
|
|
|
|
50
|
|
|
$response = $this->client->post( |
|
51
|
|
|
'https://api.honeybadger.io/v1/deploys', |
|
52
|
|
|
[ |
|
53
|
|
|
'form_params' => $params, |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
|
|
$body = json_decode((string) $response->getBody(), true); |
|
58
|
|
|
|
|
59
|
|
|
if ($response->getStatusCode() !== Response::HTTP_CREATED || $body['status'] !== 'OK') { |
|
60
|
|
|
throw new \Exception(vsprintf('Sending the deployment to Honeybadger failed. Status code %s. Response %s.', [ |
|
61
|
|
|
$response->getStatusCode(), |
|
62
|
|
|
(string) $response->getBody(), |
|
63
|
|
|
])); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$this->info(sprintf('Deployment %s successfully sent', $params['deploy']['revision'])); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function resolveParams() : array |
|
70
|
|
|
{ |
|
71
|
|
|
$config = Config::get('honeybadger'); |
|
72
|
|
|
|
|
73
|
|
|
return [ |
|
74
|
|
|
'api_key' => $this->option('apiKey') ?? $config['api_key'], |
|
75
|
|
|
'deploy' => array_merge([ |
|
76
|
|
|
'revision' => $config['version'] ?? $this->gitHash(), |
|
77
|
|
|
'environment' => $config['environment_name'], |
|
78
|
|
|
], $this->resolveOptions()), |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function resolveOptions() : array |
|
83
|
|
|
{ |
|
84
|
|
|
return array_filter([ |
|
85
|
|
|
'environment' => $this->option('environment'), |
|
86
|
|
|
'revision' => $this->option('revision'), |
|
87
|
|
|
'repository' => $this->option('repository') ?? $this->gitRemote(), |
|
88
|
|
|
'local_username' => $this->option('username') ?? get_current_user(), |
|
89
|
|
|
]); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function gitHash() : string |
|
93
|
|
|
{ |
|
94
|
|
|
return trim(exec('git log --pretty="%h" -n1 HEAD')); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function gitRemote() : string |
|
98
|
|
|
{ |
|
99
|
|
|
return trim(exec('git config --get remote.origin.url')); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|