Page MenuHomeDevCentral

D636.diff
No OneTemporary

D636.diff

diff --git a/app/Actions/TriggerDockerHubBuildAction.php b/app/Actions/TriggerDockerHubBuildAction.php
new file mode 100644
--- /dev/null
+++ b/app/Actions/TriggerDockerHubBuildAction.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Nasqueron\Notifications\Actions;
+
+class TriggerDockerHubBuildAction extends Action {
+ /**
+ * The Docker Hub image
+ *
+ * @var string
+ */
+ public $image;
+
+ /**
+ * Initializes a new instance of a DockerHub build trigger action to report
+ */
+ public function __construct ($image) {
+ parent::__construct();
+
+ $this->image = $image;
+ }
+}
diff --git a/app/Jobs/TriggerDockerHubBuild.php b/app/Jobs/TriggerDockerHubBuild.php
new file mode 100644
--- /dev/null
+++ b/app/Jobs/TriggerDockerHubBuild.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace Nasqueron\Notifications\Jobs;
+
+use Nasqueron\Notifications\Actions\ActionError;
+use Nasqueron\Notifications\Actions\TriggerDockerHubBuildAction;
+use Nasqueron\Notifications\Events\ReportEvent;
+
+use DockerHub;
+use Event;
+
+use BadMethodCallException;
+use Exception;
+
+/**
+ * This class allows to trigger a new Docker Hub build.
+ */
+class TriggerDockerHubBuild extends Job {
+
+ ///
+ /// Private members
+ ///
+
+ /**
+ * @var string The Docker image to trigger a build for
+ */
+ private $image;
+
+ /**
+ * @var TriggerDockerHubBuildAction
+ */
+ private $actionToReport;
+
+ ///
+ /// Constructor
+ ///
+
+ /**
+ * Initializes a new instance of TriggerDockerHubBuild.
+ */
+ public function __construct ($image) {
+ $this->image = $image;
+ }
+
+ ///
+ /// Task
+ ///
+
+ /**
+ * Executes the job.
+ *
+ * @return void
+ */
+ public function handle () {
+ $this->initializeReport();
+ $this->triggerBuild();
+ $this->sendReport();
+ }
+
+ /**
+ * Initializes the actions report.
+ */
+ private function initializeReport () {
+ $this->actionToReport = new TriggerDockerHubBuildAction($this->image);
+ }
+
+ /**
+ * Triggers a new Docker Hub build.
+ */
+ private function triggerBuild () {
+ try {
+ DockerHub::build($this->image);
+ } catch (Exception $ex) {
+ $actionError = new ActionError($ex);
+ $this->actionToReport->attachError($actionError);
+ }
+ }
+
+ /**
+ * Fires a report event with the actions report.
+ */
+ private function sendReport () {
+ $event = new ReportEvent($this->actionToReport);
+ Event::fire($event);
+ }
+
+}
diff --git a/app/Listeners/DockerHubListener.php b/app/Listeners/DockerHubListener.php
new file mode 100644
--- /dev/null
+++ b/app/Listeners/DockerHubListener.php
@@ -0,0 +1,81 @@
+<?php
+
+namespace Nasqueron\Notifications\Listeners;
+
+use Nasqueron\Notifications\Events\GitHubPayloadEvent;
+use Nasqueron\Notifications\Jobs\TriggerDockerHubBuild;
+
+use Illuminate\Events\Dispatcher;
+
+use DockerHub;
+
+/**
+ * Listens to events Docker Hub is interested by.
+ */
+class DockerHubListener {
+
+ ///
+ /// GitHub → Phabricator
+ ///
+
+ /**
+ * Handles payload events.
+ *
+ * @param GitHubPayloadEvent $event The GitHub payload event
+ */
+ public function onGitHubPayload (GitHubPayloadEvent $event) {
+ if ($this->shouldNotify($event)) {
+ $this->notifyNewCommits($event);
+ }
+ }
+
+ /**
+ * Determines if the event should be notified to Docker Hub.
+ * We're interested by push events, for repos with Docker images
+ * we've a token to trigger a build.
+ *
+ * @param GitHubPayloadEvent $event The GitHub payload event
+ * @return bool
+ */
+ public function shouldNotify (GitHubPayloadEvent $event) {
+ return $event->event === 'push'
+ && DockerHub::hasToken($this->getRepository($event));
+ }
+
+ /**
+ * Notifies Phabricator there are new commits to pull.
+ *
+ * @param GitHubPayloadEvent $event The GitHub payload event
+ */
+ public function notifyNewCommits (GitHubPayloadEvent $event) {
+ $job = new TriggerDockerHubBuild($this->getRepository($event));
+ $job->handle();
+ }
+
+ /**
+ * Extracts repository fullname (e.g. acme/foo) from event.
+ *
+ * @var string
+ */
+ private function getRepository (GitHubPayloadEvent $event) {
+ return $event->payload->repository->full_name;
+ }
+
+ ///
+ /// Events listening
+ ///
+
+ /**
+ * Registers the listeners for the subscriber.
+ *
+ * @param Dispatcher $events
+ */
+ public function subscribe (Dispatcher $events) {
+ $class = DockerHubListener::class;
+ $events->listen(
+ GitHubPayloadEvent::class,
+ "$class@onGitHubPayload"
+ );
+ }
+
+}
diff --git a/composer.json b/composer.json
--- a/composer.json
+++ b/composer.json
@@ -13,7 +13,7 @@
"php": ">=5.6.0",
"laravel/framework": "5.2.*",
"guzzlehttp/guzzle": "^6.2",
- "keruald/dockerhub": "^0.0.2",
+ "keruald/dockerhub": "^0.0.3",
"keruald/github": "^0.2.0",
"keruald/broker": "^0.4.1",
"keruald/mailgun": "^0.0.1",
diff --git a/config/app.php b/config/app.php
--- a/config/app.php
+++ b/config/app.php
@@ -195,6 +195,7 @@
'listeners' => [
Nasqueron\Notifications\Listeners\AMQPEventListener::class,
+ Nasqueron\Notifications\Listeners\DockerHubListener::class,
Nasqueron\Notifications\Listeners\LastPayloadSaver::class,
Nasqueron\Notifications\Listeners\NotificationListener::class,
Nasqueron\Notifications\Listeners\PhabricatorListener::class,

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 27, 12:09 (16 h, 20 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2266826
Default Alt Text
D636.diff (5 KB)

Event Timeline