Page MenuHomeDevCentral

D113.diff
No OneTemporary

D113.diff

diff --git a/app/Events/DockerHubPayloadEvent.php b/app/Events/DockerHubPayloadEvent.php
new file mode 100644
--- /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
--- /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
--- /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
--- a/app/Listeners/LastPayloadSaver.php
+++ b/app/Listeners/LastPayloadSaver.php
@@ -3,6 +3,7 @@
namespace Nasqueron\Notifications\Listeners;
use Nasqueron\Notifications\Events\Event;
+use Nasqueron\Notifications\Events\DockerHubPayloadEvent;
use Nasqueron\Notifications\Events\GitHubPayloadEvent;
class LastPayloadSaver {
@@ -43,5 +44,9 @@
'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
--- a/app/Listeners/NotificationListener.php
+++ b/app/Listeners/NotificationListener.php
@@ -2,9 +2,11 @@
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;
@@ -18,6 +20,17 @@
///
/**
+ * 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
@@ -51,6 +64,10 @@
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"
);
diff --git a/app/Notifications/DockerHubNotification.php b/app/Notifications/DockerHubNotification.php
new file mode 100644
--- /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
--- a/config/gate.php
+++ b/config/gate.php
@@ -13,6 +13,7 @@
*/
'controllers' => [
+ 'DockerHub',
'GitHub',
'Phabricator',
],

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 29, 01:39 (22 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2270404
Default Alt Text
D113.diff (9 KB)

Event Timeline