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