Page MenuHomeDevCentral

PhabricatorBotFeedNotificationHandler now filters IRC notifications by projects
ActivePublic

Authored by dereckson on Apr 4 2015, 12:46.
--- 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 @@
}
}
}
-
}

Event Timeline

dereckson changed the title of this paste from untitled to PhabricatorBotFeedNotificationHandler now filters IRC notifications by projects.
dereckson updated the paste's language from autodetect to php.
dereckson added projects: Tasacora, DevCentral.
dereckson edited the content of this paste. (Show Details)

This code change of phabricator/src/infrastructure/daemon/bot/handler/PhabricatorBotFeedNotificationHandler.php is to test and deploy to fix T276.