Page MenuHomeDevCentral

No OneTemporary

diff --git a/app/Events/DockerHubPayloadEvent.php b/app/Events/DockerHubPayloadEvent.php
new file mode 100644
index 0000000..8a1941f
--- /dev/null
+++ b/app/Events/DockerHubPayloadEvent.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Nasqueron\Notifications\Events;
+
+use Nasqueron\Notifications\Events\Event;
+use Illuminate\Queue\SerializesModels;
+
+class DockerHubPayloadEvent extends Event {
+ use SerializesModels;
+
+ /**
+ * The gate door which receives the request
+ * @var string
+ */
+ public $door;
+
+ /**
+ * The event triggering this request
+ * @var string
+ */
+ public $event = "push";
+
+ /**
+ * The request content, as a structured data
+ * @var stdClass
+ */
+ public $payload;
+
+ /**
+ * Creates a new event instance.
+ *
+ * @param string $door
+ * @param stdClass $payload
+ */
+ public function __construct($door, $payload) {
+ $this->door = $door;
+ //$this->event = $event; // Currently, the API only send push events
+ $this->payload = $payload;
+ }
+}
diff --git a/app/Http/Controllers/Gate/DockerHubGateController.php b/app/Http/Controllers/Gate/DockerHubGateController.php
new file mode 100644
index 0000000..87a6988
--- /dev/null
+++ b/app/Http/Controllers/Gate/DockerHubGateController.php
@@ -0,0 +1,94 @@
+<?php
+
+namespace Nasqueron\Notifications\Http\Controllers\Gate;
+
+use Event;
+use Log;
+use Request;
+
+use Nasqueron\Notifications\Events\DockerHubPayloadEvent;
+
+class DockerHubGateController extends GateController {
+
+ ///
+ /// Private members
+ ///
+
+ /**
+ * The request content, as a structured data
+ *
+ * @var stdClass
+ */
+ private $payload;
+
+ /**
+ * The request content
+ *
+ * @var string
+ */
+ private $rawRequestContent;
+
+ ///
+ /// Constants
+ ///
+
+ const SERVICE_NAME = 'DockerHub';
+
+ ///
+ /// Request processing
+ ///
+
+ /**
+ * Handles POST requests
+ *
+ * @param Request $request the HTTP request
+ * @return Illuminate\Http\Response
+ */
+ public function onPost ($door) {
+ // Parses the request and check if it's legit
+
+ $this->door = $door;
+ $this->extractPayload();
+
+ // Process the request
+
+ $this->logRequest();
+ $this->onPayload();
+
+ // Output
+
+ return parent::renderReport();
+ }
+
+ /**
+ * Extracts payload from the request
+ */
+ protected function extractPayload () {
+ $request = Request::instance();
+ $this->rawRequestContent = $request->getContent();
+ $this->payload = json_decode($this->rawRequestContent);
+ }
+
+ /**
+ * 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 DockerHubPayloadEvent(
+ $this->door,
+ $this->payload
+ ));
+ }
+}
diff --git a/app/Jobs/FireDockerHubNotification.php b/app/Jobs/FireDockerHubNotification.php
new file mode 100644
index 0000000..6e589a5
--- /dev/null
+++ b/app/Jobs/FireDockerHubNotification.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Nasqueron\Notifications\Jobs;
+
+use Illuminate\Contracts\Bus\SelfHandling;
+use Nasqueron\Notifications\Notifications\DockerHubNotification;
+use Nasqueron\Notifications\Events\DockerHubPayloadEvent;
+use Nasqueron\Notifications\Events\NotificationEvent;
+use Nasqueron\Notifications\Jobs\Job;
+
+use Event;
+
+class FireDockerHubNotification extends Job implements SelfHandling {
+
+ /**
+ * @var DockerHubPayloadEvent;
+ */
+ private $event;
+
+ /**
+ * Initializes a new instance of FireDockerHubNotification
+ *
+ * @param DockerHubPayloadEvent $event The event to notify
+ */
+ public function __construct (DockerHubPayloadEvent $event) {
+ $this->event = $event;
+ }
+
+ ///
+ /// Task
+ ///
+
+ /**
+ * Executes the job.
+ *
+ * @return void
+ */
+ public function handle() {
+ $notification = $this->createNotification();
+ Event::fire(new NotificationEvent($notification));
+ }
+
+ /**
+ * Creates a DockerHub notification
+ *
+ * @param DockerHubPayloadEvent $event
+ * @return Notification the notification
+ */
+ protected function createNotification() {
+ return new DockerHubNotification(
+ $this->event->door, // project
+ $this->event->event, // event type
+ $this->event->payload // raw content
+ );
+ }
+}
diff --git a/app/Listeners/LastPayloadSaver.php b/app/Listeners/LastPayloadSaver.php
index b55cd7d..811ec39 100644
--- a/app/Listeners/LastPayloadSaver.php
+++ b/app/Listeners/LastPayloadSaver.php
@@ -1,47 +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) {
$class = 'Nasqueron\Notifications\Listeners\LastPayloadSaver';
$events->listen(
'Nasqueron\Notifications\Events\GitHubPayloadEvent',
"$class@onPayload"
);
+ $events->listen(
+ 'Nasqueron\Notifications\Events\DockerHubPayloadEvent',
+ "$class@onPayload"
+ );
}
}
\ No newline at end of file
diff --git a/app/Listeners/NotificationListener.php b/app/Listeners/NotificationListener.php
index 5b0c7e1..23ef5e4 100644
--- a/app/Listeners/NotificationListener.php
+++ b/app/Listeners/NotificationListener.php
@@ -1,63 +1,80 @@
<?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"
);
}
}
\ No newline at end of file
diff --git a/app/Notifications/DockerHubNotification.php b/app/Notifications/DockerHubNotification.php
new file mode 100644
index 0000000..5d9f9d9
--- /dev/null
+++ b/app/Notifications/DockerHubNotification.php
@@ -0,0 +1,52 @@
+<?php
+
+namespace Nasqueron\Notifications\Notifications;
+
+use Nasqueron\Notifications\Notification;
+
+/**
+ * A Docker Hub notification.
+ *
+ * As we always sort them to the 'docker' group, and the registry only fires
+ * one kind of event, this is pretty straightforward without any need for
+ * configuration files or analyser class.
+ *
+ * HOW TO IMPLEMENT PAYLOADS SORT PER REPOSITORY?
+ *
+ * If you want to extend this to sort Docker images through some rules, we
+ * suggest you add a feature request to Docker to include source repository
+ * for the image, then call the GitHubPayloadAnalyzer with this repo instead
+ * of implementing a new one. This will allows to avoid to maintain two sets
+ * of configuration, one for the GitHub repos, one for the Docker repos.
+ *
+ * Even without that, you can probably be safe with a class or a method to map
+ * GitHub and Docker names, either because they are the same, either because
+ * there is a prefix: e.g. nasqueron/arcanist and nasqueron/docker-arcanist.
+ */
+class DockerHubNotification extends Notification {
+
+ public function __construct ($project, $event, $payload) {
+ // Straightforward properties
+ $this->service = "DockerHub";
+ $this->project = $project;
+ $this->type = $event;
+ $this->rawContent = $payload;
+ $this->group = "docker";
+
+ // Properties from the payload
+ $this->text = $this->getText();
+ $this->link = $payload->repository->repo_url;
+ }
+
+ /**
+ * Gets the notification text. Intended to convey a short message (thing Twitter or IRC).
+ *
+ * @return string
+ */
+ public function getText () {
+ $repo = $this->rawContent->repository->repo_name;
+ $who = $this->rawContent->push_data->pusher;
+ return "New image pushed to Docker Hub registry for $repo by $who";
+ }
+
+}
diff --git a/config/gate.php b/config/gate.php
index 1075b46..de9e5bc 100644
--- a/config/gate.php
+++ b/config/gate.php
@@ -1,20 +1,21 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| Gate controllers
|--------------------------------------------------------------------------
|
| Notifications center accept payload from several services and calls
| matching gate controllers to process messages.
|
*/
'controllers' => [
+ 'DockerHub',
'GitHub',
'Phabricator',
],
];

File Metadata

Mime Type
text/x-diff
Expires
Sat, Mar 21, 05:41 (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3542390
Default Alt Text
(11 KB)

Event Timeline