Home
DevCentral
Search
Configure Global Search
Log In
Transactions
P45
Change Details
Change Details
Old
New
Diff
Code change of phabricator/src/infrastructure/daemon/bot/handler/PhabricatorBotFeedNotificationHandler.php to test and deploy to fix T276: --- PhabricatorBotFeedNotificationHandler.php.orig 2015-04-04 14:39:41.733038612 +0200 +++ PhabricatorBotFeedNotificationHandler.php 2015-04-04 14:39:08.554318370 +0200 @@ -83,6 +83,91 @@ return false; } + /** + * Gets the list of the projects attached to an object + * + * @param string $objectPHID the object's PHID + * @return Array the name of the projects attached to the specified object PHID + */ + private static function getProjects ($objectPHID) { + //Query projects attached to this object. + $PHIDs = self::getProjectsPHIDs($objectPHID); + $projects = id(new $class()) + ->loadAllWhere('phid IN (%Ls)', + $PHIDs); + + //Returns an array with project names + if (!count($projects)) { + return []; + } + + $projectsNames = []; + foreach ($project in $projects) { + $projectsNames[] = $project->name; + } + return $projectsNames; + } + + /** + * Gets projects' PHIDs attached to an object + * + * @param string $objectPHID the object's PHID + * @return Array the PHIDs of the projects attached to the specified object PHID + */ + private static function getProjectsPHIDs ($objectPHID) { + $object_type = phid_get_type($objectPHID); + switch ($object_type) { + case 'DREV': + $class = 'DifferentialRevision'; + break; + + case 'TASK': + $class = 'ManiphestTask'; + break; + + case 'CMIT': + $class = 'PhabricatorRepositoryCommit'; + break; + + default: + return []; + } + + $object = id(new $class()) + ->loadOneWhere('phid = %s', + $objectPHID); + if ($object) { + if ($class == "PhabricatorRepositoryCommit" || $class == "DifferentialRevision") { + return $object->getRepository()->getProjectPHIDs(); + } + return $object->getProjectPHIDs(); + } + + return []; + } + + /** + * Determines if the story message should be shown on the specified channel + * + * @param string $channel the channel we may notify + * @param string $channel the $storyObjectPHID the story object's PHID + * @return boolean true if the message should be shown; otherwise, false. + */ + private function shouldShowStoryOnChannel ($channel, $storyObjectPHID) { + $filters = $this->getConfig('notification.filtersByProject'); + if ($filters === null || !array_key_exists($channel, $filters)) { + return true; + } + $projects = $filters[$channel]; + $storyObjectProjects = $this->getProjectsPHIDs($storyObjectPHID); //TODO: replace by getProjects + foreach ($projects as $project) { + if (in_array($project, $storyObjectProjects)) { + return true; + } + } + return false; + } + public function receiveMessage(PhabricatorBotMessage $message) { return; } @@ -167,6 +252,10 @@ $channel = id(new PhabricatorBotChannel()) ->setName($channel_name); + if (!$this->shouldShowStoryOnChannel($channel_name, $story['objectPHID'])) { + continue; + } + $this->writeMessage( id(new PhabricatorBotMessage()) ->setCommand('MESSAGE') @@ -176,5 +265,4 @@ } } } - }
--- PhabricatorBotFeedNotificationHandler.php.orig 2015-04-04 14:39:41.733038612 +0200 +++ PhabricatorBotFeedNotificationHandler.php 2015-04-04 14:39:08.554318370 +0200 @@ -83,6 +83,91 @@ return false; } + /** + * Gets the list of the projects attached to an object + * + * @param string $objectPHID the object's PHID + * @return Array the name of the projects attached to the specified object PHID + */ + private static function getProjects ($objectPHID) { + //Query projects attached to this object. + $PHIDs = self::getProjectsPHIDs($objectPHID); + $projects = id(new $class()) + ->loadAllWhere('phid IN (%Ls)', + $PHIDs); + + //Returns an array with project names + if (!count($projects)) { + return []; + } + + $projectsNames = []; + foreach ($project in $projects) { + $projectsNames[] = $project->name; + } + return $projectsNames; + } + + /** + * Gets projects' PHIDs attached to an object + * + * @param string $objectPHID the object's PHID + * @return Array the PHIDs of the projects attached to the specified object PHID + */ + private static function getProjectsPHIDs ($objectPHID) { + $object_type = phid_get_type($objectPHID); + switch ($object_type) { + case 'DREV': + $class = 'DifferentialRevision'; + break; + + case 'TASK': + $class = 'ManiphestTask'; + break; + + case 'CMIT': + $class = 'PhabricatorRepositoryCommit'; + break; + + default: + return []; + } + + $object = id(new $class()) + ->loadOneWhere('phid = %s', + $objectPHID); + if ($object) { + if ($class == "PhabricatorRepositoryCommit" || $class == "DifferentialRevision") { + return $object->getRepository()->getProjectPHIDs(); + } + return $object->getProjectPHIDs(); + } + + return []; + } + + /** + * Determines if the story message should be shown on the specified channel + * + * @param string $channel the channel we may notify + * @param string $channel the $storyObjectPHID the story object's PHID + * @return boolean true if the message should be shown; otherwise, false. + */ + private function shouldShowStoryOnChannel ($channel, $storyObjectPHID) { + $filters = $this->getConfig('notification.filtersByProject'); + if ($filters === null || !array_key_exists($channel, $filters)) { + return true; + } + $projects = $filters[$channel]; + $storyObjectProjects = $this->getProjectsPHIDs($storyObjectPHID); //TODO: replace by getProjects + foreach ($projects as $project) { + if (in_array($project, $storyObjectProjects)) { + return true; + } + } + return false; + } + public function receiveMessage(PhabricatorBotMessage $message) { return; } @@ -167,6 +252,10 @@ $channel = id(new PhabricatorBotChannel()) ->setName($channel_name); + if (!$this->shouldShowStoryOnChannel($channel_name, $story['objectPHID'])) { + continue; + } + $this->writeMessage( id(new PhabricatorBotMessage()) ->setCommand('MESSAGE') @@ -176,5 +265,4 @@ } } } - }
Code change of phabricator/src/infrastructure/daemon/bot/handler/PhabricatorBotFeedNotificationHandler.php to test and deploy to fix T276:
--- PhabricatorBotFeedNotificationHandler.php.orig 2015-04-04 14:39:41.733038612 +0200 +++ PhabricatorBotFeedNotificationHandler.php 2015-04-04 14:39:08.554318370 +0200 @@ -83,6 +83,91 @@ return false; } + /** + * Gets the list of the projects attached to an object + * + * @param string $objectPHID the object's PHID + * @return Array the name of the projects attached to the specified object PHID + */ + private static function getProjects ($objectPHID) { + //Query projects attached to this object. + $PHIDs = self::getProjectsPHIDs($objectPHID); + $projects = id(new $class()) + ->loadAllWhere('phid IN (%Ls)', + $PHIDs); + + //Returns an array with project names + if (!count($projects)) { + return []; + } + + $projectsNames = []; + foreach ($project in $projects) { + $projectsNames[] = $project->name; + } + return $projectsNames; + } + + /** + * Gets projects' PHIDs attached to an object + * + * @param string $objectPHID the object's PHID + * @return Array the PHIDs of the projects attached to the specified object PHID + */ + private static function getProjectsPHIDs ($objectPHID) { + $object_type = phid_get_type($objectPHID); + switch ($object_type) { + case 'DREV': + $class = 'DifferentialRevision'; + break; + + case 'TASK': + $class = 'ManiphestTask'; + break; + + case 'CMIT': + $class = 'PhabricatorRepositoryCommit'; + break; + + default: + return []; + } + + $object = id(new $class()) + ->loadOneWhere('phid = %s', + $objectPHID); + if ($object) { + if ($class == "PhabricatorRepositoryCommit" || $class == "DifferentialRevision") { + return $object->getRepository()->getProjectPHIDs(); + } + return $object->getProjectPHIDs(); + } + + return []; + } + + /** + * Determines if the story message should be shown on the specified channel + * + * @param string $channel the channel we may notify + * @param string $channel the $storyObjectPHID the story object's PHID + * @return boolean true if the message should be shown; otherwise, false. + */ + private function shouldShowStoryOnChannel ($channel, $storyObjectPHID) { + $filters = $this->getConfig('notification.filtersByProject'); + if ($filters === null || !array_key_exists($channel, $filters)) { + return true; + } + $projects = $filters[$channel]; + $storyObjectProjects = $this->getProjectsPHIDs($storyObjectPHID); //TODO: replace by getProjects + foreach ($projects as $project) { + if (in_array($project, $storyObjectProjects)) { + return true; + } + } + return false; + } + public function receiveMessage(PhabricatorBotMessage $message) { return; } @@ -167,6 +252,10 @@ $channel = id(new PhabricatorBotChannel()) ->setName($channel_name); + if (!$this->shouldShowStoryOnChannel($channel_name, $story['objectPHID'])) { + continue; + } + $this->writeMessage( id(new PhabricatorBotMessage()) ->setCommand('MESSAGE') @@ -176,5 +265,4 @@ } } } - }
Continue