Module.php #4

  • //
  • guest/
  • matt_attaway/
  • Kato/
  • Module.php
  • View
  • Commits
  • Open Download .zip Download (5 KB)
<?php

namespace Kato;

use Projects\Model\Project;
use Zend\Mvc\MvcEvent;
use Activity\Model\Activity;

class Module
{
    /**
     * Publish Swarm activity to kato.im
     *
     * This module demonstrates firiing off a curl event based on incoming Perforce/Swarm activity.
     * You should only need to update the $room variable below.
     *
     * @param   Event   $event  the bootstrap event
     * @return  void
     */
    public function onBootstrap(MvcEvent $event)
    {
        $application  = $event->getApplication();
        $services     = $application->getServiceManager();
        $manager      = $services->get('queue');
        $events       = $manager->getEventManager();

        $events->attach(
            'task.commit',   // only fire on task.commit events
            function ($event) use ($services, $events) { // we need events and services for this module

                // sanity check to make sure this is a properly formed Activity event
                $model = $event->getParam('activity');
                if (!$model instanceof Activity) {
                    return;
                }

                // provide the kato.im URL for your particular room here.
                $room = "https://api.kato.im/rooms/36284a1d50c33c4593c3932b7e5f6942d2e4bd1af990ceb204c9c41cfeea935/simple";

                // build up the message
                $change       = $model->get('target');
                $user         = $model->get('user');
                $description  = $model->get('description');
                $qualifiedUrl = $services->get('viewhelpermanager')->get('qualifiedUrl');
                $url          = $qualifiedUrl('change', array( 'change' => substr($change, 7)));
                $message = json_encode(ucfirst("[$change](" . $url . ") submitted by $user\n\n$description"));
                $json    = '{\"from\": \"Robot\", \"color\": \"green\", \"renderer\": \"markdown\", \"text\":' . str_replace("\"",'\"', $message) . ' }';

                // some logging that may be handy for debugging
                // $services->get('logger')->err("/usr/bin/curl -X POST -d \"$json\" $room");
                // $services->get('logger')->err("$url");

                // post the message to our room using curl
                exec("/usr/bin/curl -X POST -d \"$json\" $room");

                return;
            },
            // set our priority, -90 to put ourselves near the end of the event pipeline
            -90
        );

        $events->attach(
            'task.review',   // only fire on task.review events
            function ($event) use ($services, $events) { // we need events and services for this module

                // sanity check to make sure this is a properly formed Activity event
                $model = $event->getParam('activity');
                if (!$model instanceof Activity) {
                    return;
                }

                // provide the kato.im URL for your particular room here.
                $room = "https://api.kato.im/rooms/36284a1d50c33c4593c3932b7e5f6942d2e4bd1af990ceb204c9c41cfeea935/simple";

                // build up the message
                $target       = $model->get('target');
                $user         = $model->get('user');
                $action       = $model->get('action');
                $qualifiedUrl = $services->get('viewhelpermanager')->get('qualifiedUrl');

                // $target can have a variety of values based on the event. We need to extract that change number
                if( strpos($target, 'review') === FALSE )
                {
                    $url = $qualifiedUrl('review', array( 'review' => $target));
                }
                else
                {
                    $url = $qualifiedUrl('review', array( 'review' => substr($target, 7)));
                }
                
                $message = json_encode("$user $action [$target](" . $url .")");
                $json    = '{\"from\": \"Robot\", \"color\": \"green\", \"renderer\": \"markdown\", \"text\":' . str_replace("\"",'\"', $message) . ' }';

                // some logging that may be handy for debugging
                // $services->get('logger')->err("/usr/bin/curl -X POST -d \"$json\" $room");
                // $services->get('logger')->err("$url");

                // post the message to our room using curl
                exec("/usr/bin/curl -X POST -d \"$json\" $room");

                return;
            },
            // set our priority, -90 to put ourselves near the end of the event pipeline
            -90
        );

    }

    // loads up our config file. all modules should have this method.
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    // makes sure our module is automatically loaded. otherwise the user would
    // need to explicitly install our module in the application
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}
# Change User Description Committed
#4 11193 Matt Attaway Stripping out unneeded copyrights from the file headers
#3 11191 Matt Attaway Add support for review events to the kato.im module
#2 11185 Matt Attaway Properly form the URL for commit notifitications to Kato.im
#1 11184 Matt Attaway WIP on a kato.im integration for Swarm