diff --git a/app/Config/Reporting/ConfigReport.php b/app/Config/Reporting/ConfigReport.php new file mode 100644 --- /dev/null +++ b/app/Config/Reporting/ConfigReport.php @@ -0,0 +1,79 @@ +<?php + +namespace Nasqueron\Notifications\Config\Reporting; + +use Nasqueron\Notifications\Config\Features; + +use Config; +use Services; + +class ConfigReport { + + /// + /// Public properties + /// + + /** + * @var string[] + */ + public $gates; + + /** + * @var FeatureReportEntry[] + */ + public $features; + + /** + * @var ServiceReportEntry[] + */ + public $services; + + /// + /// Public constructor + /// + + public function __construct () { + $this->gates = $this->queryGates(); + $this->features = $this->queryFeatures(); + $this->services = $this->queryServices(); + } + + /// + /// Report builder + /// + + /** + * @return string[] + */ + protected function queryGates () : array { + return Config::get('gate.controllers'); + } + + /** + * @return FeatureReportEntry[] + */ + protected function queryFeatures () : array { + $features = []; + + foreach (Features::getAll() as $feature => $enabled) { + $features[] = new FeatureReportEntry($feature, $enabled); + } + + return $features; + } + + /** + * @return ServiceReportEntry[] + */ + protected function queryServices () : array { + $services = []; + + foreach (Services::get() as $service) { + $services[] = new ServiceReportEntry($service); + } + + return $services; + } + + +} diff --git a/app/Config/Reporting/FeatureReportEntry.php b/app/Config/Reporting/FeatureReportEntry.php new file mode 100644 --- /dev/null +++ b/app/Config/Reporting/FeatureReportEntry.php @@ -0,0 +1,36 @@ +<?php + +namespace Nasqueron\Notifications\Config\Reporting; + +class FeatureReportEntry { + + /// + /// Public properties + /// + + /** + * @var string + */ + public $name; + + /** + * @var bool + */ + public $enabled; + + /// + /// Constructor + /// + + /** + * Initializes a new instance of the FeatureReportEntry class. + * + * @var name The feature name + * @var bool If the feature enabled, true. Otherwise, false. + */ + public function __construct (string $name, bool $enabled) { + $this->name = $name; + $this->enabled = $enabled; + } + +} diff --git a/app/Config/Reporting/ServiceReportEntry.php b/app/Config/Reporting/ServiceReportEntry.php new file mode 100644 --- /dev/null +++ b/app/Config/Reporting/ServiceReportEntry.php @@ -0,0 +1,102 @@ +<?php + +namespace Nasqueron\Notifications\Config\Reporting; + +use Nasqueron\Notifications\Config\Services\Service; + +use ProjectsMap; + +class ServiceReportEntry { + + /// + /// Private members + /// + + /** + * @var Service + */ + private $service; + + /// + /// Public properties + /// + + /** + * @var string + */ + public $gate; + + /** + * @var string + */ + public $door; + + /** + * @var string + */ + public $instance; + + /** + * @var string + */ + public $status = ""; + + /// + /// Constructor + /// + + /** + * Initializes a new instance of the ServiceReportEntry class. + * + * @param \Nasqueron\Notifications\Config\Services\Service $service The service + */ + public function __construct (Service $service) { + $this->service = $service; + $this->query(); + } + + /// + /// Report builder + /// + + /** + * Queries the service to fill public properties. + */ + protected function query () : void { + // Direct properties + $this->gate = $this->service->gate; + $this->door = $this->service->door; + $this->instance = (string)$this->service->instance; + + // Properties to query with business logic + $this->status = $this->getServiceStatus(); + } + + /** + * @return string An issue to fix, or an empty string if all looks good. + */ + protected function getServiceStatus () : string { + if ($this->isPhabricatorServiceWithNotCachedProjectsMap()) { + return "Projects map not cached."; + } + + return ""; + } + + /** + * Determines if the service matches the following issue to report: + * - service is Phabricator + * - instance doesn't have the projects' name/PHID map in cache + * + * @return bool + */ + protected function isPhabricatorServiceWithNotCachedProjectsMap () : bool { + if ($this->service->gate !== 'Phabricator') { + return false; + } + + $map = ProjectsMap::fetch($this->service->door); + return !$map->isCached(); + } + +} diff --git a/app/Http/routes.php b/app/Http/routes.php --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -1,6 +1,7 @@ <?php use Nasqueron\Notifications\Config\Features; +use Nasqueron\Notifications\Config\Reporting\ConfigReport; /* |-------------------------------------------------------------------------- @@ -22,6 +23,14 @@ return "ALIVE"; }); +// Allows to external tool to check the current configuration. +if (Features::isEnabled('GetConfig')) { + Route::get('/config', function() { + $report = new ConfigReport(); + return Response::json($report); + }); +} + // Gate controllers if (Features::isEnabled('Gate')) { foreach (Config::get('gate.controllers') as $controller) { diff --git a/config/app.php b/config/app.php --- a/config/app.php +++ b/config/app.php @@ -125,6 +125,9 @@ // Enable the API entry point at the /gate URL 'Gate' => true, + // Enable the configuration report entry point at the /config URL + 'GetConfig' => true, + // Send a response to inform the caller of the different actions done. // If disabled, send an empty 200 response instead. 'ActionsReport' => true, diff --git a/tests/data/config.json b/tests/data/config.json new file mode 100644 --- /dev/null +++ b/tests/data/config.json @@ -0,0 +1,32 @@ +{ + "gates": [ + "DockerHub", + "GitHub", + "Jenkins", + "Phabricator" + ], + "features": [ + { + "name": "Gate", + "enabled": true + }, + { + "name": "ActionsReport" + "enabled": true + } + ], + "services": [ + { + "gate": "GitHub", + "door": "Nasqueron", + "instance": "", + "status": "" + }, + { + "gate": "Phabricator", + "door": "Nasqueron", + "instance": "https://devcentral.nasqueron.org", + "status": "" + } + ] +}