Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3794078
D641.id1610.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
9 KB
Referenced Files
None
Subscribers
None
D641.id1610.diff
View Options
diff --git a/app/Events/DockerHubPayloadEvent.php b/app/Events/DockerHubPayloadEvent.php
--- a/app/Events/DockerHubPayloadEvent.php
+++ b/app/Events/DockerHubPayloadEvent.php
@@ -18,7 +18,7 @@
* The event triggering this request
* @var string
*/
- public $event = "push";
+ public $event;
/**
* The request content, as a structured data
@@ -27,6 +27,19 @@
public $payload;
/**
+ * Gets event according the kind of payload we receive.
+ *
+ * @return string
+ */
+ public function getEvent () {
+ if (isset($this->payload->repository->repo_url)) {
+ return "push";
+ }
+
+ return "buildFailure";
+ }
+
+ /**
* Creates a new event instance.
*
* @param string $door
@@ -34,7 +47,7 @@
*/
public function __construct($door, $payload) {
$this->door = $door;
- //$this->event = $event; // Currently, the API only send push events
$this->payload = $payload;
+ $this->event = $this->getEvent();
}
}
diff --git a/app/Notifications/DockerHubNotification.php b/app/Notifications/DockerHubNotification.php
--- a/app/Notifications/DockerHubNotification.php
+++ b/app/Notifications/DockerHubNotification.php
@@ -4,6 +4,10 @@
use Nasqueron\Notifications\Notification;
+use Mailgun;
+
+use InvalidArgumentException;
+
/**
* A Docker Hub notification.
*
@@ -34,19 +38,109 @@
$this->group = "docker";
// Properties from the payload
- $this->text = $this->getText();
- $this->link = $payload->repository->repo_url;
+ $this->analyzeByEvent($event);
+ }
+
+ ///
+ /// Analyze by event
+ ///
+
+ /**
+ * Analyzes by event
+ */
+ private function analyzeByEvent ($event) {
+ $method = "analyze" . ucfirst($event);
+
+ if (!method_exists($this, $method)) {
+ throw new InvalidArgumentException("Event $event doesn't have a matching $method method.");
+ }
+
+ $this->$method();
+ }
+
+ ///
+ /// Analyze by event: push
+ ///
+
+ /**
+ * Analyzes a push payload sent by the Docker Hub API.
+ */
+ private function analyzePush () {
+ $this->text = $this->getTextForPush();
+ $this->link = $this->rawContent->repository->repo_url;
}
+
/**
* Gets the notification text. Intended to convey a short message (thing Twitter or IRC).
*
* @return string
*/
- public function getText () {
+ private function getTextForPush () {
$repo = $this->rawContent->repository->repo_name;
$who = $this->rawContent->push_data->pusher;
return "New image pushed to Docker Hub registry for $repo by $who";
}
+ ///
+ /// Analyze by event: buildFailure
+ ///
+
+ /**
+ * Analyzes a build failure payload sent by mail,
+ * then fired by the Mailgun API.
+ */
+ private function analyzeBuildFailure () {
+ $this->rawContent = $this->getMailGunPayload();
+
+ $this->text = $this->getTextForBuildFailure();
+ $this->link = $this->getLinkForBuildFailure();
+ }
+
+ /**
+ * Gets a MailGun message.
+ *
+ * @return stdClass
+ */
+ private function getMailGunPayload () {
+ return Mailgun::fetchMessageFromPayload($this->rawContent);
+ }
+
+ /**
+ * @return string
+ */
+ private function getMailBody () {
+ $bodyProperty = 'body-plain';
+ return $this->rawContent->$bodyProperty;
+ }
+
+ /**
+ * Extracts the text for a build failure from the mail body.
+ *
+ * @return string
+ */
+ private function getTextForBuildFailure () {
+ $body = $this->getMailBody();
+
+ $regex = "@\"(.*?\/.*?)\"@";
+ preg_match($regex, $body, $matches);
+ $repo = $matches[1];
+
+ return "Image build by Docker Hub registry failure for $repo";
+ }
+
+ /**
+ * Extracts the URL for a build failure from the mail body.
+ *
+ * @return string
+ */
+ private function getLinkForBuildFailure () {
+ $body = $this->getMailBody();
+
+ $regex = "@(https\:\/\/hub.docker.com\/r.*)@";
+ preg_match($regex, $body, $matches);
+
+ return $matches[1];
+ }
+
}
diff --git a/tests/Notifications/DockerHubNotificationTest.php b/tests/Notifications/DockerHubNotificationTest.php
--- a/tests/Notifications/DockerHubNotificationTest.php
+++ b/tests/Notifications/DockerHubNotificationTest.php
@@ -5,41 +5,76 @@
use Nasqueron\Notifications\Notifications\DockerHubNotification;
use Nasqueron\Notifications\Tests\TestCase;
+use Keruald\Mailgun\Tests\WithMockHttpClient;
+
+use Mockery;
+
class DockerHubNotificationTest extends TestCase {
- /**
- * @var Nasqueron\Notifications\Notifications\DockerHubNotification
- */
- private $notification;
+
+ use WithMockHttpClient;
/**
- * @var stdClass
+ * @return Nasqueron\Notifications\Notifications\DockerHubNotification
*/
- private $payload;
-
- public function setUp () {
- $path = __DIR__ . '/../data/payloads/DockerHubPushPayload.json';
- $this->payload = json_decode(file_get_contents($path));
+ protected function prepareNotification ($event) {
+ $path = __DIR__ . '/../data/payloads/DockerHub' . ucfirst($event)
+ . 'Payload.json';
+ $payload = json_decode(file_get_contents($path));
- $this->notification = new DockerHubNotification(
- "Acme",
- "push",
- $this->payload
- );
+ return new DockerHubNotification("Acme", $event, $payload);
}
- public function testProperties () {
- $this->assertSame("DockerHub", $this->notification->service);
- $this->assertSame("Acme", $this->notification->project);
- $this->assertSame("docker", $this->notification->group);
- $this->assertSame($this->payload, $this->notification->rawContent);
- $this->assertSame("push", $this->notification->type);
+ public function testPropertiesForPush () {
+ $notification = $this->prepareNotification("push");
+
+ $this->assertSame("DockerHub", $notification->service);
+ $this->assertSame("Acme", $notification->project);
+ $this->assertSame("docker", $notification->group);
+ $this->assertSame("push", $notification->type);
$this->assertSame(
"New image pushed to Docker Hub registry for svendowideit/testhook by trustedbuilder",
- $this->notification->text
+ $notification->text
);
$this->assertSame(
"https://registry.hub.docker.com/u/svendowideit/testhook/",
- $this->notification->link
+ $notification->link
);
}
+
+ public function testPropertiesForBuildFailure () {
+ $this->mockMailgunServiceProvider();
+ $notification = $this->prepareNotification("buildFailure");
+ $this->assertSame("buildFailure", $notification->type);
+
+ $this->assertSame(
+ "Image build by Docker Hub registry failure for acme/foo",
+ $notification->text
+ );
+ $this->assertSame(
+ "https://hub.docker.com/r/acme/foo/builds/abcdef123456/",
+ $notification->link
+ );
+ }
+
+ ///
+ /// Helper mock method
+ ///
+
+ /**
+ * Injects into our container a mock of MailgunMessageFactory
+ */
+ protected function mockMailgunServiceProvider () {
+ $mock = Mockery::mock('Keruald\Mailgun\MailgunMessageFactory');
+ $payload = $this->mockMailgunResponse();
+ $mock->shouldReceive('fetchMessageFromPayload')->once()->andReturn($payload);
+ $this->app->instance('mailgun', $mock);
+ }
+
+ /**
+ * @return stdClass
+ */
+ protected function mockMailgunResponse () {
+ return json_decode($this->mockHttpClientResponseBody());
+ }
+
}
diff --git a/tests/data/payloads/DockerHubBuildFailurePayload.json b/tests/data/payloads/DockerHubBuildFailurePayload.json
new file mode 100644
--- /dev/null
+++ b/tests/data/payloads/DockerHubBuildFailurePayload.json
@@ -0,0 +1,40 @@
+{
+ "tags": [],
+ "timestamp": 1472676730.131279,
+ "storage": {
+ "url": "https://so.api.mailgun.net/v3/domains/notifications.domain.tld/messages/somehash",
+ "key": "somekey"
+ },
+ "envelope": {
+ "sender": "no-reply@notify.docker.com",
+ "transport": "smtp",
+ "targets": "docker-hub-notify@notifications.domain.tld"
+ },
+ "recipient-domain": "notifications.domain.tld",
+ "method": "smtp",
+ "campaigns": [],
+ "user-variables": {},
+ "flags": {
+ "is-routed": null,
+ "is-authenticated": false,
+ "is-system-test": false,
+ "is-test-mode": false
+ },
+ "log-level": "info",
+ "id": "te1cKBxQTJWxpalPRcnmBw",
+ "message": {
+ "headers": {
+ "to": "docker-hub-notify@notifications.domain.tld",
+ "message-id": "CAKg6iAHgQ4e8etV=gi4pbBueeA-o0Qsv02u1Cp9ZKTH8-tL9xw@mail.gmail.com",
+ "from": "no-reply@notify.docker.com",
+ "subject": "There's been an issue with your automated build"
+ },
+ "attachments": [],
+ "recipients": [
+ "docker-hub-notify@notifications.domain.tld"
+ ],
+ "size": 2674
+ },
+ "recipient": "docker-hub-notify@notifications.domain.tld",
+ "event": "accepted"
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Nov 28, 22:19 (10 h, 40 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2270075
Default Alt Text
D641.id1610.diff (9 KB)
Attached To
Mode
D641: Accept mail notifications for DockerHub build failures
Attached
Detach File
Event Timeline
Log In to Comment