Page MenuHomeDevCentral

D635.id1584.diff
No OneTemporary

D635.id1584.diff

diff --git a/app/Console/Commands/NotificationsPayload.php b/app/Console/Commands/NotificationsPayload.php
--- a/app/Console/Commands/NotificationsPayload.php
+++ b/app/Console/Commands/NotificationsPayload.php
@@ -2,6 +2,8 @@
namespace Nasqueron\Notifications\Console\Commands;
+use Nasqueron\Notifications\Phabricator\PhabricatorStory;
+
use Illuminate\Console\Command;
use InvalidArgumentException;
@@ -123,9 +125,24 @@
$keys = $this->getNotificationConstructorParameters();
$values = $this->argument('args');
- $values['payload'] = json_decode($this->payload);
+ $values['payload'] = $this->payload;
$this->constructor = self::argumentsArrayCombine($keys, $values);
+ $this->constructor['payload'] = $this->formatPayload();
+ }
+
+ /**
+ * Formats payload to pass to constructor
+ *
+ * @return PhabricatorStory|stdClass A deserialization of the payload
+ */
+ private function formatPayload() {
+ if ($this->service === "Phabricator") {
+ $project = $this->constructor['project'];
+ return PhabricatorStory::loadFromJson($project, $this->payload);
+ }
+
+ return json_decode($this->payload);
}
/**
diff --git a/app/Notifications/PhabricatorNotification.php b/app/Notifications/PhabricatorNotification.php
--- a/app/Notifications/PhabricatorNotification.php
+++ b/app/Notifications/PhabricatorNotification.php
@@ -13,28 +13,21 @@
*/
private $analyzer = null;
- private $story;
-
-
/**
* Initializes a new PhabricatorNotification instance
*
* @param string $project The project for this notification
- * @param PhabricatorStory $story The story to convert into a notification
- * @param string[] $projects the list of the projects for this story
+ * @param PhabricatorStory $payload The story to convert into a notification
*/
- public function __construct ($project, PhabricatorStory $story) {
- // Private property used by the analyzer
- $this->story = $story;
-
+ public function __construct ($project, PhabricatorStory $payload) {
// Straightforward properties
$this->service = "Phabricator";
$this->project = $project;
- $this->rawContent = json_encode($story);
- $this->text = $story->text;
+ $this->rawContent = $payload;
+ $this->text = $payload->text;
// Analyzes and fills
- $this->type = $story->getObjectType();
+ $this->type = $payload->getObjectType();
$this->group = $this->getGroup();
$this->link = $this->getLink();
}
@@ -48,7 +41,7 @@
if ($this->analyzer === null) {
$this->analyzer = new PhabricatorPayloadAnalyzer(
$this->project,
- $this->story
+ $this->rawContent
);
}
return $this->analyzer;
diff --git a/app/Phabricator/PhabricatorStory.php b/app/Phabricator/PhabricatorStory.php
--- a/app/Phabricator/PhabricatorStory.php
+++ b/app/Phabricator/PhabricatorStory.php
@@ -2,6 +2,8 @@
namespace Nasqueron\Notifications\Phabricator;
+use InvalidArgumentException;
+
class PhabricatorStory {
///
@@ -98,6 +100,25 @@
return $instance;
}
+ /**
+ * Initializes a new instance of PhabricatorStory from a JSON payload.
+ *
+ * This is intended to parse files saved by LastPayloadSaver::savePayload.
+ *
+ * @param string $instanceName The Phabricator instance name
+ * @param string $payload The data submitted by Phabricator's JSON representation
+ * @return PhabricatorStory
+ */
+ public static function loadFromJson ($instanceName, $payload) {
+ $array = json_decode($payload, true);
+
+ if (!is_array($array)) {
+ throw new InvalidArgumentException("Payload should be deserializable as an array.");
+ }
+
+ return self::loadFromArray($instanceName, $array);
+ }
+
///
/// Helper methods
///
diff --git a/tests/Console/Commands/NotificationsPayloadTest.php b/tests/Console/Commands/NotificationsPayloadTest.php
--- a/tests/Console/Commands/NotificationsPayloadTest.php
+++ b/tests/Console/Commands/NotificationsPayloadTest.php
@@ -28,6 +28,22 @@
$this->assertContains('svendowideit\/testhook', $this->tester->getDisplay());
}
+ public function testPhabricatorPayload () {
+ $path = __DIR__ . '/../../data/payloads/PhabricatorPastePayload.json';
+ $this->tester->execute([
+ 'command' => $this->command->getName(),
+ 'service' => 'Phabricator',
+ 'payload' => $path,
+ 'args' => [
+ 'Acme',
+ ],
+ ]);
+
+ $this->assertContains('"service": "Phabricator"', $this->tester->getDisplay());
+ $this->assertContains('"project": "Acme"', $this->tester->getDisplay());
+ $this->assertContains('"type": "PSTE"', $this->tester->getDisplay());
+ }
+
/**
* @expectedException InvalidArgumentException
*/
diff --git a/tests/Http/PayloadFullTest.php b/tests/Http/PayloadFullTest.php
--- a/tests/Http/PayloadFullTest.php
+++ b/tests/Http/PayloadFullTest.php
@@ -92,11 +92,8 @@
$this->assertResponseOk();
}
- /**
- * Tests a Phabricator gate payload.
- */
- public function testPhabricatorPayload () {
- $data = [
+ private function getDataForPhabricatorPayloadTests () {
+ return [
'storyID' => 3849,
'storyType' => 'PhabricatorApplicationTransactionFeedStory',
'storyData[objectPHID]' => 'PHID-TASK-l34fw5wievp6n6rnvpuk',
@@ -105,6 +102,13 @@
'storyText' => 'quux moved T123: Lorem ipsum dolor to Backlog on the Foo workboard.',
'epoch' => 1450654419,
];
+ }
+
+ /**
+ * Tests a Phabricator gate payload.
+ */
+ public function testPhabricatorPayload () {
+ $data = $this->getDataForPhabricatorPayloadTests();
$this->post('/gate/Phabricator/Acme', $data)->seeJson([
'gate' => 'Phabricator',
@@ -112,6 +116,13 @@
'action' => 'AMQPAction'
]);
$this->assertResponseOk();
+ }
+
+ /**
+ * Tests a Phabricator gate payload, when the door doesn't exist.
+ */
+ public function testPhabricatorPayloadOnNotExistingDoor () {
+ $data = $this->getDataForPhabricatorPayloadTests();
$this->post('/gate/Phabricator/NotExistingDoor', $data);
$this->assertResponseStatus(404);
diff --git a/tests/data/payloads/PhabricatorPastePayload.json b/tests/data/payloads/PhabricatorPastePayload.json
new file mode 100644
--- /dev/null
+++ b/tests/data/payloads/PhabricatorPastePayload.json
@@ -0,0 +1 @@
+{"storyID":"3850","storyType":"PhabricatorApplicationTransactionFeedStory","storyData":{"objectPHID":"PHID-PSTE-2lppddolr46bbyj6ti66","transactionPHIDs":{"PHID-XACT-PSTE-2j7d6wjobjz5ncb":"PHID-XACT-PSTE-2j7d6wjobjz5ncb","PHID-XACT-PSTE-d2g4pwcx3iuu2n7":"PHID-XACT-PSTE-d2g4pwcx3iuu2n7","PHID-XACT-PSTE-6krgu766clik3vl":"PHID-XACT-PSTE-6krgu766clik3vl","PHID-XACT-PSTE-uztpigtlxp6gmtr":"PHID-XACT-PSTE-uztpigtlxp6gmtr"}},"storyAuthorPHID":"PHID-USER-fnetlprx7zdotfm2hdrz","storyText":"dereckson updated the title for P145 `pkg audit` on Ysul.","epoch":"1450694711"}
\ No newline at end of file

File Metadata

Mime Type
text/plain
Expires
Fri, Dec 20, 04:02 (2 h, 49 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2307374
Default Alt Text
D635.id1584.diff (7 KB)

Event Timeline