getApplication(); $services = $application->getServiceManager(); $manager = $services->get('queue'); $events = $manager->getEventManager(); $logger = $services->get('logger'); // connect to all tasks and write activity data // we do this late (low-priority) so all handlers have // a chance to influence the activity model. $events->attach( array('task.commit', 'task.change'), function ($event) use ($services, $logger) { $logger->info("Slack: activity..."); // task.change doesn't include the change object; fetch it if we need to $p4Admin = $services->get('p4_admin'); $change = $event->getParam('change'); if (!$change instanceof Change) { try { $change = Change::fetch($event->getParam('id'), $p4Admin); $event->setParam('change', $change); } catch (SpecNotFoundException $e) { } catch (\InvalidArgumentException $e) { } } // if this isn't a submitted change; nothing to do if (!$change instanceof Change || !$change->isSubmitted()) { $logger->info("Slack: not a change..."); return; } // prepare list of projects affected by the change $impacted = Project::getAffectedByChange($change, $p4Admin); if ($impacted) { $logger->info("Slack: impacted..."); $projects = Project::fetchAll(array(Project::FETCH_BY_IDS => array_keys($impacted)), $p4Admin); foreach ($projects as $projectId => $project) { $logger->info("Slack: impacted: $projectId"); // URL to POST messages to Slack $url = $project->getSlack(); // Skip projects with no Slack URL if(!isset($url) || trim($url)==='') { continue; } // Host address of Swarm for link back URLs $host = $services->get('viewhelpermanager')->get('qualifiedUrl'); // Icon and User for messages in feed Slack $config = $services->get('config'); $config = $config['slack']; $max = (int) $config['max_length']; $message = new Message($host, $change, $max); $text = $message->toString(); $this->postSlack($url, $text, $config, $logger); } } $logger->info("Slack: end."); }, // set our priority, -90 puts us right before the end, but before the activity is published -110 ); } public function postSlack($url, $msg, $config, $logger) { $logger->info("Slack: POST to $url"); $icon = $config['icon']; $user = $config['user']; try { $json = array( 'payload' => json_encode( array( 'username' => $user, 'icon_url' => $icon, 'text' => $msg ) ) ); $request = new Request(); $request->setUri($url); $request->setMethod('POST'); $request->getPost()->fromArray($json); $client = new Client(); $client->setEncType(Client::ENC_FORMDATA); $response = $client->dispatch($request); if (!$response->isSuccess()) { $logger->err( 'Slack failed to POST resource: ' . $url . ' (' . $response->getStatusCode() . " - " . $response->getReasonPhrase() . ').', array( 'request' => $client->getLastRawRequest(), 'response' => $client->getLastRawResponse() ) ); return false; } return true; } catch (\Exception $e) { $logger->err($e); } } public function getConfig() { return include __DIR__ . '/config/module.config.php'; } public function getAutoloaderConfig() { return array( 'Zend\Loader\StandardAutoloader' => array( 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, ), ), ); } }