diff --git a/app/Bus/Commands/Incident/CreateIncidentCommand.php b/app/Bus/Commands/Incident/CreateIncidentCommand.php index ae8f5f97..5ed21694 100644 --- a/app/Bus/Commands/Incident/CreateIncidentCommand.php +++ b/app/Bus/Commands/Incident/CreateIncidentCommand.php @@ -96,6 +96,13 @@ final class CreateIncidentCommand */ public $template_vars; + /** + * Meta key/value pairs. + * + * @var array + */ + public $meta = []; + /** * The validation rules. * @@ -112,6 +119,7 @@ final class CreateIncidentCommand 'stickied' => 'required|bool', 'occurred_at' => 'nullable|string', 'template' => 'nullable|string', + 'meta' => 'required|array', ]; /** @@ -128,10 +136,11 @@ final class CreateIncidentCommand * @param string|null $occurred_at * @param string|null $template * @param array $template_vars + * @param array $meta * * @return void */ - public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = []) + public function __construct($name, $status, $message, $visible, $component_id, $component_status, $notify, $stickied, $occurred_at, $template, array $template_vars = [], $meta = []) { $this->name = $name; $this->status = $status; @@ -144,5 +153,6 @@ final class CreateIncidentCommand $this->occurred_at = $occurred_at; $this->template = $template; $this->template_vars = $template_vars; + $this->meta = $meta; } } diff --git a/app/Bus/Handlers/Commands/Incident/CreateIncidentCommandHandler.php b/app/Bus/Handlers/Commands/Incident/CreateIncidentCommandHandler.php index d8b36af2..4504d905 100644 --- a/app/Bus/Handlers/Commands/Incident/CreateIncidentCommandHandler.php +++ b/app/Bus/Handlers/Commands/Incident/CreateIncidentCommandHandler.php @@ -18,6 +18,7 @@ use CachetHQ\Cachet\Bus\Exceptions\Incident\InvalidIncidentTimestampException; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Models\IncidentTemplate; +use CachetHQ\Cachet\Models\Meta; use CachetHQ\Cachet\Services\Dates\DateFactory; use Carbon\Carbon; use Illuminate\Contracts\Auth\Guard; @@ -100,6 +101,18 @@ class CreateIncidentCommandHandler // Create the incident $incident = Incident::create($data); + // Store any meta? + if ($meta = $command->meta) { + foreach ($meta as $key => $value) { + Meta::create([ + 'key' => $key, + 'value' => $value, + 'meta_type' => 'incidents', + 'meta_id' => $incident->id, + ]); + } + } + // Update the component. if ($component = Component::find($command->component_id)) { dispatch(new UpdateComponentCommand( diff --git a/app/Http/Controllers/Api/IncidentController.php b/app/Http/Controllers/Api/IncidentController.php index 98b66cc2..ac5cd82a 100644 --- a/app/Http/Controllers/Api/IncidentController.php +++ b/app/Http/Controllers/Api/IncidentController.php @@ -78,7 +78,8 @@ class IncidentController extends AbstractApiController Binput::get('stickied', false), Binput::get('occurred_at'), Binput::get('template'), - Binput::get('vars', []) + Binput::get('vars', []), + Binput::get('meta', []) )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/tests/Api/IncidentTest.php b/tests/Api/IncidentTest.php index dea3d3d7..1ea01c8e 100644 --- a/tests/Api/IncidentTest.php +++ b/tests/Api/IncidentTest.php @@ -154,4 +154,24 @@ class IncidentTest extends AbstractApiTestCase $this->delete('/api/v1/incidents/1'); $this->assertResponseStatus(204); } + + public function testCreateIncidentWithMeta() + { + $this->beUser(); + + $this->post('/api/v1/incidents', [ + 'name' => 'Foo', + 'message' => 'Lorem ipsum dolor sit amet', + 'status' => 1, + 'meta' => [ + 'id' => 123456789, + ], + ]); + $this->seeJson([ + 'meta' => [ + 'id' => 123456789, + ], + ]); + $this->assertResponseOk(); + } } diff --git a/tests/Bus/Commands/Incident/CreateIncidentCommandTest.php b/tests/Bus/Commands/Incident/CreateIncidentCommandTest.php index e52796c3..ee84d386 100644 --- a/tests/Bus/Commands/Incident/CreateIncidentCommandTest.php +++ b/tests/Bus/Commands/Incident/CreateIncidentCommandTest.php @@ -40,6 +40,7 @@ class CreateIncidentCommandTest extends AbstractTestCase 'occurred_at' => null, 'template' => null, 'template_vars' => [], + 'meta' => [], ]; $object = new CreateIncidentCommand( @@ -53,7 +54,8 @@ class CreateIncidentCommandTest extends AbstractTestCase $params['stickied'], $params['occurred_at'], $params['template'], - $params['template_vars'] + $params['template_vars'], + $params['meta'] ); return compact('params', 'object');