Skip to content

Commit 3351751

Browse files
authored
Merge pull request #111 from honeybadger-io/checkins-sync
feat: checkins-sync command to synchronize checkins from config file
2 parents 0866f6c + 025f706 commit 3351751

File tree

6 files changed

+126
-2
lines changed

6 files changed

+126
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
88

99
## [3.16.0] - 2023-10-06
1010
### Added
11-
- Synchronize checkins `honeybadger:checkins:sync`
11+
- Synchronize checkins with `honeybadger:checkins:sync`
1212

1313
## [3.15.2] - 2023-08-06
1414
### Changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"require": {
2424
"php": "^7.2|^8.0",
25-
"honeybadger-io/honeybadger-php": "^2.14.1",
25+
"honeybadger-io/honeybadger-php": "^2.15.0",
2626
"sixlive/dotenv-editor": "^1.1|^2.0",
2727
"illuminate/console": "^7.0|^8.0|^9.0|^10.0",
2828
"illuminate/support": "^7.0|^8.0|^9.0|^10.0",

config/honeybadger.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
*/
99
'api_key' => env('HONEYBADGER_API_KEY'),
1010

11+
/**
12+
* Your personal authentication token. Get this from authentication tab in your User Settings page.
13+
*/
14+
'personal_auth_token' => env('HONEYBADGER_PERSONAL_AUTH_TOKEN'),
15+
1116
/**
1217
* The application environment.
1318
*/
@@ -128,4 +133,12 @@
128133
Breadcrumbs\ViewRendered::class,
129134
],
130135
],
136+
137+
/**
138+
* Define your checkins here and synchronize them to Honeybadger with the
139+
* honeybadger:checkins:sync artisan command.
140+
* The recommended approach is to run this command as part of your deploy process.
141+
* Learn more about checkins at https://docs.honeybadger.io/api/reporting-check-ins/
142+
*/
143+
'checkins' => [],
131144
];
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Honeybadger\HoneybadgerLaravel\Commands;
4+
5+
use Honeybadger\Contracts\SyncCheckins;
6+
use Illuminate\Console\Command;
7+
8+
class HoneybadgerCheckinsSyncCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'honeybadger:checkins:sync';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Synchronize checkins to Honeybadger';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return mixed
28+
*/
29+
public function handle(SyncCheckins $checkinsManager)
30+
{
31+
$localCheckins = config('honeybadger.checkins', []);
32+
$result = $checkinsManager->sync($localCheckins);
33+
$this->info('Checkins were synchronized with Honeybadger.');
34+
$this->table(['Id', 'Name', 'Schedule Type', 'Cron Schedule', 'Cron Timezone', 'Grace Period', 'Status'], array_map(function ($checkin) {
35+
return [
36+
$checkin->id,
37+
$checkin->name,
38+
$checkin->scheduleType,
39+
$checkin->cronSchedule,
40+
$checkin->cronTimezone,
41+
$checkin->gracePeriod,
42+
$checkin->isDeleted() ? '❌ Removed' : '✅ Synchronized',
43+
];
44+
}, $result));
45+
}
46+
}

src/HoneybadgerServiceProvider.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace Honeybadger\HoneybadgerLaravel;
44

55
use GuzzleHttp\Client;
6+
use Honeybadger\CheckinsManager;
7+
use Honeybadger\Contracts\SyncCheckins;
8+
use Honeybadger\HoneybadgerLaravel\Commands\HoneybadgerCheckinsSyncCommand;
69
use Honeybadger\LogHandler;
710
use Honeybadger\Honeybadger;
811
use Honeybadger\Contracts\Reporter;
@@ -14,6 +17,7 @@
1417
use Honeybadger\HoneybadgerLaravel\Contracts\Installer as InstallerContract;
1518
use Illuminate\Console\Scheduling\Event;
1619
use Illuminate\Support\Facades\Blade;
20+
use Illuminate\Support\Facades\Log;
1721
use Illuminate\Support\ServiceProvider;
1822

1923
class HoneybadgerServiceProvider extends ServiceProvider
@@ -46,6 +50,7 @@ public function register()
4650
$this->mergeConfigFrom(__DIR__.'/../config/honeybadger.php', 'honeybadger');
4751

4852
$this->registerReporters();
53+
$this->registerCheckinsSync();
4954

5055
$this->app->bind(LogHandler::class, function ($app) {
5156
return new LogHandler($app[Reporter::class]);
@@ -72,6 +77,7 @@ private function registerCommands()
7277
$this->commands([
7378
'command.honeybadger:test',
7479
'command.honeybadger:checkin',
80+
'command.honeybadger:checkins:sync',
7581
'command.honeybadger:install',
7682
'command.honeybadger:deploy',
7783
]);
@@ -92,6 +98,11 @@ private function bindCommands()
9298
HoneybadgerCheckinCommand::class
9399
);
94100

101+
$this->app->bind(
102+
'command.honeybadger:checkins:sync',
103+
HoneybadgerCheckinsSyncCommand::class
104+
);
105+
95106
$this->app->bind(
96107
'command.honeybadger:install',
97108
HoneybadgerInstallCommand::class
@@ -174,6 +185,13 @@ protected function setUpAutomaticBreadcrumbs()
174185
}
175186
}
176187

188+
protected function registerCheckinsSync(): void
189+
{
190+
$this->app->singleton(SyncCheckins::class, function ($app) {
191+
return new CheckinsManager($app['config']['honeybadger']);
192+
});
193+
}
194+
177195
protected function registerReporters(): void
178196
{
179197
$this->app->singleton(Reporter::class, function ($app) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Honeybadger\Tests\Commands;
4+
5+
use Honeybadger\Contracts\SyncCheckins;
6+
use Honeybadger\Tests\TestCase;
7+
use Illuminate\Support\Facades\Config;
8+
9+
class HoneybadgerCheckinsSyncCommandTest extends TestCase
10+
{
11+
const CHECKINS = [
12+
[
13+
'project_id' => 'p1234',
14+
'name' => 'simple checkin test',
15+
'scheduleType' => 'simple',
16+
'report_period' => '1 day',
17+
],
18+
[
19+
'project_id' => 'p1234',
20+
'name' => 'cron checkin test',
21+
'scheduleType' => 'cron',
22+
'cron_schedule' => '0 * * * *',
23+
],
24+
];
25+
26+
protected function setUp(): void
27+
{
28+
parent::setUp();
29+
Config::set('honeybadger', [
30+
'personal_auth_token' => '1234567890',
31+
'checkins' => self::CHECKINS,
32+
]);
33+
}
34+
35+
/** @test */
36+
public function it_reads_checkins_from_config()
37+
{
38+
$mock = $this->createMock(SyncCheckins::class);
39+
$mock->expects($this->once())
40+
->method('sync')
41+
->with(self::CHECKINS);
42+
43+
$this->app->instance(SyncCheckins::class, $mock);
44+
45+
$this->artisan('honeybadger:checkins:sync');
46+
}
47+
}

0 commit comments

Comments
 (0)