Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3769894
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
15 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/app/Console/Commands/PhabricatorGetProjectsMap.php b/app/Console/Commands/PhabricatorGetProjectsMap.php
index 6b41572..12f0a19 100644
--- a/app/Console/Commands/PhabricatorGetProjectsMap.php
+++ b/app/Console/Commands/PhabricatorGetProjectsMap.php
@@ -1,52 +1,51 @@
<?php
namespace Nasqueron\Notifications\Console\Commands;
use Illuminate\Console\Command;
use Nasqueron\Notifications\Phabricator\ProjectsMap;
use Services;
-use Storage;
class PhabricatorGetProjectsMap extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'phabricator:projectsmap';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Regenerate the projects map for each Phabricator instances';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct() {
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
foreach (Services::getForGate('Phabricator') as $service) {
$this->info("Querying projects map for " . $service->instance);
$map = ProjectsMap::fetch($service->instance);
$map->saveToCache();
$this->table(
['PHID', 'Project name'],
$map->toArray()
);
}
}
}
diff --git a/app/Http/Controllers/Gate/GateController.php b/app/Http/Controllers/Gate/GateController.php
index f754c62..6569f3a 100644
--- a/app/Http/Controllers/Gate/GateController.php
+++ b/app/Http/Controllers/Gate/GateController.php
@@ -1,107 +1,106 @@
<?php
namespace Nasqueron\Notifications\Http\Controllers\Gate;
use Nasqueron\Notifications\Features;
use Nasqueron\Notifications\Http\Controllers\Controller;
use App;
use Report;
use Response;
use Services;
-use Storage;
/**
* Represents a controller handling an entry-point for API payloads
*/
class GateController extends Controller {
///
/// Private members
///
/**
* @var string
*/
protected $door;
///
/// Requests
///
/**
* Handles GET requests
*/
public function onGet () {
// Virtually all the push APIs will send they payloads
// using a POST request, so we can provide a sensible
// default GET error message.
return view('gate/ispostonly');
}
/**
* Logs the request
*/
protected function logRequest () {
Log::info('[Gate] New payload.', [
'service' => static::SERVICE_NAME,
'door' => $this->door,
]);
}
///
/// Reports
///
/**
* Initializes the report and registers it
*/
protected function initializeReport () {
if (Features::isEnabled('ActionsReport')) {
Report::attachToGate(static::SERVICE_NAME, $this->door);
}
}
/**
* Renders the report
*
* @return Illuminate\Http\Response|null
*/
protected function renderReport () {
if (Features::isEnabled('ActionsReport')) {
$report = App::make('report');
$statusCode = $report->containsError() ? 503 : 200;
return Response::json($report)
->setStatusCode($statusCode);
}
}
///
/// Credentials
///
/**
* Gets service credentials for this gate and door
*
* @return stdClass the service credentials
*/
public function getService () {
return Services::findServiceByDoor(static::SERVICE_NAME, $this->door);
}
/**
* Gets secret for this service and door.
*
* @return string the secret, or if unknown, an empty string
*/
protected function getSecret () {
$service= $this->getService();
if ($service !== null) {
return $service->secret;
}
return "";
}
}
diff --git a/app/Http/Controllers/Gate/PhabricatorGateController.php b/app/Http/Controllers/Gate/PhabricatorGateController.php
index 747f70a..e8ed2be 100644
--- a/app/Http/Controllers/Gate/PhabricatorGateController.php
+++ b/app/Http/Controllers/Gate/PhabricatorGateController.php
@@ -1,104 +1,101 @@
<?php
namespace Nasqueron\Notifications\Http\Controllers\Gate;
use Event;
use Log;
use Request;
use Nasqueron\Notifications\Events\PhabricatorPayloadEvent;
-use Nasqueron\Notifications\Phabricator\PhabricatorStory;
-
-//use Nasqueron\Notifications\Events\PhabricatorPayloadEvent;
class PhabricatorGateController extends GateController {
///
/// Private members
///
/**
* The request content, as a structured data
*
* @var string
*/
private $payload;
///
/// Constants
///
const SERVICE_NAME = 'Phabricator';
///
/// Requests processing
///
/**
* Handles POST requests
*
* @param Request $request the HTTP request
*/
public function onPost ($door) {
$this->door = $door;
$this->instance = $this->getInstance();
if ($this->instance === "") {
abort(404, 'Unknown Phabricator instance.');
return;
}
$this->extractPayload();
$this->logRequest();
$this->onPayload();
return parent::renderReport();
}
/**
* Extracts payload from the request
*/
protected function extractPayload () {
$this->payload = Request::all();
}
/**
* Gets the instance matching this door
*
* @return string The Phabricator root URL without trailing slash
*/
protected function getInstance () {
$service = $this->getService();
if ($service === null) {
return "";
}
return $service->instance;
}
/**
* Logs the request
*/
protected function logRequest () {
Log::info('[Gate] New payload.', [
'service' => static::SERVICE_NAME,
'door' => $this->door
]);
}
///
/// Payload processing
///
protected function onPayload () {
$this->initializeReport();
Event::fire(new PhabricatorPayloadEvent(
$this->door,
$this->instance,
$this->payload
));
}
}
diff --git a/app/Listeners/AMQPEventListener.php b/app/Listeners/AMQPEventListener.php
index b7e5651..86071e4 100644
--- a/app/Listeners/AMQPEventListener.php
+++ b/app/Listeners/AMQPEventListener.php
@@ -1,146 +1,143 @@
<?php
namespace Nasqueron\Notifications\Listeners;
-use Nasqueron\Notifications\Actions\Action;
-use Nasqueron\Notifications\Actions\AMQPAction;
use Nasqueron\Notifications\Events\GitHubPayloadEvent;
use Nasqueron\Notifications\Events\NotificationEvent;
-use Nasqueron\Notifications\Events\ReportEvent;
use Nasqueron\Notifications\Analyzers\GitHubPayloadAnalyzer;
use Nasqueron\Notifications\Jobs\SendMessageToBroker;
use Nasqueron\Notifications\Notification;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Config;
class AMQPEventListener {
///
/// GitHub events
///
/**
* Gets routing key, to allow consumers to select the topic they subscribe to.
*
* @param GitHubPayloadEvent $event the payload event
*/
protected static function getGitHubEventRoutingKey (GitHubPayloadEvent $event) {
$key = [
strtolower($event->door),
self::getGroup($event),
$event->event
];
return implode('.', $key);
}
protected static function getAnalyzer (GitHubPayloadEvent $event) {
return new GitHubPayloadAnalyzer(
$event->door,
$event->event,
$event->payload
);
}
/**
* Gets the group for a specific payload
*
* @return string the group, central part of the routing key
*/
protected static function getGroup (GitHubPayloadEvent $event) {
$analyzer = self::getAnalyzer($event);
return $analyzer->getGroup();
}
/**
* Handles a GitHub payload event.
*
* @param GitHubPayloadEvent $event
* @return void
*/
public function onGitHubPayload(GitHubPayloadEvent $event) {
$this->sendRawPayload($event);
}
/**
* This is our gateway GitHub Webhooks -> Broker
*
* @param GitHubPayloadEvent $event
*/
protected function sendRawPayload(GitHubPayloadEvent $event) {
$target = Config::get('broker.targets.github_events');
$routingKey = static::getGitHubEventRoutingKey($event);
$message = json_encode($event->payload);
$job = new SendMessageToBroker($target, $routingKey, $message);
$job->handle();
}
///
/// Notifications
///
/**
* Handles a notification event.
*
* @param NotificationEvent $event
* @return void
*/
public function onNotification(NotificationEvent $event) {
$this->sendNotification($event);
}
/**
* Gets routing key, to allow consumers to select the topic they subscribe to.
*
* @param NotificationEvent $event
*/
protected static function getNotificationRoutingKey (Notification $notification) {
$key = [
$notification->project,
$notification->group,
$notification->service,
$notification->type
];
return strtolower(implode('.', $key));
}
/**
* This is our gateway specialized for distilled notifications
*
* @param NotificationEvent $event
*/
protected function sendNotification(NotificationEvent $event) {
$notification = $event->notification;
$target = Config::get('broker.targets.notifications');
$routingKey = static::getNotificationRoutingKey($notification);
$message = json_encode($notification);
$job = new SendMessageToBroker($target, $routingKey, $message);
$job->handle();
}
///
/// Events listening
///
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/
public function subscribe ($events) {
$class = 'Nasqueron\Notifications\Listeners\AMQPEventListener';
$events->listen(
'Nasqueron\Notifications\Events\GitHubPayloadEvent',
"$class@onGitHubPayload"
);
$events->listen(
'Nasqueron\Notifications\Events\NotificationEvent',
"$class@onNotification"
);
}
}
diff --git a/app/Listeners/LastPayloadSaver.php b/app/Listeners/LastPayloadSaver.php
index f8d8eb9..acd0a59 100644
--- a/app/Listeners/LastPayloadSaver.php
+++ b/app/Listeners/LastPayloadSaver.php
@@ -1,54 +1,52 @@
<?php
namespace Nasqueron\Notifications\Listeners;
use Nasqueron\Notifications\Events\Event;
-use Nasqueron\Notifications\Events\DockerHubPayloadEvent;
-use Nasqueron\Notifications\Events\GitHubPayloadEvent;
class LastPayloadSaver {
///
/// Events handling
///
/**
* Handles payload events
*/
public function onPayload (Event $event) {
self::savePayload($event->payload);
}
/**
* Saves payload to log file
*
* @param string $payload The payload to save
*/
public static function savePayload ($payload) {
$filename = storage_path('logs/payload.json');
$content = json_encode($payload);
file_put_contents($filename, $content);
}
///
/// Events listening
///
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/
public function subscribe ($events) {
$ns = 'Nasqueron\Notifications\Events';
$class = 'Nasqueron\Notifications\Listeners\LastPayloadSaver';
$eventsToListen = [
'DockerHubPayloadEvent',
'GitHubPayloadEvent',
'PhabricatorPayloadEvent',
];
foreach ($eventsToListen as $event) {
$events->listen("$ns\\$event", "$class@onPayload");
}
}
}
diff --git a/app/Listeners/NotificationListener.php b/app/Listeners/NotificationListener.php
index 1fc6243..1ff6422 100644
--- a/app/Listeners/NotificationListener.php
+++ b/app/Listeners/NotificationListener.php
@@ -1,80 +1,76 @@
<?php
namespace Nasqueron\Notifications\Listeners;
use Nasqueron\Notifications\Events\DockerHubPayloadEvent;
use Nasqueron\Notifications\Events\GitHubPayloadEvent;
-use Nasqueron\Notifications\Events\NotificationEvent;
use Nasqueron\Notifications\Events\PhabricatorPayloadEvent;
use Nasqueron\Notifications\Jobs\FireDockerHubNotification;
use Nasqueron\Notifications\Jobs\FireGitHubNotification;
use Nasqueron\Notifications\Jobs\FirePhabricatorNotification;
-use Nasqueron\Notifications\Notifications\GitHubNotification;
-
-use Event;
class NotificationListener {
///
/// Distill services' payloads into notifications
///
/**
* Handles a Docker Hub payload event.
*
* @param DockerHubPayloadEvent $event
* @return void
*/
public function onDockerHubPayload(DockerHubPayloadEvent $event) {
$job = new FireDockerHubNotification($event);
$job->handle();
}
/**
* Handles a GitHub payload event.
*
* @param GitHubPayloadEvent $event
* @return void
*/
public function onGitHubPayload(GitHubPayloadEvent $event) {
$job = new FireGitHubNotification($event);
$job->handle();
}
/**
* Handles a Phabricator payload event.
*
* @param PhabricatorPayloadEvent $event
* @return void
*/
public function onPhabricatorPayload(PhabricatorPayloadEvent $event) {
$job = new FirePhabricatorNotification($event);
$job->handle();
}
///
/// Events listening
///
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/
public function subscribe ($events) {
$class = 'Nasqueron\Notifications\Listeners\NotificationListener';
$events->listen(
'Nasqueron\Notifications\Events\DockerHubPayloadEvent',
"$class@onDockerHubPayload"
);
$events->listen(
'Nasqueron\Notifications\Events\GitHubPayloadEvent',
"$class@onGitHubPayload"
);
$events->listen(
'Nasqueron\Notifications\Events\PhabricatorPayloadEvent',
"$class@onPhabricatorPayload"
);
}
}
diff --git a/app/Providers/ServicesServiceProvider.php b/app/Providers/ServicesServiceProvider.php
index 97af9ef..0bae208 100644
--- a/app/Providers/ServicesServiceProvider.php
+++ b/app/Providers/ServicesServiceProvider.php
@@ -1,26 +1,23 @@
<?php
namespace Nasqueron\Notifications\Providers;
-use Illuminate\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;
-use Nasqueron\Notifications\Actions\ActionsReport;
-use Nasqueron\Notifications\Events\ReportEvent;
use Nasqueron\Notifications\Services\Services;
class ServicesServiceProvider extends ServiceProvider {
/**
* Registers the application services.
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function register() {
$this->app->singleton('services', function ($app) {
$path = config('services.gate.credentials');
return Services::loadFromJson($path);
});
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Nov 25, 17:46 (11 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2260666
Default Alt Text
(15 KB)
Attached To
Mode
rNOTIF Notifications center
Attached
Detach File
Event Timeline
Log In to Comment