<?php
/**
* @copyright 2013 Perforce Software. All rights reserved
* @license Please see LICENSE.txt in top-level folder of this distribution.
*/
namespace Commons_Cleaner;
use Projects\Model\Project;
use Zend\Mvc\MvcEvent;
use Activity\Model\Activity;
class Module
{
/**
* clean up the JSON left in the change description by Commons
*
* @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.change', // this attaches us only to task.change events
function ($event) use ($services) {
// a lot of events come through. we need to make sure this is actually an activity event
$model = $event->getParam('activity');
if (!$model instanceof Activity) {
return;
}
// get the description, and see if this has a block of JSON in it from Commons
$desc = '';
if( preg_match('/(.*?)(^{.*$)/sm', $model->get('description'), $pieces) == 0 ) {
$desc = $model->get('description');
}
else {
$desc = $pieces[1];
$raw_json = $pieces[2];
$action = 'commented in Commons on';
$target = 'some file';
// parse the JSON, if it is legal
$json = json_decode($raw_json);
if( json != NULL && $json->{'commentText'} != NULL )
{
$target = $json->{'fileRevisionLocation'}->{'path'};
$space = $json->{'fileRevisionLocation'}->{'spaceId'};
$type = $json->{'fileRevisionLocation'}->{'pathType'};
$model->set('link', 'http://commons.qa.perforce.com:1999/ui/file-details/space/'.$space.'/'.$type."/".$target[0]."?spaceName=".urlencode($json->{'fileRevisionLocation'}->{'spaceName'}).'&rev='.$json->{'fileRevisionLocation'}->{'revision'});
}
else if( json != NULL && $json->{'changeType'} != NULL && $json->{'changeType'} == 'RENAME_SPACE' ) {
$action = 'renamed';
$target = 'a space in Commons';
$model->set('link', 'http://commons.qa.perforce.com:1999/ui/space/');
}
else if( json != NULL && $json->{'changeType'} != NULL && $json->{'changeType'} == 'CREATE_SPACE' ) {
$action = 'created';
$target = 'a space in Commons';
$model->set('link', 'http://commons.qa.perforce.com:1999/ui/space/');
}
else {
return;
}
$model->set('action', $action);
$model->set('target', $target);
}
//
$model->set('description', $desc);
},
// set our priority, -90 puts us right before the end, but before the activity is published
-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
// 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 | |
|---|---|---|---|---|---|
| #1 | 8405 | Matt Attaway |
Add Swarm extension to cleanup Commons JSON change descriptions This is a module from our internal hackathon that identifies changes from Commons and cleans up JSON to make the change human legible. Install by copying 'Commons_Cleaner' into the 'modules' directory of your Swarm installation and restarting Apache. |